我想我找到了我正在寻找的答案: 我想我找到了一个解决方案,以减少代码量和可读性。我已经宣布在一开始的变量:Java编译器正在打印未报告的异常XPathExpressionExeption
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编译器抱怨一个未报告的异常,但我看不到任何声明这个的例子。
如果我更改代码尝试/捕获问题消失,但我不想为每个xpath表达式执行try/catch。
我正在红帽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。”我没有看到你有任何选择。 – EJP
...你觉得这个选项是什么? –
好吧我想我看看发生了什么事,这些例子都有一个声明: public static void main(String [] args)throws异常{ 在类的开始。 我可以做类似的事吗? – user3510073