195 lines
5.1 KiB
Java
195 lines
5.1 KiB
Java
package com.amarsoft.aims.util;
|
||
|
||
import java.io.ByteArrayInputStream;
|
||
import java.io.File;
|
||
import java.io.IOException;
|
||
import java.io.InputStream;
|
||
import java.io.StringWriter;
|
||
|
||
import javax.xml.parsers.DocumentBuilder;
|
||
import javax.xml.parsers.DocumentBuilderFactory;
|
||
import javax.xml.parsers.ParserConfigurationException;
|
||
import javax.xml.transform.OutputKeys;
|
||
import javax.xml.transform.Transformer;
|
||
import javax.xml.transform.TransformerConfigurationException;
|
||
import javax.xml.transform.TransformerException;
|
||
import javax.xml.transform.TransformerFactory;
|
||
import javax.xml.transform.dom.DOMSource;
|
||
import javax.xml.transform.stream.StreamResult;
|
||
import javax.xml.xpath.XPath;
|
||
import javax.xml.xpath.XPathConstants;
|
||
import javax.xml.xpath.XPathExpression;
|
||
import javax.xml.xpath.XPathExpressionException;
|
||
import javax.xml.xpath.XPathFactory;
|
||
|
||
import org.w3c.dom.Document;
|
||
import org.w3c.dom.Element;
|
||
import org.w3c.dom.Node;
|
||
import org.w3c.dom.NodeList;
|
||
import org.xml.sax.SAXException;
|
||
|
||
import com.amarsoft.are.ARE;
|
||
import com.amarsoft.are.log.Log;
|
||
|
||
/**
|
||
* 使用XPath的XML对象
|
||
* @author yangsong
|
||
* @date 2013/11/11
|
||
*/
|
||
public class XMLParse{
|
||
public static final String DEFAULT_CHARSET = "GBK";
|
||
private Log log = null;
|
||
private Document document;
|
||
|
||
public XMLParse(){
|
||
this(ARE.getLog());
|
||
}
|
||
public XMLParse(Log log){
|
||
setLog(log);
|
||
}
|
||
public void setLog(Log log){
|
||
log = ARE.getLog();
|
||
}
|
||
/**
|
||
* 构造函数
|
||
* @param file
|
||
* @param xPathExpr
|
||
*/
|
||
public XMLParse(File file) {
|
||
this();
|
||
// 1.得到DOM解析器的工厂实例
|
||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||
try {
|
||
DocumentBuilder builder = factory.newDocumentBuilder();
|
||
document = builder.parse(file);
|
||
} catch (SAXException e) {
|
||
log.error("SAXException",e);
|
||
} catch (IOException e) {
|
||
log.error("IOException",e);
|
||
} catch (ParserConfigurationException e) {
|
||
log.error("ParserConfigurationException",e);
|
||
}
|
||
}
|
||
/**
|
||
* 构造函数
|
||
* @param file
|
||
* @param xPathExpr
|
||
*/
|
||
public XMLParse(InputStream in) {
|
||
this();
|
||
// 1.得到DOM解析器的工厂实例
|
||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||
try {
|
||
DocumentBuilder builder = factory.newDocumentBuilder();
|
||
document = builder.parse(in);
|
||
} catch (SAXException e) {
|
||
log.error("SAXException",e);
|
||
} catch (IOException e) {
|
||
log.error("IOException",e);
|
||
} catch (ParserConfigurationException e) {
|
||
log.error("ParserConfigurationException",e);
|
||
}
|
||
}
|
||
/**
|
||
* 使用XML字串构建一个对象
|
||
* @param xmlString
|
||
*/
|
||
public XMLParse(String xmlString){
|
||
this();
|
||
// 1.得到DOM解析器的工厂实例
|
||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||
try {
|
||
DocumentBuilder builder = factory.newDocumentBuilder();
|
||
InputStream inputStream = new ByteArrayInputStream(xmlString.getBytes());//xml为要解析的字符串
|
||
document = builder.parse(inputStream);
|
||
} catch (SAXException e) {
|
||
log.error("SAXException",e);
|
||
} catch (IOException e) {
|
||
log.error("IOException",e);
|
||
} catch (ParserConfigurationException e) {
|
||
log.error("ParserConfigurationException",e);
|
||
}
|
||
}
|
||
/**
|
||
* 根据XPath表达式,取节点列表
|
||
* @param xPathExpr
|
||
* @return
|
||
*/
|
||
public NodeList getNodeList(String xPathExpr){
|
||
NodeList nodeList = null;
|
||
try {
|
||
XPathFactory xFactory = XPathFactory.newInstance();
|
||
XPath xPath = xFactory.newXPath();
|
||
XPathExpression xExpr = xPath.compile(xPathExpr);
|
||
nodeList = (NodeList)xExpr.evaluate(document,XPathConstants.NODESET);
|
||
} catch (XPathExpressionException e) {
|
||
log.error("XPathExpressionException",e);
|
||
}
|
||
return nodeList;
|
||
}
|
||
/**
|
||
* 根据XPath表达式,取一个节点,如有多个,则返回第一个
|
||
* @param xPathExpr
|
||
* @return
|
||
*/
|
||
public Node getNode(String xPathExpr){
|
||
NodeList nodeList = getNodeList(xPathExpr);
|
||
if(nodeList.getLength()>0){
|
||
return nodeList.item(0);
|
||
}else{
|
||
return null;
|
||
}
|
||
}
|
||
/**
|
||
* 根据XPath表达式,取一个节点,如有多个,则返回第一个
|
||
* @param xPathExpr
|
||
* @return
|
||
*/
|
||
public Element getElement(String xPathExpr){
|
||
return (Element)getNode(xPathExpr);
|
||
}
|
||
/**
|
||
* 获取一个XML片段
|
||
* @param xPathExpr
|
||
* @param charset
|
||
* @return
|
||
*/
|
||
public String getXMLString(String xPathExpr, String charset) {
|
||
String xmlString = null;
|
||
Node node = getNode(xPathExpr);
|
||
if(node==null)return xmlString;
|
||
try {
|
||
TransformerFactory factory = TransformerFactory.newInstance();
|
||
Transformer transformer;
|
||
transformer = factory.newTransformer();
|
||
DOMSource source = new DOMSource(node);
|
||
transformer.setOutputProperty(OutputKeys.ENCODING, charset);
|
||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||
StringWriter writer = new StringWriter();
|
||
StreamResult result = new StreamResult(writer);
|
||
transformer.transform(source, result);
|
||
xmlString = writer.toString();
|
||
} catch (TransformerConfigurationException e) {
|
||
log.error("TransformerConfigurationException", e);
|
||
} catch (TransformerException e) {
|
||
log.error("TransformerException", e);
|
||
}
|
||
return xmlString;
|
||
}
|
||
/**
|
||
* 取一个XML片段,默认使用UTF-8
|
||
* @param xPathExpr
|
||
* @return
|
||
*/
|
||
public String getXMLString(String xPathExpr){
|
||
return getXMLString(xPathExpr,DEFAULT_CHARSET);
|
||
}
|
||
/**
|
||
* 返回文档对象
|
||
* @return
|
||
*/
|
||
public Document getDocument(){
|
||
return this.document;
|
||
}
|
||
}
|