// 解析数据
private void pullData(int i ,InputStream inputStream) {
// 解析全部的xml
boolean isParse = true;
try {
// 创建一个xmlPullParser的工厂
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
// 获取一个解析实例
XmlPullParser parse = factory.newPullParser();
// 设置输入流的编码格式
parse.setInput(inputStream, "UTF-8");
// 当前事件的类型
int eventType = parse.getEventType();
while (XmlPullParser.END_DOCUMENT != eventType) {
// 当前节点的名称
String nodeName = parse.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if ("news".equals(nodeName)) {
// 建立一个新对象
myNews = new MyNews();
} else if ("id".equals(nodeName)) {
String id = parse.nextText();
myNews.setId(id);
} else if ("body".equals(nodeName)) {
// 节点对应的文本
String body = parse.nextText();
myNews.setBody(body);
} else if ("author".equals(nodeName)) {
String author = parse.nextText();
myNews.setAuthor(author);
} else if ("pubDate".equals(nodeName)) {
String pubDate = parse.nextText();
myNews.setPubDate(pubDate);
}else if("title".equals(nodeName)){
String title = parse.nextText();
myNews.setTitle(title);
}else if("commentCount".equals(nodeName)){
String commentCount = parse.nextText();
myNews.setCommentCount(commentCount);
}
break;
case XmlPullParser.END_TAG:
// 在解析到一个节点news结束时
if ("news".equals(nodeName)) {
list_newsTag.add(myNews);
}
// 在解析到一个newslist节点完成时,退出解析xml文件
if ("newslist".equals(nodeName)) {
eventType = XmlPullParser.END_DOCUMENT;
isParse = false;
}
break;
default:
break;
}
// 整个xml文件全部解析
if (isParse) {
eventType = parse.next();
}
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("解析结果是" + list_newsTag);
handler.sendEmptyMessage(i);
}
DOM解析(最慢最费资源)
// 获得dom解析对象
DocumentBuilderFactory builderFactory = DocumentBuilderFactory
.newInstance();
try {
DocumentBuilder newDocumentBuilder = builderFactory
.newDocumentBuilder();
Document document = newDocumentBuilder.parse(getAssets().open(
"books.xml"));
// 开始解析
Element books = document.getDocumentElement();
// 获得一级标签
NodeList booksNodes = books.getChildNodes();
for (int i = 0; i < booksNodes.getLength(); i++) {
// 获得books的节点
Node booksNode = booksNodes.item(i);
if (booksNode.getNodeType() == Node.ELEMENT_NODE) {
// 如果节点是一个标签,那么强转
Element book = (Element) booksNode;
// 获得id
String id = book.getAttribute("id");
System.out.println("标签是" + booksNode.getNodeName() + ":"
+ book.getNodeName() + " id的值是" + id);
// 获得book的节点
NodeList bookNodes = book.getChildNodes();
for (int j = 0; j < bookNodes.getLength(); j++) {
Node bookNode = bookNodes.item(j);
if (bookNode.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) bookNode;
//获得元素
Node node = element.getFirstChild();
if (bookNode.getNodeName().equals("name")) {
System.out.println("name是"
+ node.getNodeValue());
} else if (bookNode.getNodeName().equals("author")) {
System.out.println("author是"
+ node.getNodeValue());
} else if (bookNode.getNodeName().equals("price")) {
System.out.println("price是"
+ node.getNodeValue());
}
}
}
}
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SAX解析
// 获得sax解析类
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
try {
SAXParser newSAXParser = saxParserFactory.newSAXParser();
XMLReader xmlReader = newSAXParser.getXMLReader();
// 设置解析方法
xmlReader.setContentHandler(new DefaultHandler() {
String tagName = null;
@Override
public void startDocument() throws SAXException {
System.out.println("开始读取文件");
super.startDocument();
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
System.out.println("结束读取文件");
super.endDocument();
}
@Override
public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
// TODO Auto-generated method stub
tagName = localName;
if ("book".equals(tagName)) {
System.out.println("开始读取book标签");
String id = attributes.getValue("id");
System.out.println("id是" + id);
}
super.startElement(uri, localName, qName, attributes);
}
@Override
public void endElement(String uri, String localName,
String qName) throws SAXException {
// TODO Auto-generated method stub
// System.out.println("结束读取标签");
tagName = "";
super.endElement(uri, localName, qName);
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
// System.out.println("读取内容");
if ("name".equals(tagName)) {
System.out.println("name="
+ new String(ch, start, length));
} else if ("author".equals(tagName)) {
System.out.println("author="
+ new String(ch, start, length));
} else if ("price".equals(tagName)) {
System.out.println("price="
+ new String(ch, start, length));
}
super.characters(ch, start, length);
}
});
// 开始解析
xmlReader.parse(new InputSource(getAssets().open("books.xml")));
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}