Xenthide
Xenthide

Reputation: 79

How can I reference a specific DIV tag INSIDE an XML tag with jquery?

Title says it all basically.

I am using the eBay trading API and am trying to extract the text from a message sent by the user. The API somewhat unhelpfully returns a ton of HTML with the actual user message inside a div with ID UserInputtedText.

I am trying to get this data out using jQuery - the raw XML response is roughly in the format:

<XMLresponse>
    <Messages>
        <Message>
            <Text>
                "<!DOCTYPE html PUBLIC ...>\n\n\n<html ...>\n\n
                    <div id="UserInputtedText">
                        ****MESSAGE FROM USER ****

So I have been trying to use some variation of...

$xml = $.parseXML( xmlresponse )
$xml.find("Message").find('Text').find('UserInputtedText').html()

...but nothing seems to work.

I should perhaps note that I can get to the Text node itself and could almost definitely get the data out in more complicated multi-step process but I feel like there must be a simpler way.

In fact I notice now that the node seems to have the HTML wrapped in " and also interspersed with a bunch of \n line breaks as well which seems weird to me. Can anyone shed any light on why this is?

Upvotes: 0

Views: 1111

Answers (2)

Guffa
Guffa

Reputation: 700262

You need to parse it in two steps, as the XML contains the HTML code as a value. Once you have parsed the XML, the HTML code is just text, so you can't use a selector to find elements in it.

You would create a jQuery object for the XML document to traverse it:

$xml = $($.parseXML(xmlresponse));

Get the text from the Text node and turn that from HTML code to elements, then you can find the element in it:

var html = $xml.find("Message").find('Text').text();
$(html).find('#UserInputtedText').html()

Upvotes: 2

Sushil
Sushil

Reputation: 2835

I am not sure if this might work but you can try getting the value of the node and then do a $(val).find(yourdiv). haven't tried this though.

Upvotes: 0

Related Questions