gpt4 book ai didi

java - 将字符串生成器传递给文档

转载 作者:行者123 更新时间:2023-12-01 10:46:09 25 4
gpt4 key购买 nike

我有以下代码

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 文档读取的文档,但是该文档值似乎为 null 我做错了什么?这是我在控制台窗口中得到的内容 [#document: null]

最佳答案

org.w3c.dom.Document#toString 方法返回 null。然而,实际对象不是null。如果您在解析 String 后打印 doc==null,您应该会看到它返回 false 而不是 true。希望这可以帮助。

关于java - 将字符串生成器传递给文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34166732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com