www.webdeveloper.com
Results 1 to 3 of 3

Thread: Deleting XML nodes?

  1. #1
    Join Date
    Apr 2009
    Posts
    4

    Question Deleting XML nodes?

    This is a sample of my XML file:

    <blogs>
    <blog>
    <id>0</id>
    <title>Day Two</title>
    <bbody>betcha gonna work now..</bbody>
    <image>../Images/BlogImages/flower.gif</image>
    <bdate>Wednesday, 8/3/2009, 6:26pm</bdate>
    <buser>Guest</buser>
    <comments>
    <comment>
    <cid>0</cid>
    <cbody>hahahahaha sucks to be u!!</cbody>
    <cuser>Stacey</cuser>
    <cdate>Tuesday, 7/3/2009, 3:20pm</cdate>
    </comment>
    </comments>
    </blog>
    <blog>
    <id>1</id>
    <title>fasd</title>
    <bbody>fads</bbody>
    <image>../Images/BlogImages/flower.gif</image>
    <bdate>Friday, 10/3/2009, 4:0pm</bdate>
    <buser>Guest</buser>
    <comments>
    <comment>
    <cid>1</cid>
    <cbody>hahahahaha sucks to be u!!</cbody>
    <cuser>Stacey</cuser>
    <cdate>Tuesday, 7/3/2009, 3:20pm</cdate>
    </comment>
    <comment>
    <cid>1</cid>
    <cbody>i know.. =P</cbody>
    <cuser>Christie</cuser>
    <cdate>Tuesday, 7/3/2009, 3:20pm</cdate>
    </comment>
    </comments>
    </blog>
    </blogs>



    When I wish to add a new blog, this code is run:

    var blogsTag = xmldom2.getElementsByTagName("blogs")[0];
    var comments = xmldom2.getElementsByTagName("comment");
    var blogs = xmldom2.getElementsByTagName("blog");
    var newBlogNode = blogs[0].cloneNode(true); // deep clone of first message
    newBlogNode.getElementsByTagName("id")[0].firstChild.nodeValue =ID;
    newBlogNode.getElementsByTagName("title")[0].firstChild.nodeValue = Title;
    newBlogNode.getElementsByTagName("bbody")[0].firstChild.nodeValue = Body;
    newBlogNode.getElementsByTagName("image")[0].firstChild.nodeValue = Image;
    newBlogNode.getElementsByTagName("bdate")[0].firstChild.nodeValue = Date;
    newBlogNode.getElementsByTagName("buser")[0].firstChild.nodeValue = Name;
    blogsTag.appendChild(newBlogNode); // append new message node to blogs

    xhr2.onreadystatechange = ProcessResponse2;
    xhr2.open("POST", "saveXML.asp?file=blog.xml", true);
    xhr2.send(xmldom2);



    This code works perfectly to add a new blog. However, because the new blog is a clone of the first blog, all the comments of the first blog aren't deleted for the new blog when it is created.

    How do I delete all the nodes with the name 'comment'?
    What extra lines in my code do I need?

  2. #2
    Join Date
    May 2006
    Location
    Odenton, MD
    Posts
    1,449
    after you clone one blog, remove the element comments, then create a new element comments, and append to cloned blog:

    Code:
    newBlogNode.removeChild(newBlogNode.getElementsByTagName('comments')[0]);
    newBlogNode.appendChild(document.createElement('comments'));
    of course, you will end up with a blog with no comments.

  3. #3
    Join Date
    Apr 2009
    Posts
    4
    Thankyou SO much! It worked perfectly.
    I have searched the internet for hours trying to solve this problem.

    I can't say thankyou enough!

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