To create a new XML file:
<?xml version= "1.0" encoding= "UTF-8"?>
<contacts>
<contact id= "1" >
<name>zhangsan</name>
<image src= "Http://192.168.1.103:8080/mymyweb/images/1.gif"/>
</contact>
<contact id= "2" >
<name>zhangsan</name>
<image src= "Http://192.168.1.103:8080/mymyweb/images/1.gif"/>
</contact>
</contacts>
Android version Dom parsing:
private void Createxmlfile () {
File Lincesefile=new file (Books_path);
try {
Lincesefile.createnewfile ();
} catch (Exception e) {
LOG.E ("IOException", "Exception in CreateNewFile () method");
}
FileOutputStream Fileos=null;
try {
Fileos=new FileOutputStream (Lincesefile);
} catch (Exception e) {
LOG.E ("FileNotFoundException", "Can ' t create FileOutputStream");
}
XmlSerializer Serializer=xml.newserializer ();
try {
Serializer.setoutput (Fileos, "UTF-8");
Serializer.startdocument (null,true);
Serializer.starttag (NULL, "books");
for (int i=0;i<3;i++) {
Serializer.starttag (NULL, "book");
Serializer.starttag (NULL, "BookName");
Serializer.text ("Android Tutorial" +i);
Serializer.endtag (NULL, "BookName");
Serializer.starttag (NULL, "Bookauthor");
Serializer.text ("Frankie" +i);
Serializer.endtag (NULL, "Bookauthor");
Serializer.endtag (NULL, "book");
}
Serializer.endtag (NULL, "books");
Serializer.enddocument ();
Serializer.flush ();
Fileos.close ();
} catch (Exception e) {
Todo:handle exception
}
Toast.maketext (Getapplicationcontext (), "Create XML file successfully!" ", Toast.length_long). Show ();
}
private void Saxparserxml () {
}
private void Domparserxml () {
File File=new file (Books_path);
Documentbuilderfactory dbf=documentbuilderfactory.newinstance ();
Documentbuilder Db=null;
try {
Db=dbf.newdocumentbuilder ();
} catch (Exception e) {
E.printstacktrace ();
}
Document Doc=null;
try {
Doc=db.parse (file);
} catch (Exception e) {
E.printstacktrace ();
}
Element root=doc.getdocumentelement ();
NodeList books=root.getelementsbytagname ("book");
String res= "This result is parsed by DOM:/n";
for (int i=0;i<books.getlength (); i++) {
Element book= (Element) Books.item (i);
Element bookname= (Element) book.getelementsbytagname ("BookName"). Item (0);
Element bookauthor= (Element) book.getelementsbytagname ("Bookauthor"). Item (0);
res+= "title:" +bookname.getfirstchild (). Getnodevalue () + "+" Author "+bookauthor.getfirstchild (). Getnodevalue () +"/n ";
}
Tv.settext (RES);
Note the setting of permissions:
<uses-permission android:name= "Android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
Android version Pullparser parsing:
try {
Xmlpullparserfactory factory=xmlpullparserfactory.newinstance ();
Factory.setnamespaceaware (TRUE);
Xmlpullparser Xpp=factory.newpullparser ();
Xpp.setinput (New StringReader ("<contacts><contact id= ' 1 ' > <name>zhangsan</name> <image Src= ' http://192.168.1.103:8080/mymyweb/images/1.gif '/></contact></contacts> "));
int Eventtype=xpp.geteventtype ();
while (eventtype!=xmlpullparser.end_document) {
Switch (eventtype) {
Case Xmlpullparser.start_document:
SYSTEM.OUT.PRINTLN ("Start document");
Break
Case Xmlpullparser.start_tag:
System.out.println ("Start tag" +xpp.getname ());
if (Xpp.getname (). Equals ("name")) {
Toast.maketext (Getapplicationcontext (), Xpp.nexttext (), Toast.length_short). Show ();
}else if (Xpp.getname (). Equals ("image")) {
Toast.maketext (Getapplicationcontext (), Xpp.getattributevalue (0), Toast.length_short). Show ();
}
Break
Case Xmlpullparser.text:
Break
Case Xmlpullparser.end_document:
Break
Default:
Break
}
Eventtype=xpp.next ();
}
The Dom,xmlpullparser parsing XML in Java,