xml:
<?xml version="1.0" encoding="utf-8"?>
<books><book>
<id>1001</id>
<name>Thinking In Java</name>
<price>80.00</price>
</book>
<book>
<id>1002</id>
<name>Core Java</name>
<price>90.00</price>
</book>
<book>
<id>1003</id>
<name>Hello, Andriod</name>
<price>100.00</price>
</book>
</books>
bean:
public class Book {
private String id;
private String name;
private String price;
public Book() {
super();
// TODO Auto-generated constructor stub
}
public Book(String id, String name, String price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
/**
* @return the id
*/
public String getId() {
return id;
}
/**
* @param id
* the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the price
*/
public String getPrice() {
return price;
}
/**
* @param price
* the price to set
*/
public void setPrice(String price) {
this.price = price;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Book [id=" + id + ", name=" + name + ", price=" + price + "]";
}
}
DOM:
public void byDom(View v){
List<Book> list = new ArrayList<Book>();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
Document document = null;
try {
builder = factory.newDocumentBuilder();
document = builder.parse(getAssets().open("books.xml"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Element element = document.getDocumentElement();
NodeList nodeList = element.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if(node.getNodeType() == Node.ELEMENT_NODE){
Book book = new Book();
Element ele = (Element) node;
NodeList childNodes = ele.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
Node item = childNodes.item(j);
if(item.getNodeType() == Node.ELEMENT_NODE){
Element childEle = (Element) item;
String name = childEle.getNodeName();
String value = childEle.getFirstChild().getNodeValue();
if(name.equals("id")){
book.setId(value);
}else if(name.equals("name")){
book.setName(value);
}else if(name.equals("price")){
book.setPrice(value);
}
}
}
list.add(book);
}
}
ListView lv = (ListView) findViewById(R.id.lvForDom);
lv.setAdapter(new ArrayAdapter<Book>(this, android.R.layout.simple_list_item_1, list));
}
PULL:
public void byPull(View v){
try {
List<Book> list = new ArrayList<Book>();
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser pullParser = factory.newPullParser();
pullParser.setInput(getAssets().open("books.xml"), "utf-8");
int type = pullParser.getEventType();
Book book = null;
while(type != XmlPullParser.END_DOCUMENT){
String name = pullParser.getName();
switch (type) {
case XmlPullParser.START_TAG:
if(name.equals("book")){
book = new Book();
}else if(name.equals("id")){
book.setId(pullParser.nextText());
}else if(name.equals("name")){
book.setName(pullParser.nextText());
}else if(name.equals("price")){
book.setPrice(pullParser.nextText());
}
break;
case XmlPullParser.END_TAG:
if(name.equals("book")){
list.add(book);
}
break;
default:
break;
}
type = pullParser.next();
}
ListView lv = (ListView) findViewById(R.id.lvForPull);
lv.setAdapter(new ArrayAdapter<Book>(this, android.R.layout.simple_list_item_1, list));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}