我試圖將XML字符串解析爲可用於輕鬆搜索的文檔。但是當我遇到某種類型的XML時,它似乎不起作用。該文檔從不構造,並且在遇到像我在底部的XML消息時爲空。一個錯誤時拋出是不是在我的try/catch拋出的任何使用Java解析XSI
我的代碼目前看起來是這樣的:
Document convertMessageToDoc(String message){
Document doc = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(message));
doc = db.parse(is);
}
catch (Exception e) {
//e.printStackTrace();
doc = null;
}
return doc;
}
有什麼辦法,我將能夠與這樣的工作:
<ns1:SubmitFNOLResponse xmlns:ns1="http://website.com/">
<ns1:FNOLReporting xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns1:FNOLReporting">
<ns1:FNOLResponse>
<ns1:FNOLStatusInfo>
<ns1:StatusCode>0</ns1:StatusCode>
<ns1:StatusMessages />
</ns1:FNOLStatusInfo>
</ns1:FNOLResponse>
</ns1:FNOLReporting>
</ns1:SubmitFNOLResponse>
你能定義「不行」嗎? –
我已澄清我的問題 – user906153
它真的沒有封閉標籤嗎?從技術上講,我不確定它會保持原樣。沒有例外,因爲當有一個時,你什麼都不做。 –