1

I'm using the the XML for < script > library W3C DOM Parser available here. (This because I'm using js inside a .NET CLI Jint program instead of a browser.)

My problem is, I edit a XML using DOMImplementation.loadXML() function and then I edit one node value, then I want to retrieve as string all the XML including the modified one:

 //instantiate the W3C DOM Parser
 var parser = new DOMImplementation();

 //load the XML into the parser and get the DOMDocument
 var domDoc = parser.loadXML($xmlData);

 //get the root node (in this case, it is ROOTNODE)
 var docRoot = domDoc.getDocumentElement();

 // change "name" element value
 docRoot.getElementsByTagName("name").item(0).nodeValue="John";

 // try to retreive all the XML including the changed one
 $xmlData=docRoot.getXML();

Sample XML:

<name>Carlos</name>

All works good except the last line:

docRoot.getXML();

Which only returns the original XML as string and not the modified one.

Any idea how can I retreive all the XML as string including the modified?

RandomGuy
  • 648
  • 6
  • 21
  • Hey RandomGuy, sorry for being Slightly off topic: I wanted to ask how you managed to import DOMImplementation in Jint. I tried but it always for a reason or another, for instance, the "alerts" are not known. did you alter the sources on any way to get it to work ? Thank you very much :) – Fabio Salvalai Sep 06 '15 at 14:31
  • You mean how do I imported the XML for < script > library in Jint? Well I used this method: http://stackoverflow.com/questions/7059140/how-do-i-load-a-javascript-file-with-jint-in-c/16617805#16617805. Load tinyxmlsax.js and tinyxmlw3cdom.js that way. – RandomGuy Sep 06 '15 at 18:55
  • That's basically the approach I took, unfortunately, I keep on getting an exception when executing the script. Additional information: DOMImplementation is not defined Also, tinyxmlsax.js doesn't contain any declaration of DOMImplementation – Fabio Salvalai Sep 06 '15 at 20:11
  • Unfortunately I don't have the source code that I used at that time. I rewrote my application in C++ and used http://jsxml.sourceforge.net/ js library instead since it's much faster than C# with XML for < script >. Maybe you can also try jsxml and see if it works for you. – RandomGuy Sep 06 '15 at 21:33
  • Alright, I'll give it a try, thanks for the help – Fabio Salvalai Sep 07 '15 at 05:44

1 Answers1

0

Fixed it replacing:

docRoot.getElementsByTagName("name").item(0).nodeValue="John";

by:

docRoot.getElementsByTagName("name").item(0).firstChild.setNodeValue("John");

Seems I needed to call firstChild to text in the Element "name" be really replaced. With this docRoot.getXML() returned all the XML code including the modified one.

RandomGuy
  • 648
  • 6
  • 21