XML How to preserve Tree Layout

wxdqz

New Member
Hi,

I'm working on a small database using Javascript and XML.
The problem is, when I add a new node to my XML file using Javascript, it is not formatted in the XML-tree kindof style, but it's put in one long line. How can I preserve the tree style in my XML document when adding nodes?

Example of tree:
<XML>
<BOOK REF="0">
<TITLE>Blah</TITLE>
<AUTHOR>Blah</AUTHOR>
</BOOK>
</XML>

When adding node it looks like this:
<XML>
<BOOK REF="0">
<TITLE>Blah</TITLE>
<AUTHOR>Blah</AUTHOR>
</BOOK>
<BOOK REF="1"><TITLE>blah</TITLE><AUTHOR>blah</AUTHOR></BOOK></XML>

I use a function like this one to add a new node to the XML file:

// Create the elements
var newElem = xml.createElement("BOOK");
var newElem2 = xml.createElement("TITLE");
var newElem3 = xml.createElement("AUTHOR");

// Set attribute and values
newElem.setAttribute('REF', 'someValue')
newElem2.text = "blah";
newElem3.text = "blah";

// Append childs to node
newElem.appendChild(newElem2);
newElem.appendChild(newElem3);

// Append node to root of XML file
xml.documentElement.appendChild(newElem);
 
Back
Top