Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create and modify xml file using javascript

Tags:

javascript

xml

How do i create new xml file and also modify any xml file means add more nodes in xml file using javascript.?

Thanks in advance...

like image 811
Avinash Avatar asked Jul 28 '09 06:07

Avinash


People also ask

Can JavaScript manipulate XML?

Manipulating XML file data using JavaScript. Actually, just the last two lines of the function are enough to load the XML file. The previous two lines are written to ensure that the JavaScript functions that we may use later to manipulate the XML file data, does not perform any function on an uninitialized object.

How write XML file in JavaScript?

var node = document. createElement("Item"); This will create HTML nodes instead of XML nodes and will result in a node with lower-case tag names. XML tag names are case-sensitive in contrast to HTML tag names.

How do I edit an XML file with node?

Converting to XML and Writing to FIle readFile("test. xml", "utf-8", function(err, data) { if (err) console. log(err); // we log out the readFile results console. log(data); // we then pass the data to our method here parseString(data, function(err, result) { if (err) console.

How do you change values in XML?

Change the Value of an Attribute In the DOM, attributes are nodes. Unlike element nodes, attribute nodes have text values. The way to change the value of an attribute, is to change its text value. This can be done using the setAttribute() method or setting the nodeValue property of the attribute node.


1 Answers

I've found Ariel Flesler's XMLWriter constructor function to be a good start for creating XML from scratch, take a look at this

http://flesler.blogspot.com/2008/03/xmlwriter-for-javascript.html

Example

function test(){    
   var v = new  XMLWriter();
   v.writeStartDocument(true);
   v.writeElementString('test','Hello World');
   v.writeAttributeString('foo','bar');
   v.writeEndDocument();
   console.log( v.flush() );
}

Result

<?xml version="1.0" encoding="ISO-8859-1" standalone="true" ?>
<test foo="bar">Hello World</test>

One caveat to keep in mind is that it doesn't escape strings.

See also Libraries to write xml with JavaScript

like image 135
Alex Nolasco Avatar answered Sep 30 '22 11:09

Alex Nolasco