jQuery Children Before syntax issue



I'm pulling in an XML document through AJAX and trying to insert a new node at the top of the document, but I'm getting a new node inserted above EVERY matching child node. I had thought that by using .children(0) I would only do this once.


What is my error?



$.ajax({
type: "GET",
url: fileUrl,
dataType: "xml",
success: parseResultsXML
});
function parseResultsXML(xml) {
$(xml).children(0).children(0).before("<item>New Node</item> \n");
var xmlOutput = new XMLSerializer().serializeToString(xml);
console.log(newfile, xmlOutput);
}


.



//Original File
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>Existing Node 1</item>
<item>Existing Node 2</item>
</items>

//Expected Output
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>New Node</item>
<item>Existing Node 1</item>
<item>Existing Node 2</item>
</items>

//Actual Output
<items>
<item>New Node</item>
<item>Existing Node 1</item>
<item>New Node</item>
<item>Existing Node 2</item>
</items>

No comments:

Post a Comment