Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append tags in XML in Android?

Tags:

android

xml

I would like to write some content to a XML file. For that I have created a XML file and writen tags with element, attribute and value with some data like this:

XmlSerializer serializer = Xml.newSerializer();
serializer.startTag(null, element);
serializer.attribute(null, atbname, value);
serializer.text(text);
serializer.endTag(null, tag);

If I want to add a new tag with new elements, new attributes, etc. and I enter the element at the place of tag it is modifying with previous the tag.

How can I append the new tag to the previously appended tags?

like image 374
prasad.gai Avatar asked Jun 15 '11 09:06

prasad.gai


3 Answers

You can create as many tags as you want after calling serializer.startDocument() and before calling serializer.endDocument(). Once you have calling endDocument your xml is complete. if you have written this xml in a file and now again writing the same code for xml creation with change in any type of value then that new xml will override this previous xml file. so you will get the xml file having the newly inserted tags. if you want to add some new tags to the previously present xml file then first parsse that xml file get all the contents and create another xml file which wil first get data from previous xml and process it and then add your newly inserted data so that your newly created xml will have everything (both previous data as well as new data)

private String writeXml(){
    XmlSerializer serializer = Xml.newSerializer();
    StringWriter writer = new StringWriter();
    try {
        serializer.setOutput(writer);
        serializer.startDocument("UTF-8", true);
        serializer.startTag("", "messages");
        serializer.attribute("", "number", "value of attribute");

            serializer.startTag("", "title");
            serializer.text(1+" title");
            serializer.endTag("", "title");
            serializer.startTag("", "title");
            serializer.text(2+" text");
            serializer.endTag("", "title");


        serializer.endTag("", "messages");
        serializer.startTag("", "messages1");
        serializer.attribute("", "number", "value of attribute");

            serializer.startTag("", "title");
            serializer.text(1+" title");
            serializer.endTag("", "title");
            serializer.startTag("", "title");
            serializer.text(2+" text");
            serializer.endTag("", "title");


        serializer.endTag("", "messages1");
        serializer.endDocument();
        return writer.toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    } 
}

output

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<messages number="value of attribute">
<title>1 title</title>
<title>2 text</title>
</messages>
<messages1 number="value of attribute">
<title>1 title</title>
<title>2 text</title>
</messages1>
like image 145
Sunil Kumar Sahoo Avatar answered Sep 19 '22 13:09

Sunil Kumar Sahoo


I don't really see your point but for myself i've used this example and it worked just fine

private String writeXml(List<Message> messages){
    XmlSerializer serializer = Xml.newSerializer();
    StringWriter writer = new StringWriter();
    try {
        serializer.setOutput(writer);
        serializer.startDocument("UTF-8", true);
        serializer.startTag("", "messages");
        serializer.attribute("", "number", String.valueOf(messages.size()));
        for (Message msg: messages){
            serializer.startTag("", "message");
            serializer.attribute("", "date", msg.getDate());
            serializer.startTag("", "title");
            serializer.text(msg.getTitle());
            serializer.endTag("", "title");
            serializer.startTag("", "url");
            serializer.text(msg.getLink().toExternalForm());
            serializer.endTag("", "url");
            serializer.startTag("", "body");
            serializer.text(msg.getDescription());
            serializer.endTag("", "body");
            serializer.endTag("", "message");
        }
        serializer.endTag("", "messages");
        serializer.endDocument();
        return writer.toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    } 
}

You can read the full article here

like image 37
iDroid Avatar answered Sep 19 '22 13:09

iDroid


Take a look at this link. It should give you an idea of how to add nodes to your XML. Here is a snippet.

public DomXmlExample() {
        try {
            /////////////////////////////
            //Creating an empty XML Document

            //We need a Document
            DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
            Document doc = docBuilder.newDocument();

            ////////////////////////
            //Creating the XML tree

            //create the root element and add it to the document
            Element root = doc.createElement("root");
            doc.appendChild(root);

            //create a comment and put it in the root element
            Comment comment = doc.createComment("Just a thought");
            root.appendChild(comment);

            //create child element, add an attribute, and add to root
            Element child = doc.createElement("child");
            child.setAttribute("name", "value");
            root.appendChild(child);

            //add a text element to the child
            Text text = doc.createTextNode("Filler, ... I could have had a foo!");
            child.appendChild(text);

            /////////////////
            //Output the XML

            //set up a transformer
            TransformerFactory transfac = TransformerFactory.newInstance();
            Transformer trans = transfac.newTransformer();
            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            trans.setOutputProperty(OutputKeys.INDENT, "yes");

            //create string from xml tree
            StringWriter sw = new StringWriter();
            StreamResult result = new StreamResult(sw);
            DOMSource source = new DOMSource(doc);
            trans.transform(source, result);
            String xmlString = sw.toString();

            //print xml
            System.out.println("Here's the xml:\n\n" + xmlString);

        } catch (Exception e) {
            System.out.println(e);
        }
    }
like image 23
Jake Sankey Avatar answered Sep 19 '22 13:09

Jake Sankey