Problem with updating the XML file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NawazAhmed
    New Member
    • Feb 2008
    • 36

    Problem with updating the XML file

    Hi,
    I was trying to insert new node and data into the xml file.
    Let say If I am inserting the data from page1 at line 4 and displaying the inserted data at line 5, its displaying it perfectly but when I go to the xml file and look for the inserted data its not there. I am I doing any mistake in the code below????
    This is the example from w3schools.com
    this goes into ur source
    <HTML>
    <HEAD>
    </HEAD>
    <body>
    <script language="javas cript">
    xmlDoc=new ActiveXObject(" Microsoft.XMLDO M");
    xmlDoc.async=fa lse;
    xmlDoc.load("bo oks.xml");
    var x=xmlDoc.getEle mentsByTagName( 'book');
    var newel,newtext;
    for (i=0;i<x.length ;i++)
    {
    newel=xmlDoc.cr eateElement('ed ition');
    newtext=xmlDoc. createTextNode( 'First');
    newel.appendChi ld(newtext);
    x[i].appendChild(ne wel);
    }
    var y=xmlDoc.getEle mentsByTagName( "edition")[0].childNodes[0];
    document.write( y.nodeValue);
    </script>
    </body>
    </HTML>



    xml file: name it as books.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <bookstore>
    <book category="COOKI NG">
    <title lang="en">Every day Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
    </book>
    <book category="CHILD REN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
    </book>
    <book category="WEB">
    <title lang="en">XQuer y Kick Start</title>
    <author>James McGovern</author>
    <author>Per Bothner</author>
    <author>Kurt Cagle</author>
    <author>James Linn</author>
    <author>Vaidyan athan Nagarajan</author>
    <year>2003</year>
    <price>49.99</price>
    </book>
    <book category="WEB">
    <title lang="en">Learn ing XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
    </book>
    </bookstore>

    The Result is "First", thats good now go and check ur books.xml file and the new data is not there in.

    Thanks.
    Nick
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    You're misinterpreting the intentions of this script.
    The intention is to grab data dynamically from an xml file which may be changing, and display the most recent information. This is not a script to update a file.

    Further, you can't really save to disk with javascript most of the time, due to security permissions.

    If you want the result, you need a browser which will allow you to save the processed result.

    You need to find another way to do this. I suggest PHP.

    Comment

    • NawazAhmed
      New Member
      • Feb 2008
      • 36

      #3
      If this is not the code then can u send me the correct one please.
      Since I saw this example on w3schools website so I was trying to do this.

      Thanks.

      Originally posted by jkmyoung
      You're misinterpreting the intentions of this script.
      The intention is to grab data dynamically from an xml file which may be changing, and display the most recent information. This is not a script to update a file.

      Further, you can't really save to disk with javascript most of the time, due to security permissions.

      If you want the result, you need a browser which will allow you to save the processed result.

      You need to find another way to do this. I suggest PHP.

      Comment

      • crackeur
        New Member
        • Mar 2008
        • 2

        #4
        The best way to update XML documents is using VTD-XML
        http://vtd-xml.sf.net

        See this article: http://www.javaworld.c om/javaworld/jw-07-2006/jw-0724-vtdxml.html


        Originally posted by NawazAhmed
        Hi,
        I was trying to insert new node and data into the xml file.
        Let say If I am inserting the data from page1 at line 4 and displaying the inserted data at line 5, its displaying it perfectly but when I go to the xml file and look for the inserted data its not there. I am I doing any mistake in the code below????
        This is the example from w3schools.com
        this goes into ur source
        <HTML>
        <HEAD>
        </HEAD>
        <body>
        <script language="javas cript">
        xmlDoc=new ActiveXObject(" Microsoft.XMLDO M");
        xmlDoc.async=fa lse;
        xmlDoc.load("bo oks.xml");
        var x=xmlDoc.getEle mentsByTagName( 'book');
        var newel,newtext;
        for (i=0;i<x.length ;i++)
        {
        newel=xmlDoc.cr eateElement('ed ition');
        newtext=xmlDoc. createTextNode( 'First');
        newel.appendChi ld(newtext);
        x[i].appendChild(ne wel);
        }
        var y=xmlDoc.getEle mentsByTagName( "edition")[0].childNodes[0];
        document.write( y.nodeValue);
        </script>
        </body>
        </HTML>



        xml file: name it as books.xml

        <?xml version="1.0" encoding="utf-8" ?>
        <bookstore>
        <book category="COOKI NG">
        <title lang="en">Every day Italian</title>
        <author>Giada De Laurentiis</author>
        <year>2005</year>
        <price>30.00</price>
        </book>
        <book category="CHILD REN">
        <title lang="en">Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
        </book>
        <book category="WEB">
        <title lang="en">XQuer y Kick Start</title>
        <author>James McGovern</author>
        <author>Per Bothner</author>
        <author>Kurt Cagle</author>
        <author>James Linn</author>
        <author>Vaidyan athan Nagarajan</author>
        <year>2003</year>
        <price>49.99</price>
        </book>
        <book category="WEB">
        <title lang="en">Learn ing XML</title>
        <author>Erik T. Ray</author>
        <year>2003</year>
        <price>39.95</price>
        </book>
        </bookstore>

        The Result is "First", thats good now go and check ur books.xml file and the new data is not there in.

        Thanks.
        Nick

        Comment

        Working...