Javascript 使用jquery解析带有名称空间的xml文件

Javascript 使用jquery解析带有名称空间的xml文件,javascript,jquery,xml-parsing,Javascript,Jquery,Xml Parsing,任何人都能帮我用jquery解析这个文件吗。此文件包含名称空间,我在解析此文件时遇到问题 这就是xml 此XML文件似乎没有任何与之关联的样式信息。文档树如下所示 <app:categories xmlns:app="http://www.w3.org/2007/app" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:yt="http://gdata.youtube.com/schemas/2007" fixed="no" scheme="h

任何人都能帮我用jquery解析这个文件吗。此文件包含名称空间,我在解析此文件时遇到问题

这就是xml

此XML文件似乎没有任何与之关联的样式信息。文档树如下所示

<app:categories xmlns:app="http://www.w3.org/2007/app" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:yt="http://gdata.youtube.com/schemas/2007" fixed="no" scheme="http://gdata.youtube.com/schemas/2007/educategories.cat">
<atom:category term="0" label="Primary & Secondary Education" xml:lang="en-US"/>
<atom:category term="1" label="Fine Arts" xml:lang="en-US">
<yt:parentCategory term="0"/>
</atom:category>
<atom:category term="2" label="Dance" xml:lang="en-US">
<yt:parentCategory term="1"/>
</atom:category>
<atom:category term="3" label="Dramatic Arts & Theater" xml:lang="en-US">
<yt:parentCategory term="1"/>
</atom:category>
<atom:category term="4" label="Music" xml:lang="en-US">
<yt:parentCategory term="1"/>
</atom:category>
<atom:category term="405" label="Social Work" xml:lang="en-US">
<yt:parentCategory term="375"/>
</atom:category>
<atom:category term="406" label="Sociology" xml:lang="en-US">
<yt:parentCategory term="375"/>
</atom:category>
</app:categories>

您显示的是无效的XML。您必须用&;替换&;。有一些在线XML格式化程序站点,例如,可以帮助您获得有效的XML

一旦获得了有效的XML,就可以使用XML解析器(如$.parseXML函数)对其进行解析

以下是如何检索所有类别标签的示例:

var xml = '<app:categories xmlns:app="http://www.w3.org/2007/app" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:yt="http://gdata.youtube.com/schemas/2007" fixed="no" scheme="http://gdata.youtube.com/schemas/2007/educategories.cat"><atom:category term="0" label="Primary &amp; Secondary Education" xml:lang="en-US"/><atom:category term="1" label="Fine Arts" xml:lang="en-US"><yt:parentCategory term="0"/></atom:category><atom:category term="2" label="Dance" xml:lang="en-US"><yt:parentCategory term="1"/></atom:category><atom:category term="3" label="Dramatic Arts &amp; Theater" xml:lang="en-US"><yt:parentCategory term="1"/></atom:category><atom:category term="4" label="Music" xml:lang="en-US"><yt:parentCategory term="1"/></atom:category><atom:category term="405" label="Social Work" xml:lang="en-US"><yt:parentCategory term="375"/></atom:category><atom:category term="406" label="Sociology" xml:lang="en-US"><yt:parentCategory term="375"/></atom:category></app:categories>';    
var data = $.parseXML(xml);
var categories = $(data).find('category');
$.each(categories, function() {
    var label = $(this).attr('label');
    console.log(label);
});

请注意,在这种情况下,您不需要调用$.parseXML函数,因为jQuery将在调用AJAX成功回调之前自动为您调用它,它将直接提供解析的XML。

您需要发布代码,而不仅仅是一个链接,它是XML文件。我不知道如何解析它,因为我没有在jquery中解析xml,我需要一些示例或帮助。您需要在问题中显示xml,而不是发布一些链接。如何加载xml。。。您是否在使用ajax?如果是,您的脚本是否与xml文件的domaini使用$.parseXMLxml文件路径的域位于同一个域中。我在本地服务器上运行我的应用程序,文件也存储在本地。我非常感谢你的帮助,你帮了我很多。这是完美的工作。我需要先用格式化程序检查我的xml。
$.get('/file.xml', function(xml) {
    var categories = $(xml).find('category');
    $.each(categories, function() {
        var label = $(this).attr('label');
        console.log(label);
    });
}, 'xml');