Yakamoz 2014-09-05
# 准备
首先,提供相关的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下载:
##################################################################################################################
不使用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();
}
}
}