locked
Inserting data in XML using JavaScipt

    Question

  • Hi,

    I hava an xml file stored locally on my machine.

    Structure of my xml doc - 

    <root>

    <item>

    <name>item1</name>

    <price>123</price>

    </item>

    <item>

    <name>item2</name>

    <price>145</price>

    </item>

    </root>

     My requirement is to update the same by adding new data to the file(add a new item)

    I have tried using the following way. but nothing happens

    ------------------------------------------------------------------------------------

    Windows.Storage.StorageFile.getFileFromPathAysnc("path").done(function(file){

    Windows.Data.Xml.Dom.XmlDocument.loadFromFileAsync(file).done(function(xml){

    //code to update the file and save it back again

    });

    });

    * Request you to please give the code for the same

    * Any other better way of doing this is also welcome.

    * Only javascript codes please.

    Thanks in advance

    • Edited by Manvik07 Tuesday, July 10, 2012 5:58 AM
    Tuesday, July 10, 2012 5:57 AM

Answers

  • Hi,

    From my experience, DOM provides APIs to manipulate XML. Below is a simple sample:

    var xdoc = document.implementation.createDocument("", "Story", null);
    var photoElement = xdoc.createElement("Photo");
    photoElement.setAttribute("Name", photo.name);
    photoElement.setAttribute("Order", photo.order);

    Best Regards,

    Ming Xu.


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework

    Wednesday, July 11, 2012 11:17 AM
    Moderator

All replies

  • Hi,

    From my experience, DOM provides APIs to manipulate XML. Below is a simple sample:

    var xdoc = document.implementation.createDocument("", "Story", null);
    var photoElement = xdoc.createElement("Photo");
    photoElement.setAttribute("Name", photo.name);
    photoElement.setAttribute("Order", photo.order);

    Best Regards,

    Ming Xu.


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework

    Wednesday, July 11, 2012 11:17 AM
    Moderator
  • Hi,

    I will mark the reply as an answer. If you find it no help,  please feel free to unmark it and follow up.

    Thanks.

    Best Regards,

    Ming Xu.


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework

    Tuesday, July 17, 2012 3:55 AM
    Moderator