首先定义一个接口
1. package com.lei.xml;
2.
3. import java.io.InputStream;
4. import java.util.List;
5.
6. public interface XMLParser {
7. // 解析xml
8. <T> List<T> xmlParse(InputStream is);
9.
10. //构造xml
11. <T> String xmlSerializer(List<T> list);
12. }
1. PULL解析
解析速度快,事件处理,不会预加载整个文档
1. package com.lei.xml;
2.
3. import java.io.IOException;
4. import java.io.InputStream;
5. import java.io.StringWriter;
6. import java.util.ArrayList;
7. import java.util.List;
8.
9. import org.xmlpull.v1.XmlPullParser;
10. import org.xmlpull.v1.XmlPullParserException;
11. import org.xmlpull.v1.XmlSerializer;
12.
13. import android.util.Xml;
14.
15. public class PullParse implements XMLParser {
16.
17. public static PullParse getInstance() {
18. return new PullParse();
19. }
20.
21. public <T> List<T> xmlParse(InputStream is) {
22. null;
23. null;
24. XmlPullParser parser = Xml.newPullParser();
25. // XmlPullParser parser =
26. // XmlPullParserFactory.newInstance().newPullParser();
27. try {
28. "utf-8");
29.
30. int type = parser.getEventType();
31. while (type != XmlPullParser.END_DOCUMENT) {//整个xml结束标签,出口
32.
33. switch (type) {
34. case XmlPullParser.START_DOCUMENT://整个xml开始标签,入口
35. new ArrayList<Book>();
36. break;
37.
38. case XmlPullParser.START_TAG://子开始标签
39. if (parser.getName().equals("book")) {
40. new Book();
41. else if (parser.getName().equals("id")) {
42. type = parser.next();
43. book.setId(Integer.parseInt(parser.getText()));
44.
45. else if (parser.getName().equals("name")) {
46. type = parser.next();
47. book.setName(parser.getText());
48. }
49. break;
50. case XmlPullParser.END_TAG://子结束标签
51. if (parser.getName().equals("book")) {
52. list.add(book);
53. }
54. break;
55. }
56. type = parser.next();
57. }
58.
59. catch (XmlPullParserException e) {
60. e.printStackTrace();
61. catch (IOException e) {
62. e.printStackTrace();
63. }
64.
65. return (List<T>) list;
66. }
67.
68. public <T> String xmlSerializer(List<T> list) {
69. List<Book> lists = (List<Book>) list;
70. XmlSerializer serializer = Xml.newSerializer();
71. try {
72. "utf-8", true);
73. new StringWriter());
74. "", "books");
75.
76. for (Book book : lists) {
77. "", "book");
78.
79. "", "id", String.valueOf(book.getId()));
80.
81. "", "name");
82. serializer.text(book.getName());
83. "", "name");
84.
85. "", "book");
86. }
87.
88. "", "books");
89. serializer.endDocument();
90. catch (IllegalArgumentException e) {
91. e.printStackTrace();
92. catch (IllegalStateException e) {
93. e.printStackTrace();
94. catch (IOException e) {
95. e.printStackTrace();
96. }
97.
98. return serializer.toString();
99. }
100.
101. }
2. SAX解析
树形结构,速度款,占用内存少,会预加载整个文档,适用于文档较小。
1. package com.lei.xml;
2.
3. import java.io.IOException;
4. import java.io.InputStream;
5. import java.util.ArrayList;
6. import java.util.List;
7.
8. import javax.xml.parsers.ParserConfigurationException;
9. import javax.xml.parsers.SAXParser;
10. import javax.xml.parsers.SAXParserFactory;
11.
12. import org.xml.sax.Attributes;
13. import org.xml.sax.SAXException;
14. import org.xml.sax.helpers.DefaultHandler;
15.
16. import com.lei.utils.Logs;
17.
18. public class SaxParse implements XMLParser {
19.
20. public static SaxParse getInstance() {
21. return new SaxParse();
22. }
23.
24. /**
25. * 解析xml
26. */
27. public <T> List<T> xmlParse(InputStream is) {
28. try {
29. SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
30. new MyXmlHandler();
31. parser.parse(is, handler);
32.
33. return (List<T>) handler.getBookList();
34. catch (ParserConfigurationException e) {
35. e.printStackTrace();
36. catch (SAXException e) {
37. e.printStackTrace();
38. catch (IOException e) {
39. e.printStackTrace();
40. }
41. return null;
42. }
43.
44. /**
45. * 生成xml
46. */
47. public <T> String xmlSerializer(List<T> list) {
48. return null;
49. }
50.
51. class MyXmlHandler extends DefaultHandler {
52. List<Book> list;
53. Book book;
54. StringBuilder builder;
55.
56. public List<Book> getBookList() {
57.
58. return list;
59. }
60.
61. // 入口,开始
62. @Override
63. public void startDocument() throws SAXException {
64. super.startDocument();
65. "startDocument");
66. new ArrayList<Book>();
67. new StringBuilder();
68. }
69.
70. // 出口,结束
71. @Override
72. public void endDocument() throws SAXException {
73. super.endDocument();
74. "endDocument");
75.
76. }
77.
78. // 开始解析开始标签
79. @Override
80. public void startElement(String uri, String localName, String qName,
81. throws SAXException {
82. super.startElement(uri, localName, qName, attributes);
83. "startElement " + "uri " + uri + " localName " + localName
84. " qName " + qName);
85. if ("book".equals(localName)) {
86. new Book();
87. }
88. 0);// 每次豆浆builder清空
89.
90. }
91.
92. // 结束标签
93. @Override
94. public void endElement(String uri, String localName, String qName)
95. throws SAXException {
96. super.endElement(uri, localName, qName);
97. "endElement " + "uri " + uri + " localName " + localName
98. " qName " + qName);
99. if ("id".equals(localName)) {
100. book.setId(Integer.parseInt(builder.toString()));
101. else if ("name".equals(localName)) {
102. book.setName(builder.toString());
103. else if ("book".equals(localName)) {
104. list.add(book);
105. }
106.
107. }
108.
109. // 获取子标签的内容
110. @Override
111. public void characters(char[] ch, int start, int length)
112. throws SAXException {
113. super.characters(ch, start, length);
114. builder.append(ch, start, length);
115. "characters " + new String(ch, start, length));
116. }
117.
118. }
119.
120. }
3.DOM解析
预先加载整个文档,占用内存大,不推荐使用
1. package com.lei.xml;
2.
3. import java.io.IOException;
4. import java.io.InputStream;
5. import java.util.ArrayList;
6. import java.util.List;
7.
8. import javax.xml.parsers.DocumentBuilder;
9. import javax.xml.parsers.DocumentBuilderFactory;
10. import javax.xml.parsers.ParserConfigurationException;
11.
12. import org.w3c.dom.Document;
13. import org.w3c.dom.Element;
14. import org.w3c.dom.Node;
15. import org.w3c.dom.NodeList;
16. import org.xml.sax.SAXException;
17.
18. public class DomParse implements XMLParser {
19.
20. public static DomParse getInstance(){
21. return new DomParse();
22. }
23.
24. public <T> List<T> xmlParse(InputStream is) {
25. new ArrayList<Book>();
26. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
27. try {
28. DocumentBuilder builder = factory.newDocumentBuilder();
29. Document doc = builder.parse(is);
30. "book");
31. NodeList nodeList = element.getChildNodes();
32. null;
33. for (int i = 0; i < nodeList.getLength(); i++) {
34. Node node = nodeList.item(i);
35. new Book();
36.
37. NodeList details = node.getChildNodes();
38. for (int j = 0; j < details.getLength(); j++) {
39. if (node.getLocalName().equals("id")) {
40. book.setId(Integer.parseInt(node.getNodeValue()));
41. else if (node.getLocalName().equals("name")) {
42. book.setName(node.getNodeValue());
43. }
44. }
45. list.add(book);
46. }
47.
48. catch (ParserConfigurationException e) {
49. e.printStackTrace();
50. catch (SAXException e) {
51. e.printStackTrace();
52. catch (IOException e) {
53. e.printStackTrace();
54. }
55.
56. return (List<T>) list;
57. }
58.
59. public <T> String xmlSerializer(List<T> list) {
60. return null;
61. }
62.
63. }
使用:
在 assets下放了一个books.xml
1. private void parseXmBySax() {标签:xml,方式,list,List,book,import,解析,public From: https://blog.51cto.com/u_15928064/5984719
2. try {
3. "books.xml");
4. new SaxParse().xmlParse(is);
5. for (Book book : list) {
6. mShowTv.append(book.toString());
7. }
8.
9. catch (IOException e) {
10. e.printStackTrace();
11. }
12. }
13.
14. private void parseXmByPull() {
15. null;
16. try {
17. "books.xml");
18. catch (IOException e) {
19. e.printStackTrace();
20. }
21. List<Book> list = PullParse.getInstance().xmlParse(is);
22. for (Book book : list) {
23. mShowTv.append(book.toString());
24. }
25. }
26.
27. private void parseXmlByDom() {
28. null;
29. try {
30. "books.xml");
31. catch (IOException e) {
32. e.printStackTrace();
33. }
34. new SaxParse().xmlParse(is);
35. for (Book book : list) {
36. mShowTv.append(book.toString());
37. }
38. }