我有以下代碼傳遞一個StringBuilder對文檔
private void getdata(String stringfromDate,String stringtoDate) throws IOException {
StringBuilder retVal = new StringBuilder();
URL oracle = new URL("https://xxxxxxxxxxxxxxxx" + stringfromDate +"&ToDate=" + stringtoDate +"");
BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));
String newLine = "\n";
String inputLine;
while ((inputLine = in.readLine()) != null) {
//System.out.println(inputLine);
retVal.append(inputLine).append(newLine);
}
in.close();
passSting(retVal);
}
private void passSting(StringBuilder retVal) {
Document doc = null;
try {
doc = loadXMLFromString(retVal.toString());//pull in the XML data into a new doc
System.out.println(doc);
} catch (Exception ex) {
Logger.getLogger(JavaApplication63.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static org.w3c.dom.Document loadXMLFromString(String xml) throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
return builder.parse(is);
}
我穿過retVal的一種方法,這樣我可以創建可讀取作爲XML文檔一個文檔,但是該文檔值出現爲什麼我做錯了?這是我在控制檯窗口中獲得的內容[#document:null]
你確定'StringBuilder'中有東西嗎?你在'while'中有一個打印語句,所以我假設你測試了這個部分。 – gonzo
是的,如果我把鼠標懸停在retVal.toString()上面,我會看到預期的數據......它只是說文檔看起來是空的...... – Ingram
如果你在創建''factory.setNamespaceAware(true) builder'。 – gonzo