www.webdeveloper.com
Results 1 to 3 of 3

Thread: XML How to preserve Tree Layout

  1. #1
    Join Date
    Feb 2004
    Posts
    4

    Question XML How to preserve Tree Layout

    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);

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,120
    if your document starts with xml doc declaration

    <?xml version="1.0"?>

    then browser had a built in style sheet to display it in tree structure,make sure when u add new node you don't change the document
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  3. #3
    Join Date
    Feb 2004
    Posts
    4

    Thumbs up

    Thx a million, I'll give it a try!

    Greetz,
    Christian

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles