Android XML data parsing
Introduction to this section:
In the previous two sections, we looked at Android’s built-in Http request methods: HttpURLConnection and HttpClient. We originally thought OkHttp It has been integrated, and I wanted to explain the basic usage of Okhttp, but later I found that I still need to introduce a third party, so forget it and put it in the advanced part. Well, in this section we will learn about the three solutions for parsing XML data provided by Android! They are: There are three parsing methods: SAX, DOM, and PULL. Let’s learn about them below!
1. Introduction to the key points of XML data
First, let’s take a look at some requirements and concepts of XML data:
##2. Comparison of three XML parsing methods
3. SAX parsing XML data
Core code:
SAX parsing class:SaxHelper.java:
/** * Created by Jay on 2015/9/8 0008. */ public class SaxHelper extends DefaultHandler { private Person person; private ArrayList persons; //当前解析的元素标签 private String tagName = null; /** * 当读取到文档开始标志是触发,通常在这里完成一些初始化操作 */ @Override public void startDocument() throws SAXException { this.persons = new ArrayList(); Log.i("SAX", "读取到文档头,开始解析xml"); } /** * 读到一个开始标签时调用,第二个参数为标签名,最后一个参数为属性数组 */ @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (localName.equals("person")) { person = new Person(); person.setId(Integer.parseInt(attributes.getValue("id"))); Log.i("SAX", "开始处理person元素~"); } this.tagName = localName; } /** * 读到到内容,第一个参数为字符串内容,后面依次为起始位置与长度 */ @Override public void characters(char[] ch, int start, int length) throws SAXException { //判断当前标签是否有效 if (this.tagName != null) { String data = new String(ch, start, length); //读取标签中的内容 if (this.tagName.equals("name")) { this.person.setName(data); Log.i("SAX", "处理name元素内容"); } else if (this.tagName.equals("age")) { this.person.setAge(Integer.parseInt(data)); Log.i("SAX", "处理age元素内容"); } } } /** * 处理元素结束时触发,这里将对象添加到结合中 */ @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (localName.equals("person")) { this.persons.add(person); person = null; Log.i("SAX", "处理person元素结束~"); } this.tagName = null; } /** * 读取到文档结尾时触发, */ @Override public void endDocument() throws SAXException { super.endDocument(); Log.i("SAX", "读取到文档尾,xml解析结束"); } //获取persons集合 public ArrayList getPersons() { return persons; } }Then we write in MainActivity.java Such a method can be called when parsing XML. That’s fine~
private ArrayList readxmlForSAX() throws Exception { //获取文件资源建立输入流对象 InputStream is = getAssets().open("person1.xml"); //①创建XML解析处理器 SaxHelper ss = new SaxHelper(); //②得到SAX解析工厂 SAXParserFactory factory = SAXParserFactory.newInstance(); //③创建SAX解析器 SAXParser parser = factory.newSAXParser(); //④将xml解析处理器分配给解析器,对文档进行解析,将事件发送给处理器 parser.parse(is, ss); is.close(); return ss.getPersons(); }
Some other words:
Well, by the way, I forgot to tell you that we define a person1.xml file like the following, and then put To the assets directory! The content of the file is as follows:person1.xml
SAX解析 18 XML1 43We combine the three analysis methods into one demo, so we only post all the renderings at the end. Here, we will post the printed ones. Log, I believe everyone will understand the SAX XML parsing process more clearly: #In addition, the blank text outside is also a text node! These nodes will also be walked during analysis!
4.DOM parsing XML data##Core code
:
DomHelper. java/**
* Created by Jay on 2015/9/8 0008.
*/
public class DomHelper {
public static ArrayList queryXML(Context context)
{
ArrayList Persons = new ArrayList();
try {
//①获得DOM解析器的工厂示例:
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
//②从Dom工厂中获得dom解析器
DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
//③把要解析的xml文件读入Dom解析器
Document doc = dbBuilder.parse(context.getAssets().open("person2.xml"));
System.out.println("处理该文档的DomImplemention对象=" + doc.getImplementation());
//④得到文档中名称为person的元素的结点列表
NodeList nList = doc.getElementsByTagName("person");
//⑤遍历该集合,显示集合中的元素以及子元素的名字
for(int i = 0;i < nList.getLength();i++)
{
//先从Person元素开始解析
Element personElement = (Element) nList.item(i);
Person p = new Person();
p.setId(Integer.valueOf(personElement.getAttribute("id")));
//获取person下的name和age的Note集合
NodeList childNoList = personElement.getChildNodes();
for(int j = 0;j < childNoList.getLength();j++)
{
Node childNode = childNoList.item(j);
//判断子note类型是否为元素Note
if(childNode.getNodeType() == Node.ELEMENT_NODE)
{
Element childElement = (Element) childNode;
if("name".equals(childElement.getNodeName()))
p.setName(childElement.getFirstChild().getNodeValue());
else if("age".equals(childElement.getNodeName()))
p.setAge(Integer.valueOf(childElement.getFirstChild().getNodeValue()));
}
}
Persons.add(p);
}
} catch (Exception e) {e.printStackTrace();}
return Persons;
}
}
:
From the code we can see the process of DOM parsing XML. First, the entire file is read into the Dom parser. , and then form a tree, Then we can traverse the node list to get the data we need!5.PULL parses XML data
Use PULL Process of parsing XML data
:
Core code: public static ArrayList getPersons(InputStream xml)throws Exception
{
//XmlPullParserFactory pullPaser = XmlPullParserFactory.newInstance();
ArrayList persons = null;
Person person = null;
// 创建一个xml解析的工厂
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
// 获得xml解析类的引用
XmlPullParser parser = factory.newPullParser();
parser.setInput(xml, "UTF-8");
// 获得事件的类型
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
persons = new ArrayList();
break;
case XmlPullParser.START_TAG:
if ("person".equals(parser.getName())) {
person = new Person();
// 取出属性值
int id = Integer.parseInt(parser.getAttributeValue(0));
person.setId(id);
} else if ("name".equals(parser.getName())) {
String name = parser.nextText();// 获取该节点的内容
person.setName(name);
} else if ("age".equals(parser.getName())) {
int age = Integer.parseInt(parser.nextText());
person.setAge(age);
}
break;
case XmlPullParser.END_TAG:
if ("person".equals(parser.getName())) {
persons.add(person);
person = null;
}
break;
}
eventType = parser.next();
}
return persons;
}
:
Core code:
public static void save(List persons, OutputStream out) throws Exception { XmlSerializer serializer = Xml.newSerializer(); serializer.setOutput(out, "UTF-8"); serializer.startDocument("UTF-8", true); serializer.startTag(null, "persons"); for (Person p : persons) { serializer.startTag(null, "person"); serializer.attribute(null, "id", p.getId() + ""); serializer.startTag(null, "name"); serializer.text(p.getName()); serializer.endTag(null, "name"); serializer.startTag(null, "age"); serializer.text(p.getAge() + ""); serializer.endTag(null, "age"); serializer.endTag(null, "person"); } serializer.endTag(null, "persons"); serializer.endDocument(); out.flush(); out.close(); }
6. Code sample download:
Running renderings:
Code download:XMLParseDemo.zip:Download XMLParseDemo.zip
Summary of this section:
This section introduces three commonly used XML parsing methods in Android, DOM, SAX and PULL. We recommend using the latter for mobile terminals. There are two types, and PULL is simpler to use. I won’t go into details here. Code is the best teacher~ That’s it for this section. We will Come and learn the JSON parsing method provided by Android! Thank you~