利用Dom4j按节点解析xml文件的两种实现方法附代码

XML文件
在项目指定目录下面创建Student.xml文件

比如我的这个xml文件创建位置在:src/main/resources/spring/service/cs/Student.xml


<?xml version="1.0" encoding="UTF-8"?>
<students>
    <student>
        <ID>001</ID>
        <NAME>刘德华</NAME>
        <SEX>男</SEX>
        <AGE>55</AGE>
    </student>
    <student>
        <ID>002</ID>
        <NAME>张学友</NAME>
        <SEX>男</SEX>
        <AGE>56</AGE>
    </student>
</students>

JAVA代码实现方式一:

package com.idbp.usr.cif.cs.impl;
import java.io.File;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Dom4jTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
           //获取xml文件所在位置
            File file = new File("src/main/resources/spring/service/cs/Student.xml");
            SAXReader reader=new SAXReader();
            //读取xml文件到Document中
            Document doc=reader.read(file);
            //获取xml文件的根节点
            Element rootElement=doc.getRootElement();
            //定义一个Element用于遍历
            Element fooElement;
            //遍历所有名叫“VALUE”的节点
            for(Iterator i=rootElement.elementIterator("student");i.hasNext();){
                fooElement=(Element)i.next();
                System.out.println("学号:"+fooElement.elementText("ID"));
                System.out.println("姓名:"+fooElement.elementText("NAME"));
                System.out.println("性别:"+fooElement.elementText("SEX"));
                System.out.println("年龄:"+fooElement.elementText("AGE"));
            }
            System.out.println(rootElement.toString());
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
       
    }

}

 

JAVA代码实现方式二:

package com.idbp.usr.cif.cs.impl;
import java.io.File;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class Dom4j {
 /**
  * 递归遍历所有父节点、子节点
  * @param ele
  */
 public static void parserNode(Element ele){
  String element=ele.getName()+":"+ele.getText().trim();
  System.out.println(element);
  //从根节点开始遍历,像【属性=值】的形式存为一个Attribute对象存储在List集合中
  List<Attribute> attrList = ele.attributes();
  for(Attribute attr : attrList){
   //每循环一次,解析此节点的一个【属性=值】,没有则输出空
   String name = attr.getName();
   String value = attr.getValue();
   System.out.println(name+"="+value);
  }
  List<Element> eleList = ele.elements();
  //递归遍历父节点下的所有子节点
  for(Element e : eleList){
   parserNode(e);
  }
 }
 public static void main(String[] args) {
  SAXReader saxReader = new SAXReader();
  try {
   Document document = saxReader.read(new File("src/main/resources/spring/service/cs/Student.xml"));
   Element ele = document.getRootElement();
   parserNode(ele);
  } catch (DocumentException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

注意:在整个代码过程中一定要dom4j对应的jar包,以及类包的正确运用!

猜你喜欢

转载自blog.csdn.net/weixin_41648325/article/details/81136440