Android采用PULL解析XML文档
Android系统中采用PULL技术解析XML文档比用SAX技术性能要好得多。建议Android的开发者采用PULL技术解析XML文档。跟SAX类似,只不过PULL技术返回的事件类型是整型,所以我们只要对返回的事件进行处理就行了。1.package com.cn.service;
- 5.import java.io.IOException;
- 6.
- 7.import java.io.InputStream;
- 8.
- 9.import java.io.OutputStream;
- 10.
- 11.import java.util.ArrayList;
- 12.
- 13.import java.util.List;
- 14.
- 15.
- 16.
- 17.import org.xmlpull.v1.XmlPullParser;
- 18.
- 19.import org.xmlpull.v1.XmlSerializer;
- 20.
- 21.
- 22.
- 23.import android.util.Xml;
- 24.
- 25.
- 26.
- 27.import com.cn.domain.Persion;
- 28.
- 29./****
- 30.
- 31.* 采用Pull解析XML
- 32.
- 33.*
- 34.
- 35.* ****/
- 36.
- 37.public class PULLPersionService {
- 38.
- 39.
- 40.
- 41. public static List<Persion> getPersino(InputStream is)throws Exception{
- 42.
- 43. List<Persion> list = null;
- 44.
- 45. Persion persion = null;
- 46.
- 47. XmlPullParser parser = Xml.newPullParser();
- 48.
- 49. parser.setInput(is, "UTF-8");
- 50.
- 51. int event = parser.getEventType();
- 52.
- 53. while(event != XmlPullParser.END_DOCUMENT){
- 54.
- 55. switch (event){
- 56.
- 57. case XmlPullParser.START_DOCUMENT:
- 58.
- 59. list = new ArrayList<Persion>();
- 60.
- 61. break;
- 62.
- 63. case XmlPullParser.START_TAG:
- 64.
- 65. if("person".equals(parser.getName())){//得到指针所指向的节点名称
- 66.
- 67. int id = Integer.parseInt(parser.getAttributeValue(0));
- 68.
- 69. persion = new Persion();
- 70.
- 71. persion.setId(id);
- 72.
- 73. }
- 74.
- 75. if(persion != null){
- 76.
- 77. if("name".equals(parser.getName())){
- 78.
- 79. String name = parser.nextText();
- 80.
- 81. persion.setName(name);
- 82.
- 83. }else if("age".equals(parser.getName())){
- 84.
- 85. String age = parser.nextText();
- 86.
- 87. persion.setName(age);
- 88.
- 89. }
- 90.
- 91. }
- 92.
- 93. break;
- 94.
- 95. case XmlPullParser.END_TAG:
- 96.
- 97. if("person".equals(parser.getName())){
- 98.
- 99. list.add(persion);
- 100.
- 101. persion = null;
- 102.
- 103. }
- 104.
- 105. break;
- 106.
- 107. default:
- 108.
- 109. break;
- 110.
- 111. }
- 112.
- 113. }
- 114.
- 115. event = parser.next();
- 116.
- 117. return list;
- 118.
- 119. }
- 120.
- 121.
- 122.
- 123. public static void save(List<Persion> list,OutputStream os) throws IllegalArgumentException, IllegalStateException, IOException{
- 124.
- 125. XmlSerializer serializer = Xml.newSerializer();
- 126.
- 127. serializer.setOutput(os, "UTF-8");
- 128.
- 129. serializer.startDocument("UTF-8", true);
- 130.
- 131. serializer.startTag(null, "persons");
- 132.
- 133. for(Persion persion : list){
- 134.
- 135. serializer.startTag(null, "person");
- 136.
- 137. serializer.attribute(null, "id", String.valueOf(persion.getId()));
- 138.
- 139. serializer.startTag(null, "name");
- 140.
- 141. serializer.text(persion.getName());
- 142.
- 143. serializer.endTag(null, "name");
- 144.
- 145.
- 146.
- 147. serializer.startTag(null, "age");
- 148.
- 149. serializer.text(String.valueOf(persion.getAge()));
- 150.
- 151. serializer.endTag(null, "age");
- 152.
- 153. serializer.endTag(null, "person");
- 154.
- 155. }
- 156.
- 157. serializer.endTag(null, "persons");
- 158.
- 159. serializer.endDocument();
- 160.
- 161. os.flush();
- 162.
- 163. os.close();
- 164.
- 165. }
- 166.
- 167.}
- 168.复制打印
- 169.package com.cn.service;
- 170.
- 171.import java.io.IOException;
- 172.import java.io.InputStream;
- 173.import java.io.OutputStream;
- 174.import java.util.ArrayList;
- 175.import java.util.List;
- 176.
- 177.import org.xmlpull.v1.XmlPullParser;
- 178.import org.xmlpull.v1.XmlSerializer;
- 179.
- 180.import android.util.Xml;
- 181.
- 182.import com.cn.domain.Persion;
- 183./****
- 184.* 采用Pull解析XML
- 185.*
- 186.* ****/
- 187.public class PULLPersionService {
- 188.
- 189. public static List<Persion> getPersino(InputStream is)throws Exception{
- 190. List<Persion> list = null;
- 191. Persion persion = null;
- 192. XmlPullParser parser = Xml.newPullParser();
- 193. parser.setInput(is, "UTF-8");
- 194. int event = parser.getEventType();
- 195. while(event != XmlPullParser.END_DOCUMENT){
- 196. switch (event){
- 197. case XmlPullParser.START_DOCUMENT:
- 198. list = new ArrayList<Persion>();
- 199. break;
- 200. case XmlPullParser.START_TAG:
- 201. if("person".equals(parser.getName())){//得到指针所指向的节点名称
- 202. int id = Integer.parseInt(parser.getAttributeValue(0));
- 203. persion = new Persion();
- 204. persion.setId(id);
- 205. }
- 206. if(persion != null){
- 207. if("name".equals(parser.getName())){
- 208. String name = parser.nextText();
- 209. persion.setName(name);
- 210. }else if("age".equals(parser.getName())){
- 211. String age = parser.nextText();
- 212. persion.setName(age);
- 213. }
- 214. }
- 215. break;
- 216. case XmlPullParser.END_TAG:
- 217. if("person".equals(parser.getName())){
- 218. list.add(persion);
- 219. persion = null;
- 220. }
- 221. break;
- 222. default:
- 223. break;
- 224. }
- 225. }
- 226. event = parser.next();
- 227. return list;
- 228. }
- 229.
- 230. public static void save(List<Persion> list,OutputStream os) throws IllegalArgumentException, IllegalStateException, IOException{
- 231. XmlSerializer serializer = Xml.newSerializer();
- 232. serializer.setOutput(os, "UTF-8");
- 233. serializer.startDocument("UTF-8", true);
- 234. serializer.startTag(null, "persons");
- 235. for(Persion persion : list){
- 236. serializer.startTag(null, "person");
- 237. serializer.attribute(null, "id", String.valueOf(persion.getId()));
- 238. serializer.startTag(null, "name");
- 239. serializer.text(persion.getName());
- 240. serializer.endTag(null, "name");
- 241.
- 242. serializer.startTag(null, "age");
- 243. serializer.text(String.valueOf(persion.getAge()));
- 244. serializer.endTag(null, "age");
- 245. serializer.endTag(null, "person");
- 246. }
- 247. serializer.endTag(null, "persons");
- 248. serializer.endDocument();
- 249. os.flush();
- 250. os.close();
- 251. }
- 252.}
复制代码
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论