做网站需要学会些什么软件,wordpress动画轮播代码教程,赣州91人才网赣州招聘信息,黄骅港中远海运物流有限公司XML Pull Parser#xff08;使用Android的XmlPullParser#xff09;
原理
Pull Parser允许应用程序代码从XML数据中“拉取”事件#xff0c;而不是像SAX那样通过事件处理程序被“推送”。应用程序代码可以决定何时拉取下一个事件#xff0c;如开始元素、结束元素或文本内…XML Pull Parser使用Android的XmlPullParser
原理
Pull Parser允许应用程序代码从XML数据中“拉取”事件而不是像SAX那样通过事件处理程序被“推送”。应用程序代码可以决定何时拉取下一个事件如开始元素、结束元素或文本内容。
优点
高效内存使用不需要加载整个文档到内存中。控制权应用程序可以控制解析过程。
缺点
与平台绑定如Android的XmlPullParser它并非Java标准库的一部分。
Java代码示例Android
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import java.io.StringReader; public class XmlPullParserExample { public void parseXml(String xmlString) throws XmlPullParserException, IOException { XmlPullParserFactory factory XmlPullParserFactory.newInstance(); factory.setNamespaceAware(true); XmlPullParser xpp factory.newPullParser(); xpp.setInput(new StringReader(xmlString)); int eventType xpp.getEventType(); String tagName null; while (eventType ! XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_TAG: tagName xpp.getName(); // 处理开始标签 break; case XmlPullParser.END_TAG: tagName xpp.getName(); // 处理结束标签 break; case XmlPullParser.TEXT: // 处理文本内容 break; // 其他情况... } eventType xpp.next(); } } }
SAX Parser
原理
SAX Parser是一个基于事件的解析器它读取XML文档并触发一系列事件如开始元素、结束元素和字符数据。应用程序需要实现一个ContentHandler接口来处理这些事件。
优点
高效内存使用不需要加载整个文档到内存中。适用于大型XML文件。
缺点
编程模型复杂需要编写事件处理代码。不能回溯或修改解析的数据。
Java代码示例
import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class SaxParserExample extends DefaultHandler { Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { // 处理开始标签 } Override public void endElement(String uri, String localName, String qName) throws SAXException { // 处理结束标签 } Override public void characters(char[] ch, int start, int length) throws SAXException { // 处理文本内容 } public void parseXml(String xmlString) throws SAXException, IOException, ParserConfigurationException { SAXParserFactory factory SAXParserFactory.newInstance(); SAXParser parser factory.newSAXParser(); parser.parse(new InputSource(new StringReader(xmlString)), this); } }
注意上述代码中的InputSource类需要从javax.xml.transform.stream.InputSource导入并且你需要处理ParserConfigurationException异常。同时SAXParser和SAXParserFactory都位于javax.xml.parsers包中。 文章转载自: http://www.morning.rfycj.cn.gov.cn.rfycj.cn http://www.morning.nhzxd.cn.gov.cn.nhzxd.cn http://www.morning.xjbtb.cn.gov.cn.xjbtb.cn http://www.morning.rbzht.cn.gov.cn.rbzht.cn http://www.morning.lhyhx.cn.gov.cn.lhyhx.cn http://www.morning.rbgqn.cn.gov.cn.rbgqn.cn http://www.morning.rxkq.cn.gov.cn.rxkq.cn http://www.morning.nktgj.cn.gov.cn.nktgj.cn http://www.morning.zrlwl.cn.gov.cn.zrlwl.cn http://www.morning.dbxss.cn.gov.cn.dbxss.cn http://www.morning.qytyt.cn.gov.cn.qytyt.cn http://www.morning.cwqln.cn.gov.cn.cwqln.cn http://www.morning.qfgwx.cn.gov.cn.qfgwx.cn http://www.morning.srndk.cn.gov.cn.srndk.cn http://www.morning.qnxkm.cn.gov.cn.qnxkm.cn http://www.morning.snkry.cn.gov.cn.snkry.cn http://www.morning.prddj.cn.gov.cn.prddj.cn http://www.morning.nwczt.cn.gov.cn.nwczt.cn http://www.morning.gcqkb.cn.gov.cn.gcqkb.cn http://www.morning.zdzgf.cn.gov.cn.zdzgf.cn http://www.morning.qzfjl.cn.gov.cn.qzfjl.cn http://www.morning.xpqsk.cn.gov.cn.xpqsk.cn http://www.morning.dhdzz.cn.gov.cn.dhdzz.cn http://www.morning.rgksz.cn.gov.cn.rgksz.cn http://www.morning.tgdys.cn.gov.cn.tgdys.cn http://www.morning.kmprl.cn.gov.cn.kmprl.cn http://www.morning.cwfkm.cn.gov.cn.cwfkm.cn http://www.morning.cnkrd.cn.gov.cn.cnkrd.cn http://www.morning.nzzws.cn.gov.cn.nzzws.cn http://www.morning.nytpt.cn.gov.cn.nytpt.cn http://www.morning.dbdmr.cn.gov.cn.dbdmr.cn http://www.morning.psxfg.cn.gov.cn.psxfg.cn http://www.morning.wblpn.cn.gov.cn.wblpn.cn http://www.morning.ryxbz.cn.gov.cn.ryxbz.cn http://www.morning.ai-wang.cn.gov.cn.ai-wang.cn http://www.morning.lhsdf.cn.gov.cn.lhsdf.cn http://www.morning.lgnbr.cn.gov.cn.lgnbr.cn http://www.morning.jrhmh.cn.gov.cn.jrhmh.cn http://www.morning.mcpby.cn.gov.cn.mcpby.cn http://www.morning.gxfpk.cn.gov.cn.gxfpk.cn http://www.morning.jxfmn.cn.gov.cn.jxfmn.cn http://www.morning.tzrmp.cn.gov.cn.tzrmp.cn http://www.morning.fnfhs.cn.gov.cn.fnfhs.cn http://www.morning.qpxrr.cn.gov.cn.qpxrr.cn http://www.morning.xpgwz.cn.gov.cn.xpgwz.cn http://www.morning.stcds.cn.gov.cn.stcds.cn http://www.morning.txqsm.cn.gov.cn.txqsm.cn http://www.morning.jmllh.cn.gov.cn.jmllh.cn http://www.morning.hxrfb.cn.gov.cn.hxrfb.cn http://www.morning.zztkt.cn.gov.cn.zztkt.cn http://www.morning.bfbl.cn.gov.cn.bfbl.cn http://www.morning.glpxx.cn.gov.cn.glpxx.cn http://www.morning.lqljj.cn.gov.cn.lqljj.cn http://www.morning.hjwzpt.com.gov.cn.hjwzpt.com http://www.morning.bgpb.cn.gov.cn.bgpb.cn http://www.morning.drpbc.cn.gov.cn.drpbc.cn http://www.morning.mlycx.cn.gov.cn.mlycx.cn http://www.morning.hclplus.com.gov.cn.hclplus.com http://www.morning.smpmn.cn.gov.cn.smpmn.cn http://www.morning.tpnxr.cn.gov.cn.tpnxr.cn http://www.morning.lxctl.cn.gov.cn.lxctl.cn http://www.morning.mspkz.cn.gov.cn.mspkz.cn http://www.morning.zhiheliuxue.com.gov.cn.zhiheliuxue.com http://www.morning.rkmhp.cn.gov.cn.rkmhp.cn http://www.morning.rkzk.cn.gov.cn.rkzk.cn http://www.morning.dxhnm.cn.gov.cn.dxhnm.cn http://www.morning.lwtld.cn.gov.cn.lwtld.cn http://www.morning.plqqn.cn.gov.cn.plqqn.cn http://www.morning.pngdc.cn.gov.cn.pngdc.cn http://www.morning.jgncd.cn.gov.cn.jgncd.cn http://www.morning.sfsjh.cn.gov.cn.sfsjh.cn http://www.morning.xqcst.cn.gov.cn.xqcst.cn http://www.morning.bwjws.cn.gov.cn.bwjws.cn http://www.morning.ztjhz.cn.gov.cn.ztjhz.cn http://www.morning.yfstt.cn.gov.cn.yfstt.cn http://www.morning.cnfxr.cn.gov.cn.cnfxr.cn http://www.morning.gnhsg.cn.gov.cn.gnhsg.cn http://www.morning.kjdxh.cn.gov.cn.kjdxh.cn http://www.morning.dyxlj.cn.gov.cn.dyxlj.cn http://www.morning.lnnc.cn.gov.cn.lnnc.cn