什么HTML解析器可以整理这段代码?

时间:2012-03-29 22:21:02

标签: java jsoup html-parsing

我正在使用Java 6.我想找到一个可以清理丑陋HTML的工具。具体来说,我想要一个可以处理以下内容的工具......

<script type="text/javascript">
    document.write(
     '<scr'+'ipt src="http://ox-d.journatic.com/w/1.0/jstag"><\/scr'+'ipt>');
</script>

我已经尝试过JSoup v 1.6.2来处理上述问题,但是使用

运行上面的代码
final org.jsoup.nodes.Document doc = Jsoup.parse(html);
final String formattedHtml = doc.toString();

返回相同的代码。当我尝试用

解析它时,上面的问题就是这个问题
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
final DocumentBuilder builder = factory.newDocumentBuilder();
final InputSource s = new InputSource(new StringReader(cleanedUpHtml));
org.w3c.dom.Document result = builder.parse(s);

我得到例外......

org.xml.sax.SAXParseException: Element type "scr" must be followed by either attribute specifications, ">" or "/>".
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:249)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
    at com.myco.myproject.util.XmlUtilities.getStringAsDocument(XmlUtilities.java:146)
    at com.myco.myproject.util.NetUtilities.getUrlAsDocument(NetUtilities.java:54)
    at com.myco.myproject.parsers.impl.AbstractMetromixParser.parsePage(AbstractMetromixParser.java:107)
    at com.myco.myproject.parsers.impl.AbstractMetromixParser.getEvents(AbstractMetromixParser.java:76)
    at com.myco.myproject.domain.EventFeed.refresh(EventFeed.java:81)
    at com.myco.myproject.domain.EventFeed.getEvents(EventFeed.java:72)
    at com.myco.myproject.parsers.impl.MetromixParserTest.testParser(MetromixParserTest.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

有关可以整理上述内容的HTML解析器的任何建议吗? - 戴夫

1 个答案:

答案 0 :(得分:0)

您无需将String解析为html文档。只需在原始String上使用Jsoup.clean()。请参阅http://jsoup.org/cookbook/cleaning-html/whitelist-sanitizer

上的一个简单示例