package com.softeem.pull; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import org.kxml2.io.KXmlParser; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import com.softeem.dom.write.UserBean; public class Demo1 { public static void main(String[] args) throws XmlPullParserException,IOException { List<UserBean> userList = null; UserBean userBean = null; List<String> favoriteList = null; // 1.创建解析器 KXmlParser parser = new KXmlParser(); // 2.设置输入流:保证 读取的编码方式和xml文档中声明的encoding一致 // parser.setInput(new FileInputStream(new File("src\\UserList.xml")),// "utf-8"); parser.setInput(new InputStreamReader(new FileInputStream(new File( "src\\UserList.xml")),"utf-8")); // 3.解析 // parser.getEventType();获取事件类型。 // parser.next();继续读取下一个标签。 // parser.getName();获取当前标签的标签名 // parset.nextText();获取当前标签后的文本 // <username>aaa int type = parser.getEventType(); // DOCUMENT_START while (type != XmlPullParser.END_DOCUMENT) { switch (type) { case XmlPullParser.START_TAG: if ("userlist".equals(parser.getName())) { // userlist开始标签 userList = new ArrayList<UserBean>(); } if ("user".equals(parser.getName())) { // user开始标签 userBean = new UserBean(); // 读取id属性 // userBean.setId(parser.getAttributeValue(0)); // getAttributeCount():获取属性数量 // getAttributeName(int index):获取第index个属性名 // getAttributeValue(int index):获取第index个属性值 // getAttributeValue(String 命名空间,String 属性名); // getNameSpace()获取命名空间 String nameSpace = parser.getNamespace(); String id = parser.getAttributeValue(nameSpace,"id"); System.out.println(parser.getAttributeValue(nameSpace,"sex")); userBean.setId(id); } if ("username".equals(parser.getName())) { // username开始标签: userBean.setUsername(parser.nextText()); } if ("password".equals(parser.getName())) { // password开始标签: userBean.setPassword(parser.nextText()); } if ("favoritelist".equals(parser.getName())) { // favoritelist开始标签 favoriteList = new ArrayList<String>(); } if ("favorite".equals(parser.getName())) { // favorite开始标签 favoriteList.add(parser.nextText()); } break; case XmlPullParser.END_TAG: if ("favoritelist".equals(parser.getName())) { // favoritelist结束标签 userBean.setFavorites(favoriteList); } if ("user".equals(parser.getName())) { // user结束标签 userList.add(userBean); } break; } // 跳转到下一个标签 type = parser.next(); } } }
UserList.xml
<?xml version="1.0" encoding="utf-8"?> <userlist> <user id="100" sex="男"> <username>aaa</username> <password>aaa</password> <favoritelist> <favorite>aihao1</favorite> <favorite>aihao1</favorite> <favorite>aihao1</favorite> </favoritelist> </user> <user id="101" sex="女"> <username>bbb</username> <password>bbb</password> <favoritelist> <favorite>aihao1</favorite> <favorite>aihao1</favorite> <favorite>aihao1</favorite> </favoritelist> </user> <user id="102" sex="人妖"> <username>ccc</username> <password>ccc</password> <favoritelist> <favorite>aihao1</favorite> <favorite>aihao1</favorite> <favorite>aihao1</favorite> </favoritelist> </user> </userlist>
http://download.csdn.net/detail/hoho_12/9600193
http://download.csdn.net/detail/hoho_12/9600194
原文链接:https://www.f2er.com/xml/294918.html