1

我正在尝试打开与网站的 HTTP 连接并将 html 解析为一个org.w3c.dom.Document类。我可以打开 HTTP 连接并将网页输出到控制台就好了,但是如果我将 InputStream 对象传递给 XML 解析器,它会挂起一分钟并输出错误

[Fatal Error] :108:55: Open quote is expected for attribute "{1}" associated with an  element type  "onload".

代码:

private static Document getInputStream(String url) throws IOException, SAXException, ParserConfigurationException
{
  System.out.println(url);
  URL webUrl = new URL(url);
  URLConnection connection = webUrl.openConnection();
  connection.setConnectTimeout(60 * 1000);
  connection.setReadTimeout(60 * 1000);

  InputStream stream = connection.getInputStream();

  DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
  domFactory.setNamespaceAware(true);
  DocumentBuilder builder = domFactory.newDocumentBuilder();
  Document doc = builder.parse(stream); // This line is hanging
  return doc;
}

暂停时的堆栈跟踪:

Thread [main] (Suspended)   
    SocketInputStream.socketRead0(FileDescriptor, byte[], int, int, int) line: not available [native method]    
    SocketInputStream.read(byte[], int, int) line: not available    
    BufferedInputStream.fill() line: not available  
    BufferedInputStream.read1(byte[], int, int) line: not available 
    BufferedInputStream.read(byte[], int, int) line: not available  
    HttpClient.parseHTTPHeader(MessageHeader, ProgressSource, HttpURLConnection) line: not available    
    HttpClient.parseHTTP(MessageHeader, ProgressSource, HttpURLConnection) line: not available  
    HttpURLConnection.getInputStream() line: not available  
    XMLEntityManager.setupCurrentEntity(String, XMLInputSource, boolean, boolean) line: not available   
    XMLEntityManager.startEntity(String, XMLInputSource, boolean, boolean) line: not available  
    XMLEntityManager.startDTDEntity(XMLInputSource) line: not available 
    XMLDTDScannerImpl.setInputSource(XMLInputSource) line: not available    
    XMLDocumentScannerImpl$DTDDriver.dispatch(boolean) line: not available  
    XMLDocumentScannerImpl$DTDDriver.next() line: not available 
    XMLDocumentScannerImpl$PrologDriver.next() line: not available  
    XMLNSDocumentScannerImpl(XMLDocumentScannerImpl).next() line: not available 
    XMLNSDocumentScannerImpl.next() line: not available 
    XMLNSDocumentScannerImpl(XMLDocumentFragmentScannerImpl).scanDocument(boolean) line: not available  
    XIncludeAwareParserConfiguration(XML11Configuration).parse(boolean) line: not available 
    XIncludeAwareParserConfiguration(XML11Configuration).parse(XMLInputSource) line: not available  
    DOMParser(XMLParser).parse(XMLInputSource) line: not available  
    DOMParser.parse(InputSource) line: not available    
    DocumentBuilderImpl.parse(InputSource) line: not available  
    DocumentBuilderImpl(DocumentBuilder).parse(InputStream) line: not available 
    MSCommunicator.getInputStream(String) line: 45  
    MSCommunicator.getGamePageFromForum(int, int, int) line: 70 
    MSCommunicator.getGamePageFromForum(int, int) line: 57  
    Game.<init>(int, int) line: 21  
    MSCommunicator.main(String[]) line: 26  
4

2 回答 2

0

即使您获得的 HTML 页面是正确且格式正确的 HTML,它也可能不是格式正确的 XML。例如,这在 HTML4 中有效:

<p class=myclass>Paragraph<br>Next line</p>

而在 XML (XHTML) 中,这被认为是有效的:

<p class="myclass">Paragraph<br/>Next line</p>

注意封闭<br/>标签和围绕类属性的引号p

此外,互联网是一个狂野的地方,所以内容不太可能是格式良好的,这就是为什么您需要“对所有内容持保留态度” - 甚至是格式良好,因此您必须使用 HTML 整理器,像jTidynekoHTML

于 2012-10-15T08:00:59.150 回答
0

您不能只期望将 HTML 解析为 XML DOM 树。它不一定是有效的 XML。你可能需要先清理它。查看这个问题的答案:

使用 Java 将 HTML 文件读取到 DOM 树

于 2012-10-15T07:53:31.053 回答