Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 750
Brian Liang Avatar asked May 12 '09 16:05

Brian Liang


People also ask

How can we give namespace in XML?

XML Namespaces - The xmlns Attribute When using prefixes in XML, a namespace for the prefix must be defined. The namespace can be defined by an xmlns attribute in the start tag of an element. The namespace declaration has the following syntax. xmlns:prefix="URI".

Can XML attributes have namespaces?

An XML namespace is a collection of names that can be used as element or attribute names in an XML document. The namespace qualifies element names uniquely on the Web in order to avoid conflicts between elements with the same name.

Can XML have multiple namespaces?

When you use multiple namespaces in an XML document, you can define one namespace as the default namespace to create a cleaner looking document. The default namespace is declared in the root element and applies to all unqualified elements in the document. Default namespaces apply to elements only, not to attributes.

Can an XML schema define namespaces?

Figure 1: Elements and attributes in XML Schema namespace are used to write an XML Schema document, which generates elements and attributes as defined by user and puts them in {target namespace}. This {target namespace} is then used to validate the XML instance.


2 Answers

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]") 
like image 113
Brian Liang Avatar answered Sep 28 '22 07:09

Brian Liang


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() 
like image 38
Fasani Avatar answered Sep 28 '22 06:09

Fasani