4

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?

6
  • Does $(xml).find('TEXTFORMAT P FONT').text() not work? Commented Jun 7, 2010 at 12:38
  • That does work, however it retrieves the content of every FONT tag in the XML, I need it to only retrieve the content of the node it's currently looking at. What should I change? Commented Jun 7, 2010 at 12:43
  • <FONT>? ouch. What have you tried? sounds like $("font") would work fine in this situation. Commented Jun 7, 2010 at 12:43
  • What does "the content of the node it's currently looking at" mean? What is it and why is it staring at your nodes? Commented Jun 7, 2010 at 12:53
  • 1
    @JackRoscoe - if you are already working on pieces of the XML document, you can use jQuery to work on those pieces too, rather than the entire document. So instead of $(myEntireXmlDoc).find(...), do $(theContentIAmLookingAt).find(...). Also, if you are already working on a raw XML object, you should be able to drill down to the text like this: theXmlIAmLookingAt.getElementsByTagName('font')[0].firstChild.nodeValue. Commented Jun 7, 2010 at 13:04

2 Answers 2

2

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:

Sign up to request clarification or add additional context in comments.

Comments

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.