关于 方式的介绍 基本参考了:
http://blog.csdn.net/dongfengkuayue/article/details/50240157 但感觉仍然不是官方的,以后再跟新
预先的一个xml文件:
<?xmlversion="1.0" encoding="UTF-8" standalone ="yes"?>
<collect>
<queue tableName="aaa"queueName="aQueue" text="a队列">
<columnname="STR_PARAM1" type="VARCHAR2" nullAble="notnull" text="a编码"aName="ID"></column>
<columnname="TIME_PARAM1" type="DATE" nullAble="notnull" text="a时间"aName="Time"></column>
<column name="STR_PARAM2"type="VARCHAR2" nullAble="not null" text="a状态"aName="Status"></column>
</queue>
<queue tableName="bbb"queueName="bQueue" text="b队列">
<columnname="STR_PARAM1" type="VARCHAR2" nullAble="notnull" text="b编码"dcsName="ID"></column>
<column name="TIME_PARAM1"type="DATE" nullAble="not null" text="b时间"dcsName="Time"></column>
<columnname="STR_PARAM2" type="VARCHAR" nullAble="notnull" text="b编码"dcsName="BId"></column>
<columnname="NUM_PARAM1" type="NUMBER" nullAble="defaultnull" text="b次数"dcsName="BCount"></column>
</queue>
</collect>
1.DOM生成和解析XML文档
为 XML文档的已解析版本定义了一组接口。解析器读入整个文档,然后构建一个驻留内存的树结构,
然后代码就可以使用 DOM接口来操作这个树结构。优点:整个文档树在内存中,便于操作;支持删除、修改、重新排列等多种功能;
缺点:将整个文档调入内存(包括无用的节点),浪费时间和空间;
使用场合:一旦解析了文档还需多次访问这些数据;硬件资源充足(内存、CPU)。
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.util.ArrayList;
importjava.util.List;
importjavax.xml.parsers.DocumentBuilder;
importjavax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
importorg.w3c.dom.Document;
importorg.w3c.dom.Node;
importorg.w3c.dom.NodeList;
importorg.xml.sax.SAXException;
/**
*
* <p>ClassName:DomParseQueueConfig</p>
* <p>Description: DOM解析XML文档</p>
* <p>Author: 1</p>
* <p>Date: 2017-5-5</p>
*/
public classDomParseQueueConfig {
/**
* 单例 DomParseQueueConfig 对象
*/
private volatile static DomParseQueueConfigsingleton;
private DomParseQueueConfig() {
}
/**
* <p>Description: 初始化DomParseQueueConfig对象并设置其collectNodeArrayList成员变量</p>
* @param filePath
*/
private DomParseQueueConfig(StringfilePath) {
this.setCollectNodeArrayList(parserXml(filePath));
}
/**
* <p>Description: 单例获取DomParseQueueConfig对象</p>
* @param filePath xml文件路径
* @return DomParseQueueConfig对象
*/
public static DomParseQueueConfiggetSingleton(String filePath) {
if (singleton == null) {
synchronized (DomParseQueueConfig.class){
if (singleton == null) {
singleton = newDomParseQueueConfig(filePath);
}
}
}
return singleton;
}
/**
* 队列节点列表
*/
private List<CollectNode>collectNodeArrayList;
/**
* <p>Description: 解析xml逻辑</p>
* @param filePath 文件目标路径
*/
public List<CollectNode>parserXml(String filePath) {
List<CollectNode>tempCollectNodeArrayList = new ArrayList<CollectNode>();
try {
DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();
DocumentBuilder db =dbf.newDocumentBuilder();
Document document =db.parse(filePath);
NodeList nodeList =document.getChildNodes();
Node manNode = nodeList.item(0);
NodeList collectNodeList =manNode.getChildNodes();
for (int i = 0; i <collectNodeList.getLength(); i++) {
Node queueNode =collectNodeList.item(i);
if (queueNode.hasAttributes()){
CollectNode collectNode =new CollectNode();
collectNode.setQueueName(queueNode.getAttributes().getNamedItem("queueName").getNodeValue());
collectNode.setTableName(queueNode.getAttributes().getNamedItem("tableName").getNodeValue());
collectNode.setText(queueNode.getAttributes().getNamedItem("text").getNodeValue());
NodeList columnNodeList =queueNode.getChildNodes();
List<ColumnNode>columnNodeArrayList = new ArrayList<ColumnNode>();
for (int j = 0; j <columnNodeList.getLength(); j++) {
Node node =columnNodeList.item(j);
if(node.hasAttributes()) {
ColumnNodecolumnNode = new ColumnNode();
columnNode.setName(node.getAttributes().getNamedItem("name").getNodeValue());
columnNode.setType(node.getAttributes().getNamedItem("type").getNodeValue());
columnNode.setNullAble(node.getAttributes().getNamedItem("nullAble").getNodeValue());
columnNode.setText(node.getAttributes().getNamedItem("text").getNodeValue());
columnNode.setDcsName(node.getAttributes().getNamedItem("dcsName").getNodeValue());
columnNodeArrayList.add(columnNode);
}
}
collectNode.setColumnNodeList(columnNodeArrayList);
tempCollectNodeArrayList.add(collectNode);
}
}
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (ParserConfigurationExceptione) {
System.out.println(e.getMessage());
} catch (SAXException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
return tempCollectNodeArrayList;
}
public List<CollectNode>getCollectNodeArrayList() {
return collectNodeArrayList;
}
public void setCollectNodeArrayList(List<CollectNode>collectNodeArrayList) {
this.collectNodeArrayList =collectNodeArrayList;
}
}
2.SAX生成和解析XML文档 (再续)
为解决DOM的问题,出现了SAX。SAX,事件驱动。当解析器发现元素开始、元素结束、文本、文档的开始或结束等时,发送事件,程序员编写响应这些事件的代码,保存数据。
优点:不用事先调入整个文档,占用资源少;SAX解析器代码比DOM解析器代码小,适于Applet,下载。
缺点:不是持久的;事件过后,若没保存数据,那么数据就丢了;无状态性;从事件中只能得到文本,但不知该文本属于哪个元素;
使用场合:Applet;只需XML文档的少量内容,很少回头访问;机器内存少;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SaxParseXml extends DefaultHandler {
//存放遍历集合
private List<CollectNode> list;
//构建Student对象
private CollectNode collectNode;
//用来存放每次遍历后的元素名称(节点名称)
private String tagName;
public List<CollectNode> getList() {
return list;
}
public void setList(List<CollectNode> list) {
this.list = list;
}
public CollectNode getCollectNode() {
return collectNode;
}
public void setStudent(CollectNode collectNode) {
this.collectNode = collectNode;
}
public String getTagName() {
return tagName;
}
public void setTagName(String tagName) {
this.tagName = tagName;
}
//只调用一次 初始化list集合
@Override
public void startDocument() throws SAXException {
list = new ArrayList<CollectNode>();
}
//调用多次 开始解析
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equals("queue")) {
collectNode = new CollectNode();
collectNode.setTableName(attributes.getValue(0));
collectNode.setQueueName(attributes.getValue(1));
collectNode.setColumnNodeList(new ArrayList<ColumnNode>());
}
if (qName.equals("column")) {
ColumnNode columnNode = new ColumnNode();
columnNode.setName(attributes.getValue(0));
columnNode.setType(attributes.getValue(1));
columnNode.setNullAble(attributes.getValue(2));
columnNode.setText(attributes.getValue(3));
columnNode.setDcsName(attributes.getValue(4));
this.collectNode.getColumnNodeList().add(columnNode);
}
this.tagName = qName;
}
//调用多次
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals("queue")) {
this.list.add(this.collectNode);
}
this.tagName = null;
}
//只调用一次
@Override
public void endDocument() throws SAXException {
}
//调用多次
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
}
}
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.helpers.DefaultHandler;
/**
*
* <p>ClassName: SaxParseQueueConfig</p>
* <p>Description: Sax解析XML文档</p>
* <p>Author: 1</p>
* <p>Date: 2017-6-29</p>
*/
public class SaxParseQueueConfig extends DefaultHandler {
/**
* 单例 SaxParseQueueConfig 对象
*/
private volatile static SaxParseQueueConfig singleton;
private SaxParseQueueConfig() {
}
/**
* <p>Description: 初始化SaxParseQueueConfig对象并设置其collectNodeArrayList成员变量</p>
* @param filePath
*/
private SaxParseQueueConfig(String filePath) {
this.setCollectNodeArrayList(parserXml(filePath));
}
/**
* <p>Description: 单例获取SaxParseQueueConfig对象</p>
* @param filePath xml文件路径
* @return SaxParseQueueConfig对象
*/
public static SaxParseQueueConfig getSingleton(String filePath) {
if (singleton == null) {
synchronized (SaxParseQueueConfig.class) {
if (singleton == null) {
singleton = new SaxParseQueueConfig(filePath);
}
}
}
return singleton;
}
/**
* 队列节点列表
*/
private List<CollectNode> collectNodeArrayList;
/**
* <p>Description: 解析xml逻辑</p>
* @param filePath 文件目标路径
*/
public List<CollectNode> parserXml(String filePath) {
List<CollectNode> tempCollectNodeArrayList = new ArrayList<CollectNode>();
SAXParser parser = null;
try {//构建SAXParser
parser = SAXParserFactory.newInstance().newSAXParser();
//实例化 DefaultHandler对象
SaxParseXml parseXml = new SaxParseXml();
//加载资源文件 转化为一个输入流
//调用parse()方法
parser.parse(new File(filePath), parseXml);
//遍历结果
tempCollectNodeArrayList = parseXml.getList();
} catch (Exception e) {
e.printStackTrace();
}
return tempCollectNodeArrayList;
}
public List<CollectNode> getCollectNodeArrayList() {
return collectNodeArrayList;
}
public void setCollectNodeArrayList(List<CollectNode> collectNodeArrayList) {
this.collectNodeArrayList = collectNodeArrayList;
}
}
3.DOM4J生成和解析XML文档 (再续)
DOM4J 是一个非常非常优秀的Java XML API,具有性能优异、功能强大和极端易用使用的特点,同时它也是一个开放源代码的软件。
如今你可以看到越来越多的 Java软件都在使用 DOM4J来读写 XML,特别值得一提的是连 Sun的 JAXM 也在用 DOM4J。
4.JDOM生成和解析XML (再续)
为减少DOM、SAX的编码量,出现了JDOM;优点:20-80原则,极大减少了代码量。
使用场合:要实现的功能简单,如解析、创建等,但在底层,JDOM还是使用SAX(最常用)、DOM、Xanan文档。