1.sax mode
Copy Code code as follows:
/**
* Using SAX parsing
*/
public class saxparse{
/**
* SAX Parser
*/
Private SAXParser parser;
Public Saxparse () {
try {
SAXParserFactory f = saxparserfactory.newinstance ();
Parser = F.newsaxparser ();
catch (Parserconfigurationexception e) {
E.printstacktrace ();
catch (Exception e) {
E.printstacktrace ();
}
}
Public list<person> Doparse (InputStream is) {
try {
Xmlhandler h = new Xmlhandler ();
Parser.parse (IS,H);
return H.getpersons ();
catch (Exception e) {
E.printstacktrace ();
}
return null;
}
/**
* Processor
*/
Class Xmlhandler extends defaulthandler{
list<person> persons = NULL;
person person = null;
Current element name
Private String Currelename;
/**
* Text node triggers the method
*/
public void characters (char[] ch, int start, int length) throws Saxexception {
String str = new string (ch,start,length);
Name
if ("Name". Equals (Currelename)) {
Person.name = str;
}
else if ("Age". Equals (Currelename)) {
Person.age = Integer.parseint (str);
}
}
public void Enddocument () throws Saxexception {
}
/**
* Element End
*/
public void EndElement (string uri, String localname, String qName)
Throws Saxexception {
if ("Person". Equals (LocalName)) {
Persons.add (person);
}
Empty the current element
else if (("Name". Equals (currelename)) | | ("Age". Equals (Currelename)) {
This.currelename = "";
}
}
/**
* Document Start Event
*/
public void Startdocument () throws Saxexception {
persons = new arraylist<person> ();
}
/**
* Element Start Event
* LocalName: This place name
* URI: Name space
* QName: Qualified name, prefix + local name
*/
public void Startelement (string uri, String localname, String qName,
Attributes Attributes) throws Saxexception {
Instantiating a Person object
if ("Person". Equals (LocalName)) {
person = new person ();
Person.id = Integer.parseint (attributes.getvalue (0));
}
Name element
else if ("name". Equals (LocalName)) {
This.currelename = "name";
}
Name element
else if ("Age". Equals (LocalName)) {
This.currelename = "Age";
}
}
Public list<person> getpersons () {
return persons;
}
}
}
2.dom mode
Copy Code code as follows:
/**
* DOM parsing
*/
public class domparse{
//
Private Documentbuilder Builder;
Public Domparse () {
try {
Documentbuilderfactory f = documentbuilderfactory.newinstance ();
This.builder = F.newdocumentbuilder ();
catch (Exception e) {
E.printstacktrace ();
}
}
Public list<person> Doparse (InputStream is) {
list<person> persons = new arraylist<person> ();
person person = null;
try {
Document doc = Builder.parse (IS);
NodeList list = Doc.getelementsbytagname ("person");
Element ele = null;
for (int i = 0; i < list.getlength (); i + +) {
Ele = (Element) list.item (i);
person = new person ();
Person.id = Integer.parseint (Ele.getattribute ("id"));
Person.name = Getsubelementtextcontent (ele, "name");
Person.age = Integer.parseint (Getsubelementtextcontent (Ele, "age"));
Persons.add (person);
}
catch (Exception e) {
E.printstacktrace ();
}
return persons;
}
/**
* Get the text content in the middle of the specified resource
*/
Private String getsubelementtextcontent (Element ele, string tagName) {
NodeList list = Ele.getelementsbytagname (tagName);
Element e = (element) list.item (0);
Get the middle text node
return E.gettextcontent ();
}
}
3.pull mode
Copy Code code as follows:
/**
* Pull parsing, pull mode, you can manually control whether the next event triggers.
*/
public class pullparse{
Public list<person> Doparse (InputStream is) {
list<person> persons = NULL;
person person = null;
try {
Xmlpullparser parser = Xml.newpullparser ();
Setting up a resolution data source
Parser.setinput (IS, "utf-8");
Get the type of event
int eventtype = Parser.geteventtype ();
String elename = null;
while (EventType!= xmlpullparser.end_document) {
Switch (eventtype) {
Document Start
Case Xmlpullparser.start_document:
persons = new arraylist<person> ();
break;
Element start
Case Xmlpullparser.start_tag:
Elename = Parser.getname ();
if ("Person". Equals (Elename)) {
person = new person ();
Person.id = Integer.parseint (parser.getattributevalue (0));
}
else if ("name". Equals (Elename)) {
Person.name = Parser.nexttext ();
}
else if ("Age". Equals (Elename)) {
Person.age = Integer.parseint (Parser.nexttext ());
}
break;
Tag End
Case Xmlpullparser.end_tag:
Elename = Parser.getname ();
if ("Person". Equals (Elename)) {
Persons.add (person);
}
break;
}
Manually activate the trigger for the next event
EventType = Parser.next ();
}
catch (Exception e) {
E.printstacktrace ();
}
return persons;
}
}