How to use jQuery for XML parsing with namespaces
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>
.
So far, I've been using:
$.get(xmlPath, {}, function(xml) {
$("rs:data", xml).find("z:row").each(function(i) {
alert("found zrow");
});
}, "xml");
with really no luck. Any ideas?
I got it.
Turns out that it requires \\
to escape the colon.
$.get(xmlPath, {}, function(xml) {
$("rs\\:data", xml).find("z\\:row").each(function(i) {
alert("found zrow");
});
}, "xml");
As Rich pointed out:
The better solution does not require escaping and works on all "modern" browsers:
.find("[nodeName=z:row]")
I have spent several hours on this reading about plugins and all sorts of solutions with no luck.
ArnisAndy posted a link to a jQuery discussion, where this answer is offered and I can confirm that this works for me in Chrome(v18.0), FireFox(v11.0), IE(v9.08) and Safari (v5.1.5) using jQuery (v1.7.2).
I am trying to scrape a WordPress feed where content is named <content:encoded> and this is what worked for me:
content: $this.find("content\\:encoded, encoded").text()
If you are using jquery 1.5 you will have to add quotes around the node selector attribute value to make it work:
.find('[nodeName="z:row"]')
Although the above answer seems to be correct, it does not work in webkit browsers (Safari, Chrome). A better solution I believe would be:
.find("[nodeName=z:myRow, myRow]")
In case someone needs to do this without jQuery, just with normal Javascript, and for Google Chrome (webkit), this is the only way I found to get it to work after a lot of research and testing.
parentNode.getElementsByTagNameNS("*", "name");
That will work for retrieving the following node: <prefix:name>
. As you can see the prefix or namespace is omitted, and it will match elements with different namespaces provided the tag name is name
. But hopefully this won't be a problem for you.
None of this worked for me (I am developping a Google Chrome extension):
getElementsByTagNameNS("prefix", "name")
getElementsByTagName("prefix:name")
getElementsByTagName("prefix\\:name")
getElementsByTagName("name")
Edit: after some sleep, I found a working workaround :) This function returns the first node matching a full nodeName
such as <prefix:name>
:
// Helper function for nodes names that include a prefix and a colon, such as "<yt:rating>"
function getElementByNodeName(parentNode, nodeName)
{
var colonIndex = nodeName.indexOf(":");
var tag = nodeName.substr(colonIndex + 1);
var nodes = parentNode.getElementsByTagNameNS("*", tag);
for (var i = 0; i < nodes.length; i++)
{
if (nodes[i].nodeName == nodeName) return nodes[i]
}
return undefined;
}
It can easily be modified in case you need to return all the matching elements. Hope it helps!