How to add tags to XML on Android?

I would like to write some content in an XML file. To do this, I created an XML file and wrote down tags with elements, attributes and values ​​with the following data:

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 will introduce the element in the place of the tag, which it modifies with the previous tag.

How to add a new tag to previously added tags?

+7
source share
4 answers

Check out this link . This 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 the xml:\n\n" + xmlString); } catch (Exception e) { System.out.println(e); } } 
+1
source

You can create as many tags as you like after calling serializer.startDocument( ) and before calling serializer.endDocument() . As soon as you call endDocument, your xml is complete. if you wrote this xml in a file and now again write the same code to create xml with changing any type of value, then this new xml will override this previous xml file. so you get an xml file containing newly inserted tags. if you want to add some new tags to the previously submitted xml file, first analyze that the xml file will receive all the content and create another XML file that will first receive data from the previous xml and process it, and then add your newly inserted data to your the newly created xml will have everything (both previous data and 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); } } 

Exit

 <?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> 
+17
source

I really don’t see your point, but for myself I 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.

+2
source

Please check this link. I used it and it worked perfectly and 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 also add a new tag.

0
source

All Articles