Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to append tags in xml in android? and how save that xml file?

Tags:

java

android

xml

i would like to append a new tag with some attributes with those values in to xml file and save that xml file through my application.i have written a method for append a new tag as child to xml file which is available in sdcard of android emulator.the following method for append a new tag as follows

   public void appendTag(){

    try{

        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder.parse (new File("/sdcard/sample.xml"));

     Node node = doc.getElementsByTagName("earth").item(0);

     //append a new node to earth
     Element newelmnt = doc.createElement("new");
     newelmnt.appendChild(doc.createTextNode("this is a text"));
     node.appendChild(newelmnt);


    }
    catch (Exception e) {
        e.printStackTrace();
    }

}

after execution of this method i can't able to find a new tag in xml file.

could please any one help on how to append new tag as child in xml file and how save the modification?

if i uses TransformerFactory i am getting error as ERROR/AndroidRuntime(13479): java.lang.VerifyError: com.sample.xmlapp.DOMClass i have used as follows

     TransformerFactory factory = TransformerFactory.newInstance();
     Transformer transformer = factory.newTransformer();

     DOMSource source = new DOMSource(doc);
     StreamResult result = new StreamResult(new File("/sdcard/sample.xml"));
     transformer.transform(source, result);
like image 457
prasad.gai Avatar asked Jul 07 '11 06:07

prasad.gai


People also ask

Can you create your own tags in XML?

XML tags are the important features of XML document. It is similar to HTML but XML is more flexible then HTML. It allows to create new tags (user defined tags). The first element of XML document is called root element.

How do I move XML from one file to another in Android?

[1] call startActivity to open another layout. startActivity(new Intent(getApplicationContext(), _second. class)); [2] Create another XML layout file which you want to display.

What is XML tag android?

XML tags define the data and used to store and organize data. It's easily scalable and simple to develop. In Android, the XML is used to implement UI-related data, and it's a lightweight markup language that doesn't make layout heavy. XML only contains tags, while implementing they need to be just invoked.


1 Answers

Your in memory document will be changed, but you will need to write it to a file again.

Try adding this for writing the document to the file again:

TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();

DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("/sdcard/sample.xml"));
transformer.transform(source, result);
like image 57
wjans Avatar answered Oct 14 '22 03:10

wjans