How to use jQuery for XML parsing with namespaces

后端 未结 20 1441
隐瞒了意图╮
隐瞒了意图╮ 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:42

    content: $this.find("content\\:encoded, encoded").text()

    is the perfect solution...

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

    just replaced the namespace by empty string. Works fine for me. Tested solution across browsers: Firefox, IE, Chrome

    My task was to read and parse an EXCEL-file via Sharepoint EXCEL REST API. The XML-response contains tags with "x:" namespace.

    I decided to replace the namespace in the XML by an empty string. Works this way: 1. Get the node of interest out of the XML-response 2. Convert the selected node XML-Response (Document) to String 2. Replace namespace by empty string 3. Convert string back to XML-Document

    See code outline here -->

    function processXMLResponse)(xData)
    {
      var xml = TOOLS.convertXMLToString("", "",$(xData).find("entry content")[0]);
      xml = xml.replace(/x:/g, "");            // replace all occurences of namespace
      xData =  TOOLS.createXMLDocument(xml);   // convert string back to XML
    }
    

    For XML-to-String conversion find a solution here: http://www.sencha.com/forum/showthread.php?34553-Convert-DOM-XML-Document-to-string

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

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

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

    None of the solutions above work that well. I found this and has been improved for speed. just add this, worked like a charm:

    $.fn.filterNode = function(name) {
        return this.find('*').filter(function() {
           return this.nodeName === name;
        });
    };
    

    usage:

    var ineedthatelementwiththepsuedo = $('someparentelement').filterNode('dc:creator');
    

    source: http://www.steveworkman.com/html5-2/javascript/2011/improving-javascript-xml-node-finding-performance-by-2000/

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

    Original Answer : jQuery XML parsing how to get element attribute

    Here is an example for how to successfully get the value in Chrome..

     item.description = jQuery(this).find("[nodeName=itunes\\:summary]").eq(0).text();
    
    0 讨论(0)
提交回复
热议问题