[Solved] parse a xml file with name spaces using j query


What you have shown is invalid XML. You must replace the & with &. There are online XML formatter sites such as freeformatter.com that might assist you in having valid XML.

Once you have valid XML you can parse it with an XML parser such as the $.parseXML function.

Here’s an example of how you can retrieve all the category labels:

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);
});

If the XML is stored on your server you will need to first retrieve it using AJAX for example and then simply find the elements you are looking for:

$.get('/file.xml', function(xml) {
    var categories = $(xml).find('category');
    $.each(categories, function() {
        var label = $(this).attr('label');
        console.log(label);
    });
}, 'xml');

Note that in this case you don’t need to call the $.parseXML function because jQuery will automatically call it for you before invoking the AJAX success callback and it will directly provide the parsed XML.

1

solved parse a xml file with name spaces using j query