How to append tags in XML in Android?

前端 未结 4 1297
抹茶落季
抹茶落季 2020-12-29 16:12

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:



        
相关标签:
4条回答
  • 2020-12-29 16:24

    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);
            }
        }
    
    0 讨论(0)
  • 2020-12-29 16:27

    Please check this link I used this & it worked fine & also it will solve your problem. http://www.anddev.org/write_a_simple_xml_file_in_the_sd_card_using_xmlserializer-t8350.html. Yes you can add new tag also.

    0 讨论(0)
  • 2020-12-29 16:29

    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

    0 讨论(0)
  • 2020-12-29 16:32

    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>
    
    0 讨论(0)
提交回复
热议问题