首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

XML解析范例

2012-07-04 
XML解析实例1. XML解析有3中方式:[1] DOM方式 ?把整个XML文档读入内存中,再DOM API来访问树形结构,获取数

XML解析实例

1. XML解析有3中方式:

[1] DOM方式 ?把整个XML文档读入内存中,再DOM API来访问树形结构,获取数据; 缺点:文档很大时,消耗较多内存。

?

[2]?SAX方式 基于事件驱动的,单向读取,优点:不占内存空间、解析属性方便,但缺点就是对于套嵌多个分支来说处理不是很方便。

?

[3] PULL方式?PULL常常用在J2ME对于节点处理比较好,类似SAX方式,同样很节省内存。

?

2. 首先需要解析的XML文件代码如下:

?

3.RiverModel代码如下:

package com.testxml.demo;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NodeList;import org.xml.sax.Attributes;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;import org.xml.sax.XMLReader;import org.xml.sax.helpers.DefaultHandler;import org.xmlpull.v1.XmlPullParser;import org.xmlpull.v1.XmlPullParserException;import android.content.Context;import android.util.Xml;public class XMLHelper {// DOM方式解析 把整个XML文档读入内存中,再DOM API来访问树形结构,获取数据; 缺点:文档很大时,消耗很多内存public static List<RiverModel> DOMMannerParse(Context context,String fileName) {List<RiverModel> rivers = new ArrayList<RiverModel>();DocumentBuilderFactory factory = null;DocumentBuilder builder = null;Document document = null;InputStream inputStream = null;factory = DocumentBuilderFactory.newInstance();try {builder = factory.newDocumentBuilder();inputStream = context.getResources().getAssets().open(fileName);document = builder.parse(inputStream);Element root = document.getDocumentElement();// 解析子节点NodeList nodeList = root.getElementsByTagName(RiverModel.RIVER);RiverModel riverModel = null;for (int i = 0; i < nodeList.getLength(); i++) {riverModel = new RiverModel();Element riverElement = (Element) nodeList.item(i);riverModel.setName(riverElement.getAttribute(RiverModel.NAME));riverModel.setLength(Integer.parseInt(riverElement.getAttribute(RiverModel.LENGTH)));// 解析子节点Element introduction = (Element) riverElement.getElementsByTagName(RiverModel.INTRODUCTION).item(0);riverModel.setIntroduction(introduction.getFirstChild().getNodeValue());Element imgurl = (Element) riverElement.getElementsByTagName(RiverModel.IMGURL).item(0);riverModel.setImgUrl(imgurl.getFirstChild().getNodeValue());rivers.add(riverModel);}} catch (IOException e) {e.printStackTrace();} catch (SAXException e) {e.printStackTrace();} catch (ParserConfigurationException e) {e.printStackTrace();} finally {try {inputStream.close();} catch (Exception e) {e.printStackTrace();}}return rivers;}// PULL方式解析public static List<RiverModel> PULLParseXML(Context context, String fileName) {List<RiverModel> rivers = new ArrayList<RiverModel>();RiverModel riverModel = null;InputStream inputStream = null;XmlPullParser xmlPullParser = Xml.newPullParser();try {inputStream = context.getResources().getAssets().open(fileName);xmlPullParser.setInput(inputStream, "utf-8");int evtType = xmlPullParser.getEventType();while (evtType != XmlPullParser.END_DOCUMENT) {switch (evtType) {case XmlPullParser.START_TAG:String tag = xmlPullParser.getName();if (tag.equalsIgnoreCase(RiverModel.RIVER)) {riverModel = new RiverModel();riverModel.setName(xmlPullParser.getAttributeValue(null, RiverModel.NAME));riverModel.setLength(Integer.parseInt(xmlPullParser.getAttributeValue(null, RiverModel.LENGTH)));}// 如果是解析子节点else if (riverModel != null) {if (tag.equalsIgnoreCase(RiverModel.INTRODUCTION)) {riverModel.setIntroduction(xmlPullParser.nextText());} else if (tag.equalsIgnoreCase(RiverModel.IMGURL)) {riverModel.setImgUrl(xmlPullParser.nextText());}}break;case XmlPullParser.END_TAG:// 当碰到river结束时,添加RiverModel进rivers中去if (xmlPullParser.getName().equalsIgnoreCase(RiverModel.RIVER)&& riverModel != null) {rivers.add(riverModel);riverModel = null;}break;default:break;}}} catch (XmlPullParserException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return rivers;}// SAX方式解析public static List<RiverModel> SAXParseXML(Context context, String fileName) {List<RiverModel> rivers = null;SAXParserFactory factory = SAXParserFactory.newInstance();try {SAXParser parser = factory.newSAXParser();XMLReader xmlReader = parser.getXMLReader();MyContentHandler handler = new MyContentHandler();xmlReader.setContentHandler(handler);xmlReader.parse(new InputSource(context.getResources().getAssets().open(fileName)));rivers = handler.getRivers();} catch (ParserConfigurationException e) {e.printStackTrace();} catch (SAXException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return rivers;}static class MyContentHandler extends DefaultHandler {private boolean isRiver = false;private boolean isIntroduction = false;private boolean isImageUrl = false;private RiverModel riverModel = null;private List<RiverModel> rivers = new ArrayList<RiverModel>();public List<RiverModel> getRivers() {return rivers;}@Overridepublic void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {String tagName = localName.length() != 0 ? localName : qName;tagName = tagName.toLowerCase().trim();if (tagName.equals(RiverModel.RIVER)) {isRiver = true;riverModel = new RiverModel();riverModel.setName(attributes.getValue(RiverModel.NAME));riverModel.setLength(Integer.parseInt(attributes.getValue(RiverModel.LENGTH)));}if (isRiver) {if (tagName.equals(RiverModel.INTRODUCTION)) {isIntroduction = true;} else if (tagName.equals(RiverModel.IMGURL)) {isImageUrl = true;}}// super.startElement(uri, localName, qName, attributes);}@Overridepublic void characters(char[] ch, int start, int length)throws SAXException {if (isIntroduction) {riverModel.setIntroduction(riverModel.getIntroduction() == null ? new String(ch, start, length) : riverModel.getIntroduction()+ new String(ch, start, length));} else if (isImageUrl) {riverModel.setImgUrl(riverModel.getImgUrl() == null ? new String(ch, start, length) : riverModel.getImgUrl()+ new String(ch, start, length));}// super.characters(ch, start, length);}@Overridepublic void endElement(String uri, String localName, String qName)throws SAXException {String tagName = localName.length() != 0 ? localName : qName;tagName = tagName.toLowerCase().trim();if (tagName.equals(RiverModel.RIVER)) {isRiver = true;rivers.add(riverModel);}if (isRiver) {if (tagName.equals(RiverModel.INTRODUCTION)) {isIntroduction = false;} else if (tagName.equals(RiverModel.IMGURL)) {isImageUrl = false;}}// super.endElement(uri, localName, qName);}}}
?

?

热点排行