核心提示:XMLParaser解析xml的方法(代码实例)/*** 解析xml数据的类*/public class XmlParserUtils {//解析xml的业务方法public static ListU...
XMLParaser解析xml的方法(代码实例)
/**
* 解析xml数据的类
*/
public class XmlParserUtils {
//解析xml的业务方法
public static List<User> parserXml(InputStream in) throws XmlPullParserException, IOException {
List<User> userlist = null;
User user = null;
//[1]获取xml的解析器
XmlPullParser parser = Xml.newPullParser();
//[2]设置解析器要解析的内容
parser.setInput(in,"utf-8");
//[3]获取解析的事件类型
int type = parser.getEventType();
//[4]不停的向下解析
while(type != XmlPullParser.END_DOCUMENT){
//[5]具体判断是开始节点还是结束节点
switch (type){
case XmlPullParser.START_TAG: //解析开始节点
//[6]具体判断一下解析的是哪个开始标签
if("userinfo".equals(parser.getName())){
//创建一个List集合
System.out.println("创建集合");
userlist = new ArrayList<>();
}else if("item".equals(parser.getName())){
//创建一个User
user = new User();
}else if("username".equals(parser.getName())){
user.setUsername(parser.nextText());
}else if("email".equals(parser.getName())){
user.setEmail(parser.nextText());
}else if("password".equals(parser.getName())){
user.setPassword(parser.nextText());
}else if("age".equals(parser.getName())){
user.setAge(Integer.parseInt(parser.nextText()));
}else if("sex".equals(parser.getName())){
user.setSex(parser.nextText());
}else if("idcard".equals(parser.getName())){
user.setIdcard(parser.nextText());
}else if("phonenumber".equals(parser.getName())){
user.setPhonenumber(parser.nextText());
}else if("health".equals(parser.getName())){
user.setHealth(parser.nextText());
}
break;
case XmlPullParser.END_TAG: //解析结束节点
if("item".equals(parser.getName())){
//把JavaBean添加到集合
userlist.add(user);
}
break;
}
type = parser.next();
}
return userlist;
}
}
最后附上xml数据
<userinfo>
<item>
<username>黎红丽</username>
<email>1610984228@qq.com</email>
<password>1234</password>
<age>20</age>
<sex>女</sex>
<idcard>6123281998938892</idcard>
<phonenumber>15267899876</phonenumber>
<health>不仅要做到身体健康和身心健康,还要注意文化的健康。在学习文化知识时,不但要认真学习教科书的内容,还要注意大量阅读课外读物。我读上下五千年、中国之谜,节假日常到图书馆阅读各种书籍。</health>
</item>
<item>
<username>邹长林</username>
<email>101984342@qq.com</email>
<password>1234</password>
<age>25</age>
<sex>男</sex>
<idcard>61232819989328090</idcard>
<phonenumber>13772812803</phonenumber>
<health>身体的健康、心理的健康和文化的健康,始终陪伴着着我成长,(www.lz13.cn)不断让我在成功的道路上奋勇前进</health>
</item>
<item>
<username>邹长林</username>
<email>101984342@qq.com</email>
<password>1234</password>
<age>25</age>
<sex>男</sex>
<idcard>61232819989328090</idcard>
<phonenumber>13772812803</phonenumber>
<health>身体的健康、心理的健康和文化的健康,始终陪伴着着我成长,(www.lz13.cn)不断让我在成功的道路上奋勇前进</health>
</item>
<item>
<username>邹长林</username>
<email>101984342@qq.com</email>
<password>1234</password>
<age>25</age>
<sex>男</sex>
<idcard>61232819989328090</idcard>
<phonenumber>13772812803</phonenumber>
<health>身体的健康、心理的健康和文化的健康,始终陪伴着着我成长,(www.lz13.cn)不断让我在成功的道路上奋勇前进</health>
</item>
<item>
<username>邹长林</username>
<email>101984342@qq.com</email>
<password>1234</password>
<age>25</age>
<sex>男</sex>
<idcard>61232819989328090</idcard>
<phonenumber>13772812803</phonenumber>
<health>身体的健康、心理的健康和文化的健康,始终陪伴着着我成长,(www.lz13.cn)不断让我在成功的道路上奋勇前进</health>
</item>
<item>
<username>黎红丽</username>
<email>1610984228@qq.com</email>
<password>1234</password>
<age>20</age>
<sex>女</sex>
<idcard>6123281998938892</idcard>
<phonenumber>15267899876</phonenumber>
<health>不仅要做到身体健康和身心健康,还要注意文化的健康。在学习文化知识时,不但要认真学习教科书的内容,还要注意大量阅读课外读物。我读上下五千年、中国之谜,节假日常到图书馆阅读各种书籍。</health>
</item>
<item>
<username>黎红丽</username>
<email>1610984228@qq.com</email>
<password>1234</password>
<age>20</age>
<sex>女</sex>
<idcard>6123281998938892</idcard>
<phonenumber>15267899876</phonenumber>
<health>不仅要做到身体健康和身心健康,还要注意文化的健康。在学习文化知识时,不但要认真学习教科书的内容,还要注意大量阅读课外读物。我读上下五千年、中国之谜,节假日常到图书馆阅读各种书籍。</health>
</item>
<item>
<username>黎红丽</username>
<email>1610984228@qq.com</email>
<password>1234</password>
<age>20</age>
<sex>女</sex>
<idcard>6123281998938892</idcard>
<phonenumber>15267899876</phonenumber>
<health>不仅要做到身体健康和身心健康,还要注意文化的健康。在学习文化知识时,不但要认真学习教科书的内容,还要注意大量阅读课外读物。我读上下五千年、中国之谜,节假日常到图书馆阅读各种书籍。</health>
</item>
<item>
<username>黎红丽</username>
<email>1610984228@qq.com</email>
<password>1234</password>
<age>20</age>
<sex>女</sex>
<idcard>6123281998938892</idcard>
<phonenumber>15267899876</phonenumber>
<health>不仅要做到身体健康和身心健康,还要注意文化的健康。在学习文化知识时,不但要认真学习教科书的内容,还要注意大量阅读课外读物。我读上下五千年、中国之谜,节假日常到图书馆阅读各种书籍。</health>
</item>
<item>
<username>黎红丽</username>
<email>1610984228@qq.com</email>
<password>1234</password>
<age>20</age>
<sex>女</sex>
<idcard>6123281998938892</idcard>
<phonenumber>15267899876</phonenumber>
<health>不仅要做到身体健康和身心健康,还要注意文化的健康。在学习文化知识时,不但要认真学习教科书的内容,还要注意大量阅读课外读物。我读上下五千年、中国之谜,节假日常到图书馆阅读各种书籍。</health>
</item>
<item>
<username>黎红丽</username>
<email>1610984228@qq.com</email>
<password>1234</password>
<age>20</age>
<sex>女</sex>
<idcard>6123281998938892</idcard>
<phonenumber>15267899876</phonenumber>
<health>不仅要做到身体健康和身心健康,还要注意文化的健康。在学习文化知识时,不但要认真学习教科书的内容,还要注意大量阅读课外读物。我读上下五千年、中国之谜,节假日常到图书馆阅读各种书籍。</health>
</item>
</userinfo>


