gpt4 book ai didi

java - XMLUnit 与 HTML 文件的差异

转载 作者:行者123 更新时间:2023-12-01 12:46:50 26 4
gpt4 key购买 nike

我正在尝试将 XMLUnit 的 diff 与两个 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 的简单方法是什么?

最佳答案

您可以使用 HTMLDocumentBuilderparse 方法从 HTML 输入创建 DocumentDiff 和 XMLUnit 差异引擎的其他部分将很乐意与 Document 一起使用。

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

关于java - XMLUnit 与 HTML 文件的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24621704/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com