xml解析

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();
}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值