Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.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
Java 将xml字符串传输到org.w3c.dom.Document时是否忽略org.xml.sax.SAXParseExceptions?_Java_Html_Xml_Dom_Saxparser - Fatal编程技术网

Java 将xml字符串传输到org.w3c.dom.Document时是否忽略org.xml.sax.SAXParseExceptions?

Java 将xml字符串传输到org.w3c.dom.Document时是否忽略org.xml.sax.SAXParseExceptions?,java,html,xml,dom,saxparser,Java,Html,Xml,Dom,Saxparser,我有很多html页面(我指的是它的源代码),在java中表示为java.Util.List字符串。我需要将其转换为Java中的文档对象(来自包org.w3c.dom) 我使用DocumentBuilderFactory和Document这样做: public static org.w3c.dom.Document inputStream2Document(InputStream inputStream) throws IOException, SAXException, ParserConfig

我有很多html页面(我指的是它的源代码),在java中表示为java.Util.List字符串。我需要将其转换为Java中的文档对象(来自包org.w3c.dom)

我使用DocumentBuilderFactory和Document这样做:

public static org.w3c.dom.Document inputStream2Document(InputStream inputStream) throws IOException, SAXException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(false);
    org.w3c.dom.Document parse = dbf.newDocumentBuilder().parse(inputStream);
    return parse;
}   
有些页面的转换方式是正确的,但存在一个问题,即有些其他页面具有错误的写入属性,并且该属性无效(属性不带=“”…因此看起来像

<a href="somepage.html" someattr>


有没有办法告诉DocumentBuilderFactory,他应该忽略这个异常?我也想将这些页面转换为Document,我不介意它们无效。

XML解析器只能解析格式良好的XML(或者,同样是XHTML)。给出错误的页面格式不正确,即它们不是XML,因此XML解析器根本不适用

但是,如果唯一的问题是这些属性没有值,您可以尝试使用正则表达式对输入文件进行预处理以删除这些属性。

而不是XML解析器。NekoHTML上有一些很好的示例,展示了如何将完整的文档和HTML片段解析为DOM节点

import org.cyberneko.html.parsers.DOMParser;
import org.xml.sax.InputSource;
import org.w3c.dom.Document;
import java.io.StringReader;

DOMParser parser = new DOMParser();
InputSource in = new InputSource(new StringReader(theHtmlString));
parser.parse(in);
Document doc = parser.getDocument();

你知道有没有其他方法可以将格式不好的以字符串表示的XHTMl页面转换为Document对象?我为Apache Nutch编写了爬行插件,所以我事先不知道这些页面的结构…所以正则表达式很难应用于所有可能的情况。爬行器不应该尝试将HTML解析为XML。你呢仅对纯文本感兴趣(在浏览器中可见的文本)在这种情况下,您可以使用正则表达式剥离所有标签。嗯,我对这些页面的所有内容感兴趣…我有NodeWalker并在元素和属性中搜索。我将尝试一个NekoHTML,正如下面所写的,我们将看看它是否有帮助。非常感谢您的帮助,我尝试了NekoHTML,它现在正在工作…jTidy是我的se康德选项我想试试。祝你愉快!谢谢你的建议,我会试试的。
Nested exception: org.xml.sax.SAXParseException; lineNumber: 109; columnNumber: 32; The string "--" is not permitted within comments.
import org.cyberneko.html.parsers.DOMParser;
import org.xml.sax.InputSource;
import org.w3c.dom.Document;
import java.io.StringReader;

DOMParser parser = new DOMParser();
InputSource in = new InputSource(new StringReader(theHtmlString));
parser.parse(in);
Document doc = parser.getDocument();