Home  >  Article  >  Backend Development  >  Four ways to read xml files using dom4j

Four ways to read xml files using dom4j

尚
forward
2020-03-21 17:26:446266browse

Four ways to read xml files using dom4j

The following are four ways to read xml files, each with its own use. This is what I found when I was writing a log manager. Hope it helps everyone.

First we give a simple xml file

<?xml version="1.0" ?>
<ROWDATA>

<ROW>
  <C0>1</C0>
  <EMPNO>7891</EMPNO>
  <ENAME>sdffff</ENAME>
  <JOB>job</JOB>
  <MGR></MGR>
  <HIREDATE>2010-1-1</HIREDATE>
  <SAL>5000.00</SAL>
  <COMM>1000.00</COMM>
  <DEPTNO></DEPTNO>
</ROW>

<ROW>
  <C0>2</C0>
  <EMPNO>7369</EMPNO>
  <ENAME>SMITH</ENAME>
  <JOB>CLERK</JOB>
  <MGR>7902</MGR>
  <HIREDATE>1980-12-17</HIREDATE>
  <SAL>800.00</SAL>
  <COMM></COMM>
  <DEPTNO>20</DEPTNO>
</ROW>

</ROWDATA>

The first type:

/**
  * 使用dom4j 中saxreader 获取Document容器,利用此容器的elementIterator读取xml文件
  */
 public static void readXML() throws DocumentException{
  
  SAXReader sr = new SAXReader();//获取读取xml的对象。
  Document doc = sr.read("src/com/sinojava/EMP.xml");//得到xml所在位置。然后开始读取。并将数据放入doc中
  Element el_root = doc.getRootElement();//向外取数据,获取xml的根节点。
  Iterator it = el_root.elementIterator();//从根节点下依次遍历,获取根节点下所有子节点
  
  while(it.hasNext()){//遍历该子节点
   
   Object o = it.next();//再获取该子节点下的子节点
   Element el_row = (Element)o;
   String s = el_row.getText();

   Iterator it_row = el_row.elementIterator();
   
   while(it_row.hasNext()){//遍历节点
    
    Element el_ename = (Element)it_row.next();//获取该节点下的所有数据。
    System.out.println(el_ename.getText());
   }
   //System.out.println(o);
  }
  
 }

The second type:;

/**
  * 使用elements方法进行xml的读取,相当于条件查询,可以根据不同的节点,利用for循环查询该节点下所有的数据。
  * @throws DocumentException
  */
 public static void readXML02() throws DocumentException{
  
  SAXReader sr = new SAXReader();//获取读取方式
  Document doc = sr.read("src/com/sinojava/EMP.xml");//读取xml文件,并且将数据全部存放到Document中
  Element root = doc.getRootElement();//获取根节点
  
  List list = root.elements("ROW");//根据根节点,将根节点下 row中的所有数据放到list容器中。
  for(Object obj:list){//这种遍历方式,是jdk1.5以上的版本支持的遍历方式
   Element row = (Element)obj;
   List list_row = row.elements("ENAME");//获取ENAME节点下所有的内容,存入list_row容器中
   
   for(Object objempno:list_row){
    
    Element el_empno = (Element)objempno;
    
    System.out.println(el_empno.getName()+": "+el_empno.getText());//获取节点下的数据。
    
   }
  }
 }

The third type:

/**
  * 使用适配器来完成xml的读取。
  * @param args
  * @throws DocumentException
  */
 public static void readXML04() throws DocumentException{
  
  SAXReader sr = new SAXReader();
  Document doc = sr.read("src/com/sinojava/EMP.xml");
  
  doc.accept(new VisitorSupport() {//使用观察器的子类,来完成对xml文件的读取。
   
   public void visit(Element el) {//利用观察期进行xml的读取。
    
    System.out.println(el.getName()+": "+el.getText());

   }
   
  });
 }

The fourth type:

/**
  * 使用selectNodes读取xml文件
  * @param args
  * @throws DocumentException
  */
 public static void readXML05(String elementpath) throws DocumentException{
  
  SAXReader sr = new SAXReader();
  Document doc = sr.read("src/com/sinojava/EMP.xml");
  
  List list = doc.selectNodes(elementpath);//使用selectNodes获取所要查询xml的节点。
  
  for(Object obj:list){//遍历节点,获取节点内数据。
   
   Element el = (Element)obj;
   System.out.println(el.getText());
  }
  
 }

For more related questions, please visit the PHP Chinese website: XML Video Tutorial

The above is the detailed content of Four ways to read xml files using dom4j. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete