1 PackageXmlpulldemo;2 3 Importjava.io.FileNotFoundException;4 ImportJava.io.FileOutputStream;5 Importjava.io.IOException;6 7 Importorg.xmlpull.v1.XmlPullParserException;8 Importorg.xmlpull.v1.XmlPullParserFactory;9 ImportOrg.xmlpull.v1.XmlSerializer;Ten One Public classpullproducedocument { A - Public Static voidMain (string[] args)throwsxmlpullparserexception, IllegalArgumentException, - illegalstateexception, FileNotFoundException, IOException { the - //Parser Factory -Xmlpullparserfactory factory =xmlpullparserfactory.newinstance (); - //Serializer +XmlSerializer serializer =Factory.newserializer (); - //set the output location of an XML file +Serializer.setoutput (NewFileOutputStream ("Src/books.xml"), "UTF-8"); A //invoke the relevant method of the serializer to write data to the XML file atSerializer.startdocument ("UTF-8",true); -Serializer.starttag (NULL, "Books"); - for(inti = 0; I < 5; i++) { -Serializer.starttag (NULL, "book"); -Serializer.attribute (NULL, "id", "Book1"); -Serializer.starttag (NULL, "name"); inSerializer.text ("Kingdoms"); -Serializer.endtag (NULL, "name"); toSerializer.starttag (NULL, "Author"); +Serializer.text ("Luo Guan Zhong"); -Serializer.endtag (NULL, "Author"); theSerializer.starttag (NULL, "Price"); *Serializer.text ("30$"); $Serializer.endtag (NULL, "Price");Panax NotoginsengSerializer.endtag (NULL, "book"); - } theSerializer.endtag (NULL, "Books"); + serializer.enddocument (); A the } + -}
Pull generate XML file