如何添加标签在XML中的Andr​​oid? [英] How to append tags in XML in Android?

查看:117
本文介绍了如何添加标签在XML中的Andr​​oid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一些内容到一个XML文件中。对于我已经创建了一个XML文件,并writen标签与元素,属性和价值这样一些数据:

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

如果我想添加一个新的标签有新的元素,新的属性,等我进入的元素标签的地方是修改与previous标签。

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.

我怎么能追加新的标记至previously附加标签?

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

推荐答案

看看此链接。它应该给你如何将节点添加到您的XML的想法。这里是一个片段。

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

这篇关于如何添加标签在XML中的Andr​​oid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆