0%

jQuery解析xml,QName的处理

后面会写个范例详细讲解析xml,这里只介绍QName的处理方法。 什么是QName?QName指的是元素名前带命名空间的元素名,如:

<db:attribute name="isbn10">7543639130</db:attribute>

db:attribute就是QName. 如果直接用$(xml).find(“db:attribute”),是找不到元素的,这里需要对冒号转义一下,写成$(xml).find(“db\:attribute”) 还有一点需要注意,上面的代码在WebKit浏览器不能运行(Chrome等),在这类浏览器中需要用$(xml).find(“attribute”),所以,需要先判断一下.

/**
 * 判断是否WebKit浏览器
 */
function isWebKit()
{
    var ua=navigator.userAgent.toLowerCase();
    if(ua.indexOf("webkit")>0)
    {
        return true;
    }
    return false;
}



var attribute;
if(isWebKit())
{
    attribute=$(xml).find("attribute");
}else{
    attribute= $(xml).find("db\\:attribute");
}