Parsing XML files using pull in Android

Source: Internet
Author: User

First of all, this article is not original. Recently, XML file parsing has been used. Previously, it was all about sax parsing. I went online and heard people say that parsing XML files using Android's built-in pull method should be stable and efficient. So I implemented it based on several examples on the Internet, and then I thought it was important. I will not elaborate on anything that was not important.

The first is the XML file. I will not be fancy. I will directly copy books. XML from the online example:

<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="12">
<name>thinking in java</name>
<price>85.5</price>
</book>
<book id="15">
<name>Spring in Action</name>
<price>39.0</price>
</book>
</books>

Then there is a class related to File Parsing. This class mainly uses the xmlpullparser class. In fact, what you actually call is the kxmlparse class. The interface uses the ordinary inputstream byte stream, because it is more common:

Public class pullparseservice {
Public list <book> parsedatesource (inputstream) throws exception {
List <book> books = NULL;
Book = NULL;
Xmlpullparser parse = xml. newpullparser ();
Parse. setinput (inputstream, "UTF-8 ");

Int event = parse. geteventtype (); // returns the type of the current event (start_tag, end_tag, text, etc .)

While (event! = Xmlpullparser. end_document ){
Switch (event ){
Case xmlpullparser. start_document:
Books = new arraylist <book> (); // initialize the books set
Break;
Case xmlpullparser. start_tag:
If (PARSE. getname (). Equals ("book ")){
Book = New Book ();
// Book. setid (integer. parseint (PARSE. getattributevalue (0 )));
// Or, this is fine.
Book. setid (integer. parseint (PARSE. getattributevalue (null, "ID ")));
}
If (book! = NULL ){
/**
* Note: If... else if... cannot be changed to if... If .... (The reason is that an exception occurs when you replace it with the latter, which may be related to the internal implementation of the getname () of xmlpullparser:
* The specific reason is that the kxmlparse nexttext () method itself will call the next () function to look at the source code, and I am not very good at it)
*/
If (PARSE. getname (). Equals ("name ")){
Book. setname (PARSE. nexttext ());
} Else if (PARSE. getname (). Equals ("price ")){
Book. setprice (float. parsefloat (PARSE. nexttext ()));
}
}
Break;
Case xmlpullparser. end_tag:
If (PARSE. getname (). Equals ("book ")){
Books. Add (book );
Book = NULL;
}
Break;
Default:
Break;
}
Event = parse. Next (); // enter the next element and trigger the corresponding event
}
Return books;
}
}

There is another important point here. I like to put XML files and classes in a directory on the Internet, so I don't think this is the case. Here I will make it in the resource raw file.

Pullparseservice = new pullparseservice ();
Try {
/**
* 1. this. getclass (). getclassloader (). getresourceasstream ("book. XML "); // return inputsream. put the XML file in the same folder of this type of file
* 2. for resources on the network server, you can:
* String url = "http://eagle.phys.utk.edu/reubendb/UTRoute.php ";
* String data = "? LAT1 = 35952967 & lon1 =-83929158 & LAT2 = 35956567 & lon2 =-83925450 ";
* String xmlurl = "";
* New URL (""). openstream (); // return the same inputsream
* 3. Put the following information in the resource file.
*/

Books = pullparseservice. parsedatesource (pullxmltestactivity. This. getresources (). openrawresource (R. Raw. Books ));
} Catch (exception e ){

E. printstacktrace ();
}

I found that the XML format arrangement also has a lot to do with the concise line of your XML file. For example, I want to save XML files that can be used for some column points as follows:

 <trkpt lat="35952967" lon="-83929158" grade="1"></trkpt>  

However, if you arrange XML content in this way, when the data volume is large, it cannot be read.

<trkpt>
<lat>45132123</lat>
<lon>5454</lon>
<grade>5847654</grade>
</trkpt>

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.