java解析xml之dom4j解析xml示例分享
package com.test;
import java.io.file;
import java.util.arraylist;
import java.util.iterator;
import java.util.list;
import org.dom4j.document;
import org.dom4j.element;
import org.dom4j.io.saxreader;
public class dom4jxml {
public static void main(string[] args) {
saxreader saxreader = new saxreader();
file file = new file("e:/people.xml");
try {
document document = saxreader.read(file);
element root = document.getrootelement();
list<people> peoples = new arraylist<people>();
for (iterator iterator = root.elementiterator(); iterator.hasnext();) {
people people = new people();
element peopleelement = (element) iterator.next();
people.setid(peopleelement.attribute("id").getvalue());
for (iterator childiterator = peopleelement.elementiterator(); childiterator.hasnext();) {
element childpeopleelement = (element) childiterator.next();
if (childpeopleelement.getname().equals("name")) {
people.setenglishname(childpeopleelement.attribute("en").getvalue());
people.setname(childpeopleelement.gettext());
} else if (childpeopleelement.getname().equals("age")) {
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) {
// todo auto-generated catch block
e.printstacktrace();
}
}
}
上一篇: 深入理解Python对Json的解析
推荐阅读
-
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
-
java dom4j解析xml文件代码实例分享