【发布时间】:2014-04-11 13:41:34
【问题描述】:
我想我已经找到了我正在寻找的答案: 我想我已经找到了一个解决方案,以减少代码量和可读性。我已经在开始时声明了变量:
XPathExpression newExpression;
然后创建了一个我可以调用的方法:
XPathExpression exprNS = exprNSCreate("/LVOBSLSTR/NameSpace");
public XPathExpression exprNSCreate(String pathToCompile) {
try {
newExpression = xpath.compile(pathToCompile);
} catch (Exception e) {
System.out.println("XPathExpressionException Error " + e.getMessage());
e.printStackTrace();
}
return newExpression;
}
enter code here
我好多年没用java了,一直在写这段代码,还远远不够。它要做的是将传出和传入的 XML 更改到我们的系统。原因是系统是通过规则引擎编写的,并且对可变长度有一些限制,所以我们正在修复系统之外的一些东西。
我在任何地方都找不到关于我的问题的参考。问题是 java 编译器抱怨一个未报告的异常,但我看不到任何声明这个的例子。
如果我将代码更改为 try/catch,问题就会消失,但我不想为每个 xpath 表达式执行 try/catch。
我正在一个 red-hat linux 机器上编译。
有人可以指出我正确的方向吗? 提前致谢。
这是我得到的错误:
LVOBSLSTR.java:58: unreported exception javax.xml.xpath.XPathExpressionException; must be caught or declared to be thrown
XPathExpression exprNS = xpath.compile("/LVOBSLSTR/NameSpace");
^
LVOBSLSTR.java:59: unreported exception javax.xml.xpath.XPathExpressionException; must be caught or declared to be thrown
NodeList listNS = (NodeList) exprNS.evaluate(paramDoc, XPathConstants.NODESET);
这是代码:
/**
* The LVOBSLSTR class implements the OBSLSTR class
* it applies XML data translation dependant on the party
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathConstants;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import java.util.ArrayList;
class LVOBSLSTR implements OBSLSTR {
Document paramDoc;
ArrayList<Object> instructionList;
Element nodeElement;
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
public boolean activate() {
System.out.println("boolean activate");
if(null==instructionList) {
System.out.println("!!! instructionList NULL creating one !!!");
instructionList = new ArrayList<Object>();
loadXMLparameterFile();
System.out.println("AFTER loadXMLparameterFile");
}
return true;
}
public void beforePost() {
System.out.println("beforePost");
OUTBSOAP.statMessageBuffer = applyInstructions(OUTBSOAP.statMessageBuffer, "OutBound");
}
public void afterPost() {
System.out.println("afterPost");
OUTBSOAP.statReplyMessage = applyInstructions(OUTBSOAP.statReplyMessage, "InBound");
}
public String applyInstructions(String xmlString, String inOrOut) {
XPathExpression exprNS = xpath.compile("/LVOBSLSTR/NameSpace");
NodeList listNS = (NodeList) exprNS.evaluate(paramDoc, XPathConstants.NODESET);
return xmlString;
}
public void loadXMLparameterFile() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
System.out.println("try");
DocumentBuilder builder = factory.newDocumentBuilder();
FileInputStream fis = new FileInputStream("LVOBSLSTR.xml");
InputSource is = new InputSource(fis);
paramDoc = builder.parse(is);
} catch (Exception e) {
System.out.println("LVOBSLSTR loadXMLparameterFile Error " + e.getMessage());
e.printStackTrace();
}
}
}
【问题讨论】:
-
"我不想为每个 xpath 表达式做 try/catch。"我看你别无选择。
-
...你认为选项是什么?
-
好吧,我想我明白发生了什么,这些例子都有一个类似的声明: public static void main(String[] args) throws Exception { 在类的开始。我可以做类似的事情吗?
-
@user3510073 当然,如果您不关心例外情况。
-
@user3510073 - 关于您上面的编辑,您仍然需要在任何时候从另一个方法调用“exprNSCreate(String pathToCompile)”时处理 XPathExpressionException,因为您将在调用中引用 XPathExpression方法。
标签: java xml exception xpath compilation