我有以下代码
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文档读取的文档,但是doc值似乎为null我做错了什么?这是我在控制台窗口[#document:null]
中得到的答案 0 :(得分:1)
org.w3c.dom.Document#toString
方法返回null
。但实际对象不是null
。如果您在解析doc==null
后打印String
,则会看到它返回false
而不是true
。希望这可以帮助。