pull解析xml文件,和sax和dom一样 都可以脱离android单独使用,pull和sax的原理一样,不一样的地方是pull读取xml文件后调用方法返回的是数字,
读取到xml的声明返回数字0 START_DOCUMENT;
读取到xml的结束返回数字1 END_DOCUMENT ;
读取到xml的开始标签返回数字2 START_TAG
读取到xml的结束标签返回数字3 END_TAG
读取到xml的文本返回数字4 TEXT
pull是开源的项目 源码下载地址http://www.xmlpull.org/
被解析的xml文档和android dom 解析xml方式 中的xml文档一样,命名为pullTest.xml.
private String pullParseXml(InputStream inputStream) { String result = ""; //解析全部的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 ("group".equals(nodeName)) { // 解析<group>节点中的属性值,getAttributeCount()获取属性的个数 for (int i = 0; i < parse.getAttributeCount(); i++) { // 属性名称 String groupName = parse.getAttributeName(i); // 属性名称对应的值 String nameValue = parse.getAttributeValue(i); result = result + groupName + " = " + nameValue; } result += "\n"; } else if ("person".equals(nodeName)) { String personName = parse.getAttributeValue(0); String age = parse.getAttributeValue(1); result = result + "personName = " + personName + "age =" + age + "\n"; } else if ("chinese".equals(nodeName)) { //节点对应的文本 String chinese = parse.nextText(); Pattern p = Pattern.compile("\\s*|\t|\r|\n"); Matcher m = p.matcher(chinese); chinese = m.replaceAll(""); result = result + "chinese = " + chinese; } else if ("english".equals(nodeName)) { String english = parse.nextText(); Pattern p = Pattern.compile("\\s*|\t|\r|\n"); Matcher m = p.matcher(english); english = m.replaceAll(""); result = result + "english = " + english + "\n"; } break; case XmlPullParser.END_TAG: //在解析到一个group节点完成时,退出解析xml文件 // if("group".equals(nodeName)){ // eventType = XmlPullParser.END_DOCUMENT; // isParse = false; // } break; default: break; } //整个xml文件全部解析 if(isParse){ eventType = parse.next(); } } } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; }
pull和sax不同最主要的体现在pull可以由客户随时终止解析xml.sax解析,只能从文档头一直读到尾,中间不能停止也不能对文件进行修改。直到解析完了整个文档才会返回。
//在解析到一个group节点完成时,退出解析xml文件 // if("group".equals(nodeName)){ // eventType = XmlPullParser.END_DOCUMENT; // isParse = false; // }
只要满足退出解析的条件,只需要设置如下代码即可。
eventType = XmlPullParser.END_DOCUMENT;
pull解析方式用到的方法,大部分我都在代码中注释了。
完整项目下载:http://download.csdn.net/detail/nxh_love/3978483
以上就是 android pull 解析xml方式的内容,更多相关内容请关注PHP中文网(www.php.cn)!

When processing XML and RSS data, you can optimize performance through the following steps: 1) Use efficient parsers such as lxml to improve parsing speed; 2) Use SAX parsers to reduce memory usage; 3) Use XPath expressions to improve data extraction efficiency; 4) implement multi-process parallel processing to improve processing speed.

RSS2.0 is an open standard that allows content publishers to distribute content in a structured way. It contains rich metadata such as titles, links, descriptions, release dates, etc., allowing subscribers to quickly browse and access content. The advantages of RSS2.0 are its simplicity and scalability. For example, it allows custom elements, which means developers can add additional information based on their needs, such as authors, categories, etc.

RSS is an XML-based format used to publish frequently updated content. 1. RSSfeed organizes information through XML structure, including title, link, description, etc. 2. Creating RSSfeed requires writing in XML structure, adding metadata such as language and release date. 3. Advanced usage can include multimedia files and classified information. 4. Use XML verification tools during debugging to ensure that the required elements exist and are encoded correctly. 5. Optimizing RSSfeed can be achieved by paging, caching and keeping the structure simple. By understanding and applying this knowledge, content can be effectively managed and distributed.

RSS is an XML-based format used to publish and subscribe to content. The XML structure of an RSS file includes a root element, an element, and multiple elements, each representing a content entry. Read and parse RSS files through XML parser, and users can subscribe and get the latest content.

XML has the advantages of structured data, scalability, cross-platform compatibility and parsing verification in RSS. 1) Structured data ensures consistency and reliability of content; 2) Scalability allows the addition of custom tags to suit content needs; 3) Cross-platform compatibility makes it work seamlessly on different devices; 4) Analytical and verification tools ensure the quality and integrity of the feed.

The implementation of RSS in XML is to organize content through a structured XML format. 1) RSS uses XML as the data exchange format, including elements such as channel information and project list. 2) When generating RSS files, content must be organized according to specifications and published to the server for subscription. 3) RSS files can be subscribed through a reader or plug-in to automatically update the content.

Advanced features of RSS include content namespaces, extension modules, and conditional subscriptions. 1) Content namespace extends RSS functionality, 2) Extended modules such as DublinCore or iTunes to add metadata, 3) Conditional subscription filters entries based on specific conditions. These functions are implemented by adding XML elements and attributes to improve information acquisition efficiency.

RSSfeedsuseXMLtostructurecontentupdates.1)XMLprovidesahierarchicalstructurefordata.2)Theelementdefinesthefeed'sidentityandcontainselements.3)elementsrepresentindividualcontentpieces.4)RSSisextensible,allowingcustomelements.5)Bestpracticesincludeusing


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
