Xml Code
<? 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>
Public static readLocalXml throws Exception {
// Create Pull resolution
InputStream inputStream = getResources (). openRawResource (R. raw. frist );
XmlPullParser pullParser = Xml. newPullParser ();
// Parse XML
PullParser. setInput (inputStream, "UTF-8 ");
// Start
Int eventType = pullParser. getEventType ();
List <Book> books = null;
Book = null;
While (eventType! = XmlPullParser. END_DOCUMENT ){
String nodeName = pullParser. getName ();
Switch (eventType ){
// Start the document
Case XmlPullParser. START_DOCUMENT:
Books = new ArrayList <Book> ();
Break;
// Start Node
Case XmlPullParser. START_TAG:
If ("book". equals (nodeName )){
Book = new Book ();
// GetAttributeValue (0) Get the first attribute value of the node
Book. setId (Integer
. ParseInt (pullParser. getAttributeValue (0 )));
} Else if ("name". equals (nodeName )){
Book. setName (pullParser. nextText ());
} Else if ("price". equals (nodeName )){
// NextText
Book. setPrice (Float. parseFloat (pullParser. nextText ()));
}
Break;
// End the node
Case XmlPullParser. END_TAG:
If ("book". equals (nodeName )){
Books. add (book );
Book = null;
}
Break;
}
EventType = pullParser. next ();
}
Return books;
}