如何在Android中附加XML中的标签?

时间:2011-06-15 09:44:16

标签: android xml

我想将一些内容写入XML文件。为此我创建了一个XML文件和带有元素,属性和值的写标签,其中包含以下数据:

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

如果我想添加一个包含新元素,新属性等的新标记,我会在之前用标记修改的标记处输入元素。

如何将新标记附加到先前附加的标记?

4 个答案:

答案 0 :(得分:17)

您可以在调用serializer.startDocument(之后和调用serializer.endDocument()之前创建任意数量的代码。一旦调用了endDocument,你的xml就完成了。如果你已经在文件中编写了这个xml,现在再次为xml创建编写相同的代码,并且在任何类型的值中都有更改,那么新的xml将覆盖以前的xml文件。因此,您将获得具有新插入标记的xml文件。如果你想在之前存在的xml文件中添加一些新标签,那么首先解析该xml文件获取所有内容并创建另一个xml文件,该文件首先从先前的xml获取数据并处理它,然后添加新插入的数据,以便新创建的xml将包含所有内容(包括以前的数据和新数据)

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);
    } 
}

输出

<?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>

答案 1 :(得分:2)

我真的没有看到你的观点,但对于我自己,我已经使用过这个例子而且效果很好

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);
    } 
}

您可以阅读完整的文章here

答案 2 :(得分:1)

看看this link。它应该让您了解如何向XML添加节点。这是一个片段。

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);
        }
    }

答案 3 :(得分:0)

请检查此链接我使用此&amp;它运作良好&amp;它也会解决你的问题。 http://www.anddev.org/write_a_simple_xml_file_in_the_sd_card_using_xmlserializer-t8350.html。 是的,您也可以添加新标签。