java解析xml之jdom解析xml示例分享
package com.test;
import java.io.file;
import java.util.arraylist;
import java.util.list;
import org.jdom.document;
import org.jdom.element;
import org.jdom.input.saxbuilder;
public class jdomxml {
public static void main(string[] args) {
file file = new file("e:/people.xml");
saxbuilder builder = new saxbuilder();
try {
document document = builder.build(file);
element root = document.getrootelement();
list<element> list = root.getchildren();
list<people> peoples = new arraylist<people>();
people people = null;
for (element peopleelement : list) {
people = new people();
if(null != peopleelement.getattribute("id")){
people.setid(peopleelement.getattribute("id").getvalue());
}
list<element> childpeopleelements = peopleelement.getchildren();
for (element childpeopleelement : childpeopleelements) {
if ("name".equals(childpeopleelement.getname())) {
people.setenglishname(childpeopleelement.getattributevalue("en"));
people.setname(childpeopleelement.gettext());
}
else if ("age".equals(childpeopleelement.getname())) {
people.setage(childpeopleelement.gettext());
}
}
peoples.add(people);
}
for (people p : peoples) {
system.out.println(p.getid()+"\t"+p.getname()+"\t"+p.getenglishname()+"\t"+p.getage());
}
} catch (exception e) {
e.printstacktrace();
}
}
}
推荐阅读
-
java解析xml之jdom解析xml示例分享
-
java解析xml之dom4j解析xml示例分享
-
java解析xml之dom解析xml示例分享
-
java使用dom4j解析xml配置文件实现抽象工厂反射示例
-
java 解析由String类型拼接的XML文件方法
-
通过dom4j解析xml字符串(示例代码)
-
java dom4j解析xml用到的几个方法
-
java使用xpath和dom4j解析xml
-
Android学习笔记之AndroidManifest.xml文件解析 博客分类: android androidAndroidManifest
-
Android学习笔记之AndroidManifest.xml文件解析(转) 博客分类: Android androidAndroidManifest