You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
163 lines
4.6 KiB
163 lines
4.6 KiB
/**
|
|
* Copyright (c) 2002 sinosoft Co. Ltd.
|
|
* All right reserved.
|
|
*/
|
|
//*******************************************************************
|
|
// Java XMLPATHTool:
|
|
// Name :
|
|
// Test content:
|
|
// Comment :
|
|
// Date :
|
|
//********************************************************************
|
|
//1.得到document 对象;2.传入xpath表达式3.返回结点集
|
|
package com.sinosoft.utility;
|
|
|
|
import com.sinosoft.print.func.XMLPrintTagName;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.apache.xpath.XPathAPI;
|
|
import org.w3c.dom.Document;
|
|
import org.w3c.dom.Node;
|
|
import org.w3c.dom.NodeList;
|
|
import org.xml.sax.InputSource;
|
|
import org.xml.sax.SAXException;
|
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
import java.io.FileInputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
|
|
/**
|
|
* XPathAPI应用类
|
|
* <p>
|
|
* Title: Life Information System
|
|
* </p>
|
|
* <p>
|
|
* Description:
|
|
* </p>
|
|
* <p>
|
|
* Copyright: Copyright (c) 2002
|
|
* </p>
|
|
* <p>
|
|
* Company:
|
|
* </p>
|
|
*
|
|
* @author Kevin
|
|
* @version 1.0
|
|
*/
|
|
public class XMLPathTool {
|
|
private static final Logger logger = LoggerFactory.getLogger(XMLPathTool.class);
|
|
|
|
/* 对应的Dom树 */
|
|
private Document sourceDom;
|
|
|
|
/* 对应的文件名 */
|
|
private String fileName;
|
|
|
|
|
|
|
|
/**
|
|
* 从一个StringReader对象中产生一个Document
|
|
*
|
|
* @param in
|
|
* InputStream 输入流
|
|
*/
|
|
public XMLPathTool(InputStream in) {
|
|
this.fileName = "";
|
|
try
|
|
{
|
|
DocumentBuilderFactory dfactory = DocumentBuilderFactory.
|
|
newInstance();
|
|
// 拒绝 DOCTYPE
|
|
dfactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
|
// 禁止外部实体
|
|
dfactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
|
|
dfactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
|
|
dfactory.setNamespaceAware(true);
|
|
dfactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
|
dfactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
|
|
dfactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
|
|
dfactory.setXIncludeAware(false);
|
|
dfactory.setExpandEntityReferences(false);
|
|
InputStreamReader strInStream = new InputStreamReader(in, "UTF-8");
|
|
sourceDom = dfactory.newDocumentBuilder().parse(new InputSource(strInStream));
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.error("程序执行异常",ex);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取文档的document对象。
|
|
*
|
|
* @param fileName
|
|
* String 输入的文件名。
|
|
* @return Document 文件名对应的document对象。
|
|
* @throws ParserConfigurationException
|
|
* @throws SAXException
|
|
* @throws IOException
|
|
*/
|
|
private static Document getDocument(String fileName)
|
|
throws ParserConfigurationException, SAXException, IOException {
|
|
try (FileInputStream fis = new FileInputStream(fileName)) {
|
|
InputSource in = new InputSource(fis);
|
|
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
|
|
// 拒绝 DOCTYPE
|
|
dfactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
|
// 禁止外部实体
|
|
dfactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
|
|
dfactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
|
|
dfactory.setNamespaceAware(true);
|
|
Document doc = dfactory.newDocumentBuilder().parse(in);
|
|
return doc;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 分析xpath串。
|
|
*
|
|
* @param xpathString
|
|
* 要进行分析的xpath表达式。
|
|
* @return 结果集。
|
|
*/
|
|
public Node parseX(String xpathString) {
|
|
try {
|
|
if (sourceDom == null) {
|
|
sourceDom = getDocument(fileName);
|
|
}
|
|
// xpathString = XMLPrintTagName.changeROWToR(xpathString);
|
|
return XPathAPI.selectSingleNode(sourceDom.getDocumentElement(),
|
|
xpathString);
|
|
|
|
} catch (Exception ex) {
|
|
logger.error("程序执行异常",ex);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 分析xpath串。
|
|
*
|
|
* @param xpathString
|
|
* 要进行分析的xpath表达式。
|
|
* @return 结果集。
|
|
*/
|
|
public NodeList parseN(String xpathString) {
|
|
try {
|
|
if (sourceDom == null) {
|
|
sourceDom = getDocument(fileName);
|
|
}
|
|
xpathString = XMLPrintTagName.changeROWToR(xpathString);
|
|
return XPathAPI.selectNodeList(sourceDom.getDocumentElement(),
|
|
xpathString);
|
|
|
|
} catch (Exception ex) {
|
|
logger.error("程序执行异常",ex);
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|