Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javax.xml.parsers.DocumentBuilder分析网页时发生致命错误_Java_Html_Html Parsing_Sax_Saxparser - Fatal编程技术网

使用javax.xml.parsers.DocumentBuilder分析网页时发生致命错误

使用javax.xml.parsers.DocumentBuilder分析网页时发生致命错误,java,html,html-parsing,sax,saxparser,Java,Html,Html Parsing,Sax,Saxparser,我正在写一个解析网页的程序(一个我无法访问的网页,所以我无法修改它) 首先,我连接并使用getContent()获取页面的InputStream。那里没有问题 但在解析时: public static int[] parseMoveGameList(InputStream is) throws ParserConfigurationException, IOException, SAXException { DocumentBuilderFactory dbf = Doc

我正在写一个解析网页的程序(一个我无法访问的网页,所以我无法修改它)

首先,我连接并使用getContent()获取页面的InputStream。那里没有问题

但在解析时:

    public static int[] parseMoveGameList(InputStream is) throws ParserConfigurationException, IOException, SAXException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dbf.newDocumentBuilder();
        Document doc = builder.parse(is);
        /*...*/
    }
这里是builder.parse抛出:

org.xml.sax.SAXParseException; lineNumber: 3; columnNumber: 64; The system identifier must begin with either a single or double quote character.
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:253)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:288)
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:121)
    at cs.ualberta.lgadapter.LGAdapter.parseMoveGameList(LGAdapter.java:78)
    ...
我正在解析(但无法更改)的页面看起来像

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >









<html>
<head>
<META http-equiv="Expires" content="0" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<!-- ...  -->
</head>
<body>
<!-- ...  -->
</body>
</html>


如何通过此异常?

Html不是有效的xml。使用xml解析器解析html可能会导致很多错误(正如您已经发现的)

html失败的原因是您的Doctype声明:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >

xml解析器希望“PUBLIC”doctype声明如下所示:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "FALLBACK PATH TO DTD" >

如果你不能改变html页面,我不确定你能做什么。也许您可以修改/包装输入流以添加一些伪数据,使其符合预期,或者删除doctype声明


您应该改用HTML解析库。我不知道我脑子里有什么想法,但这篇(较旧的)帖子似乎列出了一些。搜索谷歌也会带来

我认为使用XML解析器解析HTML不是一个好主意。