【发布时间】:2020-01-12 02:30:26
【问题描述】:
我正在尝试编写使用 Saxon xPath 的 Java 代码。我有两个问题:
- 我的java不太好
- 我不确定将 net.sf.saxon.om.NodeInfo 转换为 String 的最佳方法是什么。
有人可以帮忙吗?我知道http://www.saxonica.com/download/download_page.xml 有一些很好的示例代码,但这还不够。
我看到了类似的 SO 讨论 XPath processor output as string .但是在这种情况下,我想使用 Saxon,它使用 NodeInfo。
<pre>
<!-- language: java -->
public class helloSaxon {
public static void main(String[] args) {
String xml = "";
String xPathStatement = "";
String xPathResult = "";
SaxonXPath xPathEvaluation = null;
Boolean xPathResultMatch = false;
xml="<root><a version = '1.0' encoding = 'UTF-8'>#BBB#</a><a>#CCC#</a><b><a>#DDD#</a></b></root>";
//I'm using the following XPath Tester for test scenarios
//https://www.freeformatter.com/xpath-tester.html#ad-output
// Test #1
xPathStatement="/root/a";
xPathEvaluation = new SaxonXPath(xml, xPathStatement);
xPathResult = xPathEvaluation.getxPathResult();
System.out.println("Test #1 xPathResult - " + xPathResult);
//xPathResult == "<a version = '1.0' encoding = 'UTF-8'>#BBB#</a><a>#CCC#</a>";
xPathResultMatch = xPathEvaluation.getxPathResultMatch();
System.out.println("Test #1 xPathResultMatch - " + xPathResultMatch);
//xPathResultMatch == true;
// Test #2
xPathStatement="//a";
xPathEvaluation.Reset(xml, xPathStatement);
xPathResult = xPathEvaluation.getxPathResult();
System.out.println("Test #2 xPathResult - " + xPathResult);
//xPathResult == "<a version = '1.0' encoding = 'UTF-8'>#BBB#</a><a>#CCC#</a><a>#DDD#</a>";
xPathResultMatch = xPathEvaluation.getxPathResultMatch();
System.out.println("Test #2 xPathResultMatch - " + xPathResultMatch);
//xPathResultMatch == true;
// Test #3
xPathStatement="/root/a[1]/text()";
xPathEvaluation.Reset(xml, xPathStatement);
xPathResult = xPathEvaluation.getxPathResult();
System.out.println("Test #3 xPathResult - " + xPathResult);
//xPathResult == "#BBB#";
xPathResultMatch = xPathEvaluation.getxPathResultMatch();
System.out.println("Test #3 xPathResultMatch - " + xPathResultMatch);
//xPathResultMatch == true;
// Test #4
xPathStatement="/a/root/a/text()";
xPathEvaluation.Reset(xml, xPathStatement);
xPathResult = xPathEvaluation.getxPathResult();
System.out.println("Test #4 xPathResult - " + xPathResult);
//xPathResult == "";
xPathResultMatch = xPathEvaluation.getxPathResultMatch();
System.out.println("Test #4 xPathResultMatch - " + xPathResultMatch);
//xPathResultMatch == false;
// Test #5
xPathStatement="/root";
xPathEvaluation.Reset(xml, xPathStatement);
xPathResult = xPathEvaluation.getxPathResult();
System.out.println("Test #5 xPathResult - " + xPathResult);
//xPathResult == "<root><a version = '1.0' encoding = 'UTF-8'>#BBB#</a><a>#CCC#</a><b><a>#DDD#</a></b></root>";
xPathResultMatch = xPathEvaluation.getxPathResultMatch();
System.out.println("Test #5 xPathResultMatch - " + xPathResultMatch);
//xPathResultMatch == true;
}
static class SaxonXPath{
private String xml;
private String xPathStatement;
private String xPathResult;
private Boolean xPathResultMatch;
public SaxonXPath(String xml, String xPathStatement){
this.Reset(xml, xPathStatement);
}
public void Reset(String xml, String xPathStatement){
this.xml = xml;
this.xPathStatement = xPathStatement;
this.xPathResult = "";
this.xPathResultMatch = null;
this.Evaluate();
}
public void Evaluate(){
try{
System.setProperty("javax.xml.xpath.XPathFactory:" + NamespaceConstant.OBJECT_MODEL_SAXON, "net.sf.saxon.xpath.XPathFactoryImpl");
XPathFactory xPathFactory = XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_SAXON);
XPath xPath = xPathFactory.newXPath();
InputSource inputSource = new InputSource(new StringReader(this.xml));
SAXSource saxSource = new SAXSource(inputSource);
Configuration config = ((XPathFactoryImpl) xPathFactory).getConfiguration();
DocumentInfo document = config.buildDocument(saxSource);
XPathExpression xPathExpression = xPath.compile(this.xPathStatement);
List matches = (List) xPathExpression.evaluate(document, XPathConstants.NODESET);
if (matches != null && matches.size()>0) {
this.xPathResultMatch = true;
for (Iterator iter = matches.iterator(); iter.hasNext();) {
NodeInfo node = (NodeInfo) iter.next();
//need to convert content of "node" to string
xPathResult += node.getStringValue();
}
} else {
this.xPathResultMatch = false;
}
} catch(Exception e){
e.printStackTrace();
}
}
public String getxPathResult(){
return this.xPathResult;
}
public Boolean getxPathResultMatch(){
return this.xPathResultMatch;
}
}
}
</code>
会有以下输入:
- XML 作为字符串
- xPath 表达式为字符串
输出: - xPath 评估为字符串
- xPath 结果匹配为布尔值
我还在代码 cmets 中添加了一些测试示例,以便您更好地理解我要做什么。
【问题讨论】: