I'm new to jQuery and would like to parse an xml document.
I'm able to parse regular XML with the default namespaces but with xml such as:
<xml xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
<s:Schema id="RowsetSchema">
<s:ElementType name="row" content="eltOnly" rs:CommandTimeout="30">
<s:AttributeType name="ows_ID" rs:name="ID" rs:number="1">
<s:datatype dt:type="i4" dt:maxLength="4" />
</s:AttributeType>
<s:AttributeType name="ows_DocIcon" rs:name="Type" rs:number="2">
<s:datatype dt:type="string" dt:maxLength="512" />
</s:AttributeType>
<s:AttributeType name="ows_LinkTitle" rs:name="Title" rs:number="3">
<s:datatype dt:type="string" dt:maxLength="512" />
</s:AttributeType>
<s:AttributeType name="ows_ServiceCategory" rs:name="Service Category" rs:number="4">
<s:datatype dt:type="string" dt:maxLength="512" />
</s:AttributeType>
</s:ElementType>
</s:Schema>
<rs:data>
<z:row ows_ID="2" ows_LinkTitle="Sample Data 1" />
<z:row ows_ID="3" ows_LinkTitle="Sample Data 2" />
<z:row ows_ID="4" ows_LinkTitle="Sample Data 3" />
</rs:data>
</xml>
All I really want are the <z:row>
's.
So far, I've been doing:
$.get(xmlPath, {}, function(xml) {
$("rs:data", xml).find("z:row").each(function(i) {
alert("found zrow");
});
}, "xml");
With really no luck. Any ideas? Thanks.
jQuery 1.7 doesn't work with the following:
One solution which I did get to work in Chrome, Firefox, and IE is to use selectors which work in IE AND selectors which work in Chrome, based on the fact that one way works in IE and the other in Chrome:
In IE, this returns nodes using the namespace (Firefox and IE require the namespace), and in Chrome, the selector returns nodes based on the non-namespace selector. I have not tested this in Safari, but it should work because it's working in Chrome.
If you are using jquery 1.5 you will have to add quotes around the node selector attribute value to make it work:
My solution (because I use a Php proxy) is to replace : namespace by _ ... so no more namespace issues ;-)
Keep it simple !
Original Answer : jQuery XML parsing how to get element attribute
Here is an example for how to successfully get the value in Chrome..
For Webkit browsers, you can just leave off the colon. So to find
<media:content>
in an RSS feed for example, you can do this:Found solution in the comment: Parsing XML with namespaces using jQuery $().find
My setup:
Sample XML (snippet from Google Contacts API):
Parsing code:
Plnkr: http://plnkr.co/edit/l8VzyDq1NHtn5qC9zTjf?p=preview