Adi
Adi

Reputation: 35

How Edit data of an XML node with Javascript

I want to write some data in an existing local XML file with Javascript with some text from an Html page. Is it possible to change content of nodes? Here is XML sample:

<Notepad>
    <Name>Player1</Name>
    <Notes>text1</Notes>
</Notepad>

I will get some more text from input and want to add it after "text1", but can't find a solution.

function SaveNotes(content,player)
		 {
			var xml = "serialize.xml";
			var xmlTree =  parseXml("<Notepad></Notepad>");
			var str = xmlTree.createElement("Notes");
		
           
			$(xmlTree).find("Notepad").find(player).append(str);
			$(xmlTree).find("Notes").find(player).append(content);
			var xmlString = (new XMLSerializer()).serializeToString(xmlTree);
			
					
			}
			

Upvotes: 0

Views: 676

Answers (3)

Jose Santacruz Lopez
Jose Santacruz Lopez

Reputation: 45

Try to use these two package one to convert to json and when is finish the other to come back

https://www.npmjs.com/package/xml2json

https://www.npmjs.com/package/js2xmlparser

Upvotes: 0

Vivek Solanki
Vivek Solanki

Reputation: 462

Here is the code to manipulate xml content or xml file : [Update] Please check this Fiddle

    var parseXml;

    parseXml = function(xmlStr) {
        return (new window.DOMParser()).parseFromString(xmlStr, "text/xml");
    };

var xmlTree = parseXml("<root></root>");

function add_children(child_name, parent_name) {
    str = xmlTree.createElement(child_name);
    //strXML = parseXml(str);
    $(xmlTree).find(parent_name).append(str);
    $(xmlTree).find(child_name).append("hello");
    var xmlString = (new XMLSerializer()).serializeToString(xmlTree);
    alert(xmlString);
}
add_children("apple", "root");
add_children("orange", "root");
add_children("lychee", "root");

you can use it for searching in xml as well as adding new nodes with content in it. (And sorry i dont know how to load xml from client side and display it.) but this fiddle demo will be helpful in adding content in xml and searching in it. Hope it helps :)

Upvotes: 1

robert
robert

Reputation: 978

If you want to achieve this on the client side you can parse your xml into a document object:

See https://developer.mozilla.org/en-US/docs/Web/Guide/Parsing_and_serializing_XML and http://www.w3schools.com/xml/tryit.asp?filename=tryxml_parsertest2

And then manipulate it like you would the DOM of any html doc, e.g. createElement, appendChild etc.

See https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement

Then to serialize it into a String again you could use https://developer.mozilla.org/en-US/docs/Web/API/Element/outerHTML

Persisting the data

Writing to a local file is not possible in a cross-browser way. In IE you could use ActiveX to read/write file.

You could use cookies to store data on the client side, if your data keeps small enough.

In HTML5 you could use local storage, see http://www.w3schools.com/html/html5_webstorage.asp

Upvotes: 0

Related Questions