How to use jQuery for XML parsing with namespaces

后端 未结 20 1440
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 10:17

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:



        
相关标签:
20条回答
  • 2020-11-22 10:19

    As of beginning of 2016, for me the following syntax works with jQuery 1.12.0:

    • IE 11 (11.0.9600.18204, Update 11.0.28, KB3134815): .find("z\\:row")
    • Firefox 44.0.2: .find("z\\:row")
    • Chrome 44.0.2403.89m: .find("row")

    The syntax .find("[nodeName=z:row]") doesn't work in any of the browsers mentioned above. I found no way to apply a namespace in Chrome.

    Putting it all together, the following syntax works in all of the browsers mentioned above: .find("row,z\\:row")

    0 讨论(0)
  • 2020-11-22 10:20

    I have not seen any documentation on using JQuery to parse XML. JQuery typically uses the Browser dom to browse an HTML document, I don't believe it reads the html itself.

    You should probably look at the built in XML handling in JavaScript itself.

    http://www.webreference.com/programming/javascript/definitive2/

    0 讨论(0)
  • 2020-11-22 10:23

    It's worth noting that as of jQuery 1.7 there were issues with some of the work-arounds for finding namespaced elements. See these links for more information:

    • jQuery Bug Ticket noting the lack of cross-browser support for the ability to do .find('[nodeName="z:row"]')
    • A suggested plug-in workaround with some performance tests.
    0 讨论(0)
  • 2020-11-22 10:24

    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]")
    
    0 讨论(0)
  • 2020-11-22 10:26

    Alternatively, you can use fast-xml-parser in your project, and convert the XML data into JS/JSON object. Then you can use it as object property. It doesn't use JQuery or other libraries but it'll solve your purpose.

    var xmlData = '<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>'
    
    var jsObj = parser.parse(xmlData,{attrPrefix:"",ignoreTextNodeAttr: false});
    document.write(JSON.stringify(jsObj.xml["rs:data"]["z:row"][0],null,4) + "<br>");
    document.write(JSON.stringify(jsObj.xml["rs:data"]["z:row"][1],null,4) + "<br>");
    document.write(JSON.stringify(jsObj.xml["rs:data"]["z:row"][2],null,4) + "<br>");
    <script src="https://cdnjs.cloudflare.com/ajax/libs/fast-xml-parser/2.9.2/parser.min.js"></script>

    You can ignore namespaces while parsing to js/json object. In this case you can directly access as jsObj.xml.data.row.

    for(var i=0; i< jsObj.xml.data.row.length; i++){
      console.log(jsObj.xml.data.row[i]);
    }
    

    Disclaimer: I've created fast-xml-parser.

    0 讨论(0)
  • 2020-11-22 10:27

    jQuery 1.7 doesn't work with the following:

    $(xml).find("[nodeName=a:IndexField2]")
    

    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:

    $(xml).find('a\\\\:IndexField2, IndexField2')
    

    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.

    0 讨论(0)
提交回复
热议问题