我有这个功能:
private Node getDOM(String str) throws SearchEngineException {
DOMResult result = new DOMResult();
try {
XMLReader reader = new Parser();
reader.setFeature(Parser.namespacesFeature, false);
reader.setFeature(Parser.namespacePrefixesFeature, false);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(new SAXSource(reader,new InputSource(new StringReader(str))), result);
} catch (Exception ex) {
throw new SearchEngineException("NukatSearchEngine.getDom: " + ex.getMessage());
}
return result.getNode();
}
它需要一个String,它包含一个POST请求后由http服务器发送的html文档,但无法正确解析它 – 我只得到整个文档中的四个节点.字符串本身看起来很好 – 如果我将其打印出来并将其复制到文本文档中,我会看到我期望的页面.
当我使用上述方法的重载版本时:
private Node getDOM(URL url) throws SearchEngineException {
DOMResult result = new DOMResult();
try {
XMLReader reader = new Parser();
reader.setFeature(Parser.namespacesFeature, false);
reader.setFeature(Parser.namespacePrefixesFeature, false);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(new SAXSource(reader, new InputSource(url.openStream())), result);
} catch (Exception ex) {
throw new SearchEngineException("NukatSearchEngine.getDom: " + ex.getMessage());
}
return result.getNode();
}
然后一切正常 – 我得到一个合适的DOM树,但我需要以某种方式从服务器检索POST答案.
将字符串存储在文件中并将其读回不起作用 – 仍然得到相同的结果.
可能是什么问题呢?