我想我找到了我正在尋找的答案: 我想我找到了一個解決方案,以減少代碼量和可讀性。我已經宣佈在一開始的變量: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