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();
}
}
}
相关文章
一篇文章带你搞定SpringBoot不重启项目实现修改静态资源
这篇文章主要介绍了一篇文章带你搞定SpringBoot不重启项目实现修改静态资源,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-09-09Mybatis-Plus进阶分页与乐观锁插件及通用枚举和多数据源详解
这篇文章主要介绍了Mybatis-Plus的分页插件与乐观锁插件还有通用枚举和多数据源的相关介绍,文中代码附有详细的注释,感兴趣的朋友来看看吧2022-03-03
最新评论