如何在 Android 中的 XML 中附加标签?
我想将一些内容写入 XML 文件。为此,我创建了一个 XML 文件,并用一些数据编写了包含元素、属性和值的标签,如下所示:
XmlSerializer serializer = Xml.newSerializer();
serializer.startTag(null, element);
serializer.attribute(null, atbname, value);
serializer.text(text);
serializer.endTag(null, tag);
如果我想添加包含新元素、新属性等的新标签,并且我在标签位置输入元素它正在修改以前的标签。
如何将新标签附加到之前附加的标签上?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在调用
serializer.startDocument(
) 之后和调用serializer.endDocument()
之前创建任意数量的标签。调用 endDocument 后,您的 xml 就完成了。如果您已将此 xml 写入文件中,现在再次编写相同的代码来创建 xml,并更改任何类型的值,则新的 xml 将覆盖先前的 xml 文件。这样您将获得包含新插入的标签的 xml 文件。如果您想向以前存在的 xml 文件添加一些新标签,那么首先解析该 xml 文件获取所有内容并创建另一个 xml 文件,该文件将首先从以前的 xml 获取数据并处理它,然后添加新插入的数据,以便您的新创建的 xml 将包含所有内容(包括以前的数据和新数据)输出
You can create as many tags as you want after calling
serializer.startDocument(
) and before callingserializer.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)output
我不太明白您的观点,但就我自己而言,我使用了这个示例,并且效果很好
您可以阅读全文 这里
I don't really see your point but for myself i've used this example and it worked just fine
You can read the full article here
请查看此链接。它应该让您了解如何向 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.
请检查这个链接我用过这个&它工作得很好&它也会解决你的问题。
http://www.anddev.org/write_a_simple_xml_file_in_the_sd_card_using_xmlserializer-t8350.html。
是的,您也可以添加新标签。
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.