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>
Sunil kumar sahoo
source share