在bb中的 dom 解析
各位高手,
你们好!
请问一下在BB中怎样进行dom解析?能举个例子?
谢谢!
[解决办法]
高手,指点一下!
[解决办法]
xml的解析如下:
XML:<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="12">
<name>张三</name>
<price>85.5</price>
</book>
<book id="15">
<name>ジョースミス </name>
<price>39.0</price>
</book>
</books>
BB代码:
package com;
import java.io.InputStream;
import java.util.Vector;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;
public class Show extends UiApplication {
public static void main(String[] args) {
Show show = new Show();
show.enterEventDispatcher();
}
public Show() {
pushScreen(new ShowMian());
}
class ShowMian extends MainScreen {
ButtonField buttonField;
public ShowMian() {
buttonField = new ButtonField("我点");
buttonField.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
DomParseService domParseService = new DomParseService();
//创建一个流
InputStream inputStream = this.getClass()
.getResourceAsStream("/book.xml");
try {
Vector vector = domParseService.getBooks(inputStream);
Book book=(Book) vector.elementAt(1);
Dialog.alert("取得的ID:"+String.valueOf(book.getId()));
Dialog.alert("取得的Name:"+String.valueOf(book.getName()));
} catch (Exception e) {
e.printStackTrace();
}
}
});
add(buttonField);
}
}
}
package com;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.xml.parsers.DocumentBuilder;
import net.rim.device.api.xml.parsers.DocumentBuilderFactory;
import net.rim.device.api.xml.parsers.ParserConfigurationException;
public class DomParseService {
Document document;
DocumentBuilder builder;
public Vector getBooks(InputStream inputStream) {
Vector list = new Vector();
//创建一个工厂
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
//创建一个builder
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
//通过builder解析
document = builder.parse(inputStream);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//取到主
Element element = document.getDocumentElement();
//取出一个数据
NodeList bookNodes = element.getElementsByTagName("book");
for (int i = 0; i < bookNodes.getLength(); i++) {
//迭代的方法
Element bookElement = (Element) bookNodes.item(i);
Book book = new Book();
//将值赋予对象
book.setId(Integer.parseInt(bookElement.getAttribute("id")));
//对象里面的所有
NodeList childNodes = bookElement.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
//进行解析
if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
if ("name".equals(childNodes.item(j).getNodeName())) {
book.setName(childNodes.item(j).getFirstChild()
.getNodeValue());
} else if ("price".equals(childNodes.item(j).getNodeName())) {
book.setPrice(Float.parseFloat(childNodes.item(j)
.getFirstChild().getNodeValue()));
}
}
}
list.addElement(book);
}
return list;
}
}
package com;
public class Book {
private int id;
private String name;
private float price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String toString() {
return this.id + ":" + this.name + ":" + this.price;
}
}
写的不好,请谅解!
[解决办法]
黑莓自带APIdom4j 或者你引入第三方包:KXML,即可解析!
[解决办法]
黑莓自带APIdom4j?
能举个例子?
谢谢!
[解决办法]
楼主的那段代码,为什么只读取了book里面第二个数据呢???