开发者

Trouble retrieving inner text from XML node using JavaScript

开发者 https://www.devze.com 2023-01-02 08:06 出处:网络
I\'m reading an XML document using JavaScript & jQuery, and need to extract some text from inside a node to save into an array. The structure of the开发者_开发技巧 XML is as such:

I'm reading an XML document using JavaScript & jQuery, and need to extract some text from inside a node to save into an array. The structure of the开发者_开发技巧 XML is as such:

<C>
  <I>
    <TEXTFORMAT>
      <P>
        <FONT>Here's the text I want</FONT>
      </P>
    </TEXTFORMAT>
  </I>
</C>

Everything I've tried so far returns nothing so I must be incorrectly referencing the contents of the FONT tag.

What XML path should I be using?


This will give you an array of the content of the FONT nodes.

var array = $(xml).find('FONT').map(function() {
    return $(this).text();
}).get();

Relevant jQuery docs:

  • .map() - http://api.jquery.com/map/
  • .text() - http://api.jquery.com/text/
  • .get() - http://api.jquery.com/get/
  • .find() - http://api.jquery.com/find/


function parseXml(xml)
{
    //find every FONT element and store its value
    $(xml).find("FONT").each(function()
    {
        // put this.text() into the array
    });

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号