使用Dom4j操作XML

Dom4j也可以很方便完成XML文档的创建、元素的修改、文档的查询遍历等,但dom4j稍比jdom复杂一点,不过在大片文档的情况下dom4j的性能要不jdom好。

# 准备

首先,提供相关的jar包

Dom4j jar包下载:

http://sourceforge.net/projects/dom4j/files/dom4j-2.0.0-ALPHA-2/

jaxen jar下载:

http://repo1.maven.org/maven2/jaxen/jaxen/1.1.1/jaxen-1.1.1.jar

和dom4j依赖或相关的jar:

http://dom4j.sourceforge.net/dependencies.html

Junit-jar下载:

http://ebr.springsource.com/repository/app/bundle/version/download?name=com.springsource.org.junit&version=4.8.1&type=binary

##################################################################################################################

不使用vistor方式自己封装的Dom4j工具类:

package com.unionpay.upa.importer;

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.DOMReader;
import org.dom4j.io.SAXReader;
import org.xml.sax.SAXException;

public class Dom4jReader {

 private static Dom4jReader instance = null;

 // SAX
 public Element getRootElementBySAXPattern(String fileName)
   throws DocumentException {

  SAXReader saxReader = new SAXReader();
  Document doc = saxReader.read(new File(fileName));
  return doc.getRootElement();

 }

 // DOM
 public Element getRootElmentByDomPattern(String fileName)
   throws ParserConfigurationException, SAXException, IOException {

  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  File file = new File(fileName);
  org.w3c.dom.Document document = db.parse(file);
  DOMReader domReader = new DOMReader();
  Document doc = domReader.read(document);
  return doc.getRootElement();
 }

 // get instance
 public static Dom4jReader getInstance() {
  synchronized (Dom4jReader.class) {

   if (instance == null) {

    instance = new Dom4jReader();

   }

  }

  return instance;
 }
}
########################################################################################

使用dom4j的vistor方式代码:

1.MyVisitor.java

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.VisitorSupport;


public class MyVisitor extends VisitorSupport {

 @Override
 public void visit(Attribute node) {
  System.out.println("属性:"+node.getName()+"="+node.getText()); 
 }

 @Override
 public void visit(Document document) {
  // TODO Auto-generated method stub
  super.visit(document);
 }

 @Override
 public void visit(Element node) {
  //node.elementTextTrim("");
   System.out.println("节点: "+node.getName()+"="+node.getTextTrim());
  
   if("mappingNum".equals(node.getName())){
    int mappingNum = Integer.parseInt(node.getTextTrim());
   }else if("logFileColumn".equals(node.getName())){
    String logFileColumn = node.getTextTrim();
   }else if("tableName".equals(node.getName())){
    String tableName = node.getTextTrim();
   }else if("tableColumnParam".equals(node.getName())){
    String tableColumnParam = node.getTextTrim();
   }
  
  
 }
 

}
2.VisitorXML.java

import java.io.File;

import org.dom4j.Document;
import org.dom4j.io.SAXReader;


public class VisitorXML {
 
  public static void readFile(String fileName) throws Exception{ 
          
          SAXReader reader = new SAXReader() ; 
          
          Document document = reader.read(new File(fileName)) ; 
        
         //通过访问者自动遍历节点 
          document.accept(new MyVisitor()) ; 
      }
 
  public static void main(String[] args) {
   try {
   readFile("src/dataImport.xml");
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } 
 }

}

猜你喜欢

转载自chentianliang.iteye.com/blog/2113731