How to append tags in XML in Android?

匿名 (未验证) 提交于 2019-12-03 02:05:01

问题:

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?

回答1:

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


回答2:

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

1 title2 text1 title2 text


回答3:

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

private String writeXml(List 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



回答4:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!