I've loaded two XML documents using jQuery's $.ajax() and serialized each to strings, performed a few string manipulations, combined the two strings together, wrapped them in a node, located child nodes using $.find(), and now want to convert the $.find() result back to an XMLDocument.
A much abbreviated example:
$.isXMLDoc(xmlDocument1); // returns true
$.isXMLDoc(xmlDocument2); // returns true
var xml1Str = (new XMLSerializer()).serializeToString(xmlDocument1);
var xml2Str = (new XMLSerializer()).serializeToString(xmlDocument2);
var xml1And2Str = "<root>" + xml1Str + xml2Str + "</root>";
var xml1And2Doc = $.parseXML(xml1And2Str);
$.isXMLDoc(xml1And2Doc); // returns true
var $oneNode = $(xml1And2Doc).find("oneNodeName");
Somewhere inside of xml1And2Doc there is a single xml child-element that looks like <oneNodeName>...</oneNodeName>
. The last statement finds that for me and then I can successfully use it, and afterwards, I want to convert it into an XMLDocument. How can I convert $oneNode
to an XMLDocument
?
There are examples that build up to this, but I believe I'm missing something fundamental here about jQuery or the find() results, which bar me from being able to convert that to an XMLDocument.
References:
Here is a solution. After calling $(xml1And2Doc).find("oneNodeName")
you need to get the first element in the array that jQuery is returning. Here is a snippet:
var $oneNode = $(xml1And2Doc).find("oneNodeName")[0]; //finds and selects first object
var aString = (new XMLSerializer()).serializeToString($oneNode);
jQuery by default returns a collection of DOM elements see. So accessing the first element is fine with $oneNode[0] but if it's possible for there to be more than one of those elements you could do this:
var $oneNodes = $(xml1And2Doc).find("oneNodeName"); //finds set of objects
$.each($oneNodes, function (index) {
var aString = (new XMLSerializer()).serializeToString($oneNodes[index]);
});