Maven资源库中是否有一个Java API可以解析HTML文档并验证它是否格式正确?
更新:
我程序中的代码如下所示:
url = "C:/Users/user1/Desktop/testHTML.html";
FileInputStream fi = new FileInputStream(url);
Tidy tidy = new Tidy();
//tidy.setQuiet(true);
tidy.parse(fi, null);
//tidy.parseDOM(fi, fo);
int tempWarnings = tidy.getParseWarnings();
int tempErrors = tidy.getParseErrors();`
我的HTML文件中的内容如下:
<html>
<head>
<title>This is a sample doc</title>
</head>
<body>
<p> <b>this is a sample paragraph</b></p>
然而,即使在DOCTYPE缺失的情况下,Tidy也不会发出任何警告或错误。
答案 0 :(得分:2)
答案 1 :(得分:1)
正如@rolfl所说,你可以使用JTidy。虽然JTidy文档很糟糕(我之前从未使用过),所以我下载了它并尝试使用它。此测试运行并提供3个警告:
package com.sandbox;
import org.junit.Test;
import org.w3c.tidy.Tidy;
import java.io.StringReader;
import java.io.StringWriter;
import static org.junit.Assert.assertEquals;
public class SandboxTest {
@Test
public void myTest() {
Tidy tidy = new Tidy();
StringWriter writer = new StringWriter();
tidy.parse(new StringReader("invalid html"), writer);
assertEquals(0, tidy.getParseErrors());
assertEquals(0, tidy.getParseWarnings());
}
}
这个断言在最后一行失败了,因为它返回3而不是0.这就是你要找的东西吗?
我尝试使用您的输入,我收到了警告:
package com.sandbox;
import org.junit.Test;
import org.w3c.tidy.Tidy;
import java.io.StringReader;
import java.io.StringWriter;
import static org.junit.Assert.assertEquals;
public class SandboxTest {
@Test
public void myTest() {
Tidy tidy = new Tidy();
StringWriter writer = new StringWriter();
tidy.parse(new StringReader("<html>\n" +
"<head>\n" +
" <title>This is a sample doc</title>\n" +
"</head>\n" +
"<body>\n" +
" <p> <b>this is a sample paragraph</b></p>"), writer);
assertEquals(0, tidy.getParseErrors());
assertEquals(0, tidy.getParseWarnings());
}
}
输出:
line 1 column 1 - Warning: missing <!DOCTYPE> declaration
InputStream: Document content looks like HTML 2.0
1 warning, no errors were found!
java.lang.AssertionError:
Expected :0
Actual :1
<Click to see difference>
at org.junit.Assert.fail(Assert.java:93)
at org.junit.Assert.failNotEquals(Assert.java:647)
at org.junit.Assert.assertEquals(Assert.java:128)
at org.junit.Assert.assertEquals(Assert.java:472)
at org.junit.Assert.assertEquals(Assert.java:456)
at com.sandbox.SandboxTest.myTest(SandboxTest.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Process finished with exit code -1