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!
nodeValueis the “value” of the node, which for Element types is meaningless (and rightlynull). It is not the xml string representation of the node.For WebKit you can use
XMLSerializerto get the string representation back out:Update re comment “Is there an easy way to only get the inner xml?”:
You could construct a
DocumentFragmentfrom the child nodes, and serialize just the fragment:Note that this will empty out your
testnode after running.