Java sax, DOM, pull parsing xml

Source: Internet
Author: User
Tags tagname xml parser

-------------------------------------Sax Parsing XML----------------------------------

"Sax definition

Sax is a fast parsing and memory-intensive XML parser, ideal for mobile devices such as Android

The Sax full name is simple API for Xml, which refers to both an interface and a package

As an interface, sax is a standard interface for event-driven XML parsing

"Sax Features

1. High resolution efficiency, low memory consumption

2. Can stop parsing at any time

3. Cannot load entire document into memory

4. Cannot write to XML

5.SAX parsing XML file is event-driven

---sax does not need to parse the entire document, Sax determines whether the currently read character is valid in a part of the XML syntax while parsing the document in the content order, triggering an event if it matches

"How Sax Works

Sax works simply by sequentially scanning the document, scanning to the beginning and end of the document, and invoking event handling when scanning to the beginning and end of elements (element)

The handler does the corresponding action, and then continues the scan until the document finishes.

"Sax Parsing Document procedure

1. Inherit the DefaultHandler and implement the method

2. Create a SAX parser factory

3. Get the parser

4. Get the input stream

5. Use the input stream, and implement the interface as parameters, call the parser parsing method to parse

The DefaultHandler interface is the implementation of the ContentHandler interface

Common methods in the ContentHandler interface

>startdocument ()

When you encounter the beginning of a document, call this method, where you can do some work with the preparation

>enddocument ()

When the document is finished, call this method, where you can do some cleanup work

>startelement (String Namespaceurl, String LocalName, String qName, Attributes atts)

This method is triggered when a start tag is read. Namespaceurl is a namespace, LocalName is a label name without a namespace prefix,

QName is the label name for the namespace prefix. All property names and corresponding values can be obtained by Atts.

>endelement (String uri, String localname, string name)

This method is called when the end tag is encountered

>characters (char[] ch, int start, int length)

This method is used to process the content read in the XML file, the first parameter is the string content of the file, and the next two parameters are the starting position and length of the string to be read in the array,

Use new String (CH, start, length) to get the content

"Sax Parsing example

-------------1. Create an XML file under SRC and combine it into the entity class Userinfo.java-------------

<?xml version= "1.0" encoding= "UTF-8"?>
<admins>
<admin id= "1" >
<name> Aaron </name>
<age>23</age>
<sex> Men </sex>
<email>[email protected]</email>
</admin>
</admins>

----------Userinfo.java----

String name;
int age;
String sex;
String email;
String ID;

。。。 Generate a Get, set method

--------------------2. Create Xmlpaser inheritance DefaultHandler----------------------

public class Xmlpaser extends defaulthandler{

Create a user object to put the found content inside

Userinfo user;
Public Userinfo GetUser () {
return user;
}
public void SetUser (Userinfo user) {
This.user = user;
}

Defining tag Variables

String tagName = null;

Start the document processing some preparation

public void Startdocument () throws Saxexception {
user = new Userinfo ();
Super.startdocument ();
}

Read the first tag trigger

public void Startelement (string uri, String localname, String qName,
Attributes Attributes) throws Saxexception {
TagName = LocalName;
if ("admin". Equals (TagName)) {
User.setid (attributes.getvalue (0));
SYSTEM.OUT.PRINTLN ("ID:" +attributes.getvalue (0));
}
}

Reading text content

public void characters (char[] ch, int start, int length)
Throws Saxexception {
if (tagname!=null) {
if ("Name". Equals (TagName)) {
String str = new string (ch,start,length);
User.setname (str);
System.out.println ("Name:" +str);
}else if ("Age". Equals (TagName)) {
String str = new string (ch,start,length);
User.setage (Integer.parseint (str));
System.out.println ("Age:" +str);
}else if ("Sex". Equals (TagName)) {
String str = new string (ch,start,length);
User.setsex (str);
System.out.println ("Sex:" +str);
}else if ("email". Equals (TagName)) {
String str = new string (ch,start,length);
User.setemail (str);
System.out.println ("email:" +str);
}
}
}

End Tag encountered

public void EndElement (string uri, String localname, String qName)
Throws Saxexception {
Tagname=null;

}

Document read out

public void Enddocument () throws Saxexception {
Super.enddocument ();
}

}

----------------layout file main omitted-----------

----------------------3.-class saxactivity inherit activity----------------

Button Btnok;

public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

Btnok = (Button) This.findviewbyid (R.id.button1);
Btnok.setonclicklistener (New Onclicklistener () {

public void OnClick (View v) {

Creating a parse factory and parser

SAXParserFactory SPF = saxparserfactory.newinstance ();

try{

SAXParser sp = Spf.newsaxparser ();

Analytical

Xmlpaser XP = new Xmlpaser ();

InputStream is = This.getclass (). getClassLoader (). getResourceAsStream ("User.xml");

Sp.parse (IS,XP);

Get the Read content

Userinfo user = Xp.getuser ();

On the page display

.................

}

}

});

}

---------------------------------------DOM Parsing XML--------------------------------------------

"Introduction to DOM

Dom Full name document Object Model, which defines a set of interfaces for the parsed version of an XML document. The parser reads the entire document, then constructs a tree structure of the dominant memory,

The code can then use the DOM interface to manipulate the tree structure.

The features of the DOM

> Advantages

1. The entire document tree is in memory, easy to operate, and supports deletion, modification, rearrangement and other functions

2. Accessing XML documents through a tree structure

3. You can move forward or backward on a node of a tree

> Disadvantages

1. Transfer the entire document into memory (including useless nodes), wasting time and space

> Applicable occasions

Once the document has been parsed, the data needs to be accessed multiple times, with sufficient hardware resources (memory, CPU)

"Dom Parsing steps

1. Create a parser factory

2. Get the parser factory

3. Accept an XML document as the input parameter name and get an XML Document object

4. Manipulating Document objects

"Analysis Example:

-----------------------1. Create the XML file to parse, and entity class (person)---------------------------

<?xml version= "1.0" encoding= "Utf-8"?>
<users>
<user id= "1" >
<name>Tom</name>
<age>19</age>
</user>
<user id= "2" >
<name>Jaary</name>
<age>18</age>
</user>
</users>

Entity class slightly

-----------------------2. Dom Parsing (Domservice.java)-----------------------------------

Public list<person> getpersons (InputStream input) throws throwable{

Get the parser factory

Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();

Get parser

Documentbuilder builder = Factory.newdocumentbuilder ();

To parse

Document doc = builder.parse (input);

list<person> personlist = new New arraylist<person> ();

Get all the nodes called user

NodeList list = doc.getelementsbytagname (user);

String id= "";
String name= "";
String age= "";
for (int i=0;i<list.getlength (); i++) {
Get the first user node
Element node= (Element) List.item (i);
Person P=new person ();
Get ID Property
Id=node.getattribute ("id");
Get a list of child nodes under the user node
NodeList userlist=node.getchildnodes ();
for (int j=0;j<userlist.getlength (); j + +) {
Determines whether the element node, name, and age belong to the element node
if (Userlist.item (j). Getnodetype () ==node.element_node) {

Element childnode= (Element) Userlist.item (j);
if ("Name". Equals (Childnode.getnodename ())) {
Name=childnode.getfirstchild (). Getnodevalue ();
}
else if ("Age". Equals (Childnode.getnodename ())) {
Age=childnode.getfirstchild (). Getnodevalue ();
}
P.setname (name);
P.setage (age);
P.setid (Integer.parseint (id));
}

}

Personlist.add (P);
}
return personlist;

}

-------------------------3.activity01 inherit the activity class----------------------------------------------------

public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

InputStream Is=this.getclass (). getClassLoader (). getResourceAsStream ("Test.xml");
Domservice dom=new Domservice ();

try {
List<person> ps=dom.getpersons (IS);
for (person P:ps) {
System.out.println (P.getid ());
System.out.println (P.getname ());
System.out.println (P.getage ());
}
} catch (Exception e) {
E.printstacktrace ();
}
}

--------------------------------------Pull Parsing-----------------------------

"Pull Parser Introduction

1.pull parser is a built-in parser for Android, similar to the parsing principle of sax

2.pull it provides a similar event.

For example: start element and end element event, use Parse.next () to enter the next element and trigger the corresponding event, the event will be sent as a numeric code

You can therefore use a switch to handle events of interest. When the element begins parsing, call the Parser.nexttext () method to get the value of the next text type node

The difference between pull and sax

1.pull reads an XML file and triggers the corresponding event call method to return a number.

2.pull can be controlled in the program, you want to resolve where you can stop where

Pull parsing is more recommended in 3.Android

"Pull resolution Step

1. Creating a Parser Object

Xmlpullparser Paser = Xml.newpullparser ();

2. Parsing

Paser.setinput (Input, "utf-8");

3. Generate the first parse event

int eventtype = Paser.geteventtype ();

4. You can use the loop to determine whether to continue parsing

while (eventtype!=xmlpullparser.end_document) {}

"Analytic example

-------------------1. Create XML. And instance object (Person.java)----------------slightly

--------------------2.pull Parsing (pullservice. Java)----------------------

List<person> Getalperson (InputStream is) throws Xmlpullparserexception, ioexception{

List<person> Persons=null;
Person P=null;
Xmlpullparser Parser=xml.newpullparser ();
Parser.setinput (IS, "utf-8");
Get Event Type
int Type=parser.geteventtype ();
while (type!=xmlpullparser.end_document) {

Switch (type) {

Case xmlpullparser.start_document://Document Start
Persons=new arraylist<person> ();
Break
Case xmlpullparser.start_tag://Element start
if (Parser.getname (). Equals ("user")) {

P=new person ();
String id=parser.getattributevalue (0);
P.setid (Integer.parseint (id));
}
else if (Parser.getname (). Equals ("name")) {
if (p!=null) {

String Name=parser.nexttext ();
P.setname (name);
}
}
else if (Parser.getname (). Equals ("Age")) {

String Age=parser.nexttext ();
P.setage (age);
}
Break
Case Xmlpullparser.end_tag:
if ("User". Equals (Parser.getname ())) {

Persons.add (P);
P=null;
}
Break

}
Type=parser.next ();
}
return persons;
}
}

------------------------------------Activity Class-----------------------------------

public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

InputStream Is=this.getclass (). getClassLoader (). getResourceAsStream ("Test.xml");
Pullservice pps=new Pullservice ();

try {
List<person> ps=pps.getpersons (IS);
for (person P:ps) {
System.out.println (P.getid ());
System.out.println (P.getname ());
System.out.println (P.getage ());
}
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (Xmlpullparserexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

Comparison of SAX, DOM, pull

SAX, DOM, pull each have their own characteristics, the specific operation of XML when the choice?

1. Memory consumption

This is a fundamental issue. Because Android phone performance relative to the current application operation is still limited, the memory consumption of the program directly affects the speed of parsing XML. At this point, SAX and pull are more suitable for Android phone development than they use to parse less memory than DOM.

2. Programming method

Sax uses event-driven, which invokes the user-written method when the corresponding event is triggered. That is, each parsing of a class of XML requires a new processing class that is appropriate for that class of XML. This is obviously not a good solution, although it is so good at parsing speed. And this is because DOM is the norm. So it is known and used by more programmers. So in the development process, there is not much difficulty. Although pull belongs to a small, even unknown parser, but through the above introduction and examples, we should be able to see its simplicity.

3. Access and modification

Because of the flow parsing, this means that they cannot be accessed randomly, like DOM, in any of the nodes of the XML. Also, Sax does not provide an API for adding nodes to the document, and there is no way to delete and modify the contents of the document.

4. Access mode

This is the root cause of the speed of their resolution. If sax and pull are likened to Yimushihang, but are quick to read, then the DOM is read verbatim, slowly, but with a photographic memory. It is also important to note that the Sax,pull parsing method is synchronous, that is, where the parser reads, where it is processed. The DOM is the information that the user is interested in extracting the XML after parsing it well.

Summarize:

For memory consumption, it is recommended to use Sax or pull to work. But based on how they work: if you just need information about the last few nodes of the XML, or if you have repeatedly retrieved an XML file. Then basically there is no difference in performance, but at this point, the Sax processing class will make the program appear more bloated than other implementations. So, want to do a high-performance Android software, or more analysis, choose the right tool, to play its role.

Java sax, DOM, pull parsing xml

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.