0

我正在尝试将 XMLUnit 的差异与两个 HTML 文档一起使用。为此,我将它们转换为字符串,然后从这两个字符串构造一个 diff 对象。

但是,这会引发以下 SAXException:

[Fatal Error] :1:177: The element type "br" must be terminated by the matching end-tag "</br>".
org.xml.sax.SAXParseException: The element type "br" must be terminated by the matching end-tag "</br>".
    at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    at org.custommonkey.xmlunit.XMLUnit.buildDocument(XMLUnit.java:383)
    at org.custommonkey.xmlunit.XMLUnit.buildDocument(XMLUnit.java:370)
    at org.custommonkey.xmlunit.Diff.<init>(Diff.java:101)
    at org.custommonkey.xmlunit.Diff.<init>(Diff.java:93)
    at controllers.Api.diffUrls(Api.java:292)
    at Routes$$anonfun$routes$1$$anonfun$applyOrElse$8$$anonfun$apply$8.apply(routes_routing.scala:165)
    at Routes$$anonfun$routes$1$$anonfun$applyOrElse$8$$anonfun$apply$8.apply(routes_routing.scala:165)
    at play.core.Router$HandlerInvoker$$anon$7$$anon$2.invocation(Router.scala:183)
    at play.core.Router$Routes$$anon$1.invocation(Router.scala:377)
    at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:56)
    at play.core.j.JavaAction$$anon$3.apply(JavaAction.scala:91)
    at play.core.j.JavaAction$$anon$3.apply(JavaAction.scala:90)
    at play.core.j.FPromiseHelper$$anonfun$flatMap$1.apply(FPromiseHelper.scala:82)
    at play.core.j.FPromiseHelper$$anonfun$flatMap$1.apply(FPromiseHelper.scala:82)
    at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:278)
    at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:274)
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:29)
    at play.core.j.HttpExecutionContext$$anon$2.run(HttpExecutionContext.scala:37)
    at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:42)
    at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:386)
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

因此,我需要将此 HTML 转换为有效的 XML。我看到 XMLUnit 提供了 HTMLDocumentBuilder 似乎提供了一种方法来做到这一点,但仅用于 XPath 评估。什么是转换为有效 HTML 以便我可以进行 XMLUnit diff 的简单方法?

4

1 回答 1

1

您可以使用HTMLDocumentBuilder'方法从您的 HTML 输入 parse创建一个。XMLUnit 的差异引擎的其他部分将很乐意与s 一起使用。DocumentDiffDocument

请注意,HTMLDocumentBuilder这有点小技巧,您可能更适合专门从事 HTML => XML 转换的库,例如 jTidy。

于 2015-02-03T19:27:17.750 回答