Search code examples
javascriptjqueryajaxcordovaapache-cordova

how to read & write external xml file in apache cordova?


I am creating android app. I want to use xml file just to store a little amount of data for short period of time.. I am able to read xml using ajax.get() method file but not able to write xml.. Please help me to do so..


Solution

  • If you want to store a little amount of data and you are using cordova, you could use get and set local storage:

    localStorage.setItem("lastname", "Smith");
    

    and

    var agentSmith=localStorage.getItem("lastname");
    

    as w3school says:

    With local storage, web applications can store data locally within the user's browser.

    but if you want to use xml, you could do:

    var v = new  XMLWriter();
       v.writeStartDocument(true);
       v.writeElementString('test','Hello ');
       v.writeAttributeString('foo','bar');
       v.writeEndDocument();
       console.log( v.flush() );
    

    and obtain:

    <?xml version="1.0" encoding="ISO-8859-1" standalone="true" ?>
    <test foo="bar">Hello World</test>
    

    like here create and modify xml file using javascript