0

I have the following XML as a result from a webservice:

<response>
    <type>SUCCESS</type>
    <message/>
    <data>
        <rowset>
            <head>
               <!-- more tags here -->
            </head>
            <Row>
                <cell Col="ID">102</cell>
                <cell Col="SHIPMENT">1000036096</cell>
                <cell Col="RFC">test</cell>
                <cell Col="STATUS">SUCCESS</cell>
                <cell Col="FIRST_PROCESSING">2014-08-27T15:48:08</cell>
                <cell Col="LAST_PROCESSING">2014-08-27T15:57:59</cell>
                <cell Col="MESSAGE"/>
                <cell Col="RETRY_COUNT">2</cell>
            </Row>
            <Row>
                <cell Col="ID">100</cell>
                <cell Col="SHIPMENT">1000036157</cell>
                <cell Col="RFC">test</cell>
                <cell Col="STATUS">SUCCESS</cell>
                <cell Col="FIRST_PROCESSING">2014-08-27T15:29:58</cell>
                <cell Col="LAST_PROCESSING">2014-08-27T15:29:58</cell>
                <cell Col="MESSAGE"/>
                <cell Col="RETRY_COUNT">0</cell>
            </Row>
        </rowset>
    </data>
</response>

I want to get the part of the XML under the data tag. I need this part of the XML to pass to a javascript library that creates a grid based on this XML.

//webservice callback
onSuccess : function(xml){
    var  gridXML = $(xml).find("data").text();
},

The problem with this is that .text() only keeps the tag values and removes the tags. How can I get everything under data tag in string format?

EDIT: I tried .html() as suggested and it indeed returns the nodes under data. But the head tag is removed.

Anonymoose
  • 2,389
  • 6
  • 36
  • 69
  • 2
    Have you tried `.html()`? – Stryner Aug 27 '14 at 14:37
  • Maybe this will help you: http://stackoverflow.com/questions/1675027/how-do-i-get-the-entire-xml-string-from-a-xmldocument-returned-by-jquery-cross You can grab the `XML` as `text` and make some regexp to grab all text from `` until `` – RaphaelDDL Aug 27 '14 at 14:40
  • By `head` tag, do you mean the `data` tag? – Stryner Aug 27 '14 at 15:11
  • @Ninsly I forgot I did not include this part in my question, I simplified the XML. I updates my question now with the tag. – Anonymoose Aug 27 '14 at 17:23

2 Answers2

2

Now that you have specified your question further, it appears that your issue lies in that fact that jQuery will remove the <head> element when parsing it like html.

Example:

var myUnparsedDocument = "<response><test>" +
                             "<head>1</head>" +
                             "<data>2</data>" +
                         "</test></response>"

//this statement will yield: "1<data>2</data>"
$(myUnparsedDocument).find("test").html();

A way to get past this would be to tell jQuery to parse it as an XML document:

var myXMLDocument = $.parseXML(myUnparsedDocument);
$(myXMLDocument).find("test").html();

This will keep the <head> tag, as expected.

Fiddle Here

Stryner
  • 7,288
  • 3
  • 18
  • 18
1

Use html() instead of text().

Your code would look like this:

//webservice callback
onSuccess : function(xml){
    var  gridXML = $(xml).find("data").html();
},
Steve
  • 86
  • 4