一:导入依赖
<dependency><groupId>dom4j</groupId><artifactId>dom4j</artifactId><version>1.6.1</version>
</dependency>
假设xml文件内容如下
<?xml version="1.0" encoding="utf-8"?><data><head name="EASYSCAN" version="3.3.4"><action>1009</action><sessionid>72a9825f0f6d4d2eb732d714fb205188</sessionid></head><body><uploadbatch uniqueid="48e5606521ce459f8128848b8171eb17"><group caseno="202009071001"><doc doccode="202009071001"></doc></group></uploadbatch></body></data>
1:获取根元素
public class Readxml {public static void main(String[] args) {SAXReader saxReader = new SAXReader();try {Document document = saxReader.read(new File("D:\\saxreader.xml"));Element rootElement = document.getRootElement();System.out.println("name:" + rootElement.getName());System.out.println("data:" + rootElement.getText());} catch (DocumentException e) {e.printStackTrace();}}
}
结果:
根元素为data,没有值,因为data里面全是元素节点
2:遍历所有子元素
public class Readxml {public static void main(String[] args) {SAXReader saxReader = new SAXReader();try {Document document = saxReader.read(new File("D:\\saxreader.xml"));Element rootElement = document.getRootElement();System.out.println("name:" + rootElement.getName());System.out.println("data:" + rootElement.getText());System.out.println("--------------");Iterator<Element> iterator = rootElement.elementIterator();while (iterator.hasNext()) {Element next = iterator.next();System.out.println(next.getName() + ":" + next.getText());}} catch (DocumentException e) {e.printStackTrace();}}
}
结果:
根元素data下有两个子节点:head,body
3:只要其中一个子元素并取属性值(一)
public class Readxml {public static void main(String[] args) {SAXReader saxReader = new SAXReader();try {Document document = saxReader.read(new File("D:\\saxreader.xml"));Element rootElement = document.getRootElement();System.out.println("name:" + rootElement.getName());System.out.println("data:" + rootElement.getText());System.out.println("--------------");//获取跟节点下所有head节点Iterator<Element> iterator = rootElement.elementIterator("head");while (iterator.hasNext()) {//next 指的是head ,因为xml文件跟节点下可能不止一个head节点Element next = iterator.next();System.out.println(next.getName());System.out.println("head的属性name的值:" + next.attribute("name").getValue());//获取head下所有action节点Iterator<Element> actionElement = next.elementIterator("action");while (actionElement.hasNext()) {Element action = actionElement.next();System.out.println("head节点下的"+action.getName() + "节点的值:" + action.getText());}}} catch (DocumentException e) {e.printStackTrace();}}
}
4:只要其中一个子元素并取属性值(二)
public class Readxml {public static void main(String[] args) {SAXReader saxReader = new SAXReader();try {Document document = saxReader.read(new File("D:\\saxreader.xml"));Element rootElement = document.getRootElement();System.out.println("name:" + rootElement.getName());System.out.println("data:" + rootElement.getText());System.out.println("--------------");List<Element> headList = rootElement.selectNodes("head");if(headList.size()>0){Element head = headList.get(0);System.out.println(head.getName());System.out.println(head.getText());}else{System.out.println("xml文件有误");}} catch (DocumentException e) {e.printStackTrace();}}
}
错误是缺少依赖
<dependency><groupId>jaxen</groupId><artifactId>jaxen</artifactId><version>1.1.1</version></dependency>
再次运行
public class Readxml {public static void main(String[] args) {SAXReader saxReader = new SAXReader();try {Document document = saxReader.read(new File("D:\\saxreader.xml"));Element rootElement = document.getRootElement();System.out.println("name:" + rootElement.getName());System.out.println("data:" + rootElement.getText());System.out.println("--------------");List<Element> headList = rootElement.selectNodes("head");if(headList.size()>0){Element head = headList.get(0);System.out.println(head.getName());System.out.println(head.getText());System.out.println("head的name属性的值:"+head.attribute("name").getValue());List<Element> actionList = head.selectNodes("action");if(actionList.size()>0){Element action = actionList.get(0);System.out.println("head下节点"+action.getName()+"的值:"+action.getText());System.out.println();}}else{System.out.println("xml文件有误");}} catch (DocumentException e) {e.printStackTrace();}}
}
selectNodes重要语法可自行百度
这里讲解一下selectNodes(“a”)和selectNodes("/a")的区别
element.selectNodes(“a”):从当前节点的儿子节点中选择名称为 a 的所有节点。
element.selectNodes("/a"):获取根节点a下的所有子节点,这里a为根节点
结合本xml,应该这样(data为根节点)
List list = rootElement.selectNodes("/data/head");
System.out.println(list.size());
System.out.println(list.get(0).getName());
结果: