Boog
Boog

Reputation: 3863

Parsing an XML string fragment out of document in Javascript

I'm trying to use DOMParser, or XPath to get a XML fragment out of a document. A element with either DOMParser or document.evaluate returns an element with a null nodeValue, how do I get the xml fragment back as a string? Here's the example that I can't get to work in WebKit.

XML:

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
  <entity name="contact">
    <attribute name="address1_stateorprovince" />
    <attribute name="new_source" />
    <attribute name="ownerid" />
    <attribute name="new_organization" />
    <attribute name="new_lastcontacted" />
    <attribute name="emailaddress1" />
    <attribute name="address1_city" />
    <attribute name="telephone1" />
    <order attribute="fullname" descending="false" />
    <filter type="and">
      <condition attribute="new_conflicting" operator="eq" value="1" />
    </filter>
    <attribute name="fullname" />
    <attribute name="new_csuid" />
    <attribute name="new_contacttype" />
    <attribute name="contactid" />
  </entity>
</fetch>

Source:

var parser = new DOMParser();
var filterXmlDoc = parser.parseFromString(xml, "text/xml");
var test = filterXmlDoc.getElementsByTagName("filter")[0];
test.nodeValue; // null!

Upvotes: 1

Views: 2566

Answers (2)

Crescent Fresh
Crescent Fresh

Reputation: 116980

nodeValue is the "value" of the node, which for Element types is meaningless (and rightly null). It is not the xml string representation of the node.

For WebKit you can use XMLSerializer to get the string representation back out:

var xml = new XMLSerializer().serializeToString(test);
xml; // <filter type="and"><condition attribute="new_conflicting" operator="eq" value="1"/></filter>

Update re comment "Is there an easy way to only get the inner xml?":

You could construct a DocumentFragment from the child nodes, and serialize just the fragment:

var frag = filterXmlDoc.createDocumentFragment();
while(test.firstChild) {
  frag.appendChild(test.firstChild);
}
var xml = new XMLSerializer().serializeToString(frag);
xml; // <condition attribute="new_conflicting" operator="eq" value="1"/>

Note that this will empty out your test node after running.

Upvotes: 4

Darrell Brogdon
Darrell Brogdon

Reputation: 6973

Try:

test.items(0).nodeValue;

getElementsByTagName() returns a NodeList so you must use items() method of that object.

Though ideally you would do:

if (test.length)
{
    console.log(test.items(0).nodeValue);
}
else
{
    console.log('Nothing Found');
}

Upvotes: 0

Related Questions