gpt4 book ai didi

java - 我如何调用 DaisyDiff 来比较两个 HTML 文件?

转载 作者:可可西里 更新时间:2023-11-01 14:45:58 25 4
gpt4 key购买 nike

我需要在我的应用程序中创建两个 HTML 文档之间的差异。我找到了一个名为 DaisyDiff 的库可以做到这一点。它有一个看起来像这样的 API:

/**
* Diffs two html files, outputting the result to the specified consumer.
*/
public static void diffHTML(InputSource oldSource, InputSource newSource,
ContentHandler consumer, String prefix, Locale locale)
throws SAXException, IOException

我对 SAX 完全一无所知,我不知道要传递什么作为第三个参数。翻完https://code.google.com/p/daisydiff/source/browse/trunk/daisydiff/src/java/org/outerj/daisy/diff/Main.java我写了这个方法:

@Override
protected String doInBackground(String... params)
{
try {
String oldFileName = params[0],
newFileName = params[1];
ByteArrayOutputStream os = new ByteArrayOutputStream();
FileInputStream oldis = null, newis = null;
oldis = openFileInput(oldFileName);
newis = openFileInput(newFileName);

SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory
.newInstance();

TransformerHandler result = tf.newTransformerHandler();
result.setResult(new StreamResult(os));
DaisyDiff.diffHTML(new InputSource(oldis), new InputSource(newis), result, "", Locale.getDefault());

Log.d("diff", "output length = " + os.size());
return os.toString("Utf-8");

}catch (Exception e){
return e.toString();
}
}

我什至不知道这是否有意义。它不起作用,没有任何内容写入输出。请在这件事上给予我帮助。提前致谢。

最佳答案

如何根据HtmlTestFixture.diff已编码(在 src/test/javaDaisyDiff 内,您需要向其提供有关如何格式化结果的说明。您是否尝试添加以下 setOutputProperty(...) 调用?

@Test 
//@Test comes from TestNG and is not related to DaisyDiff
public void daisyDiffTest() throws Exception {
String html1 = "<html><body>var v2</body></html>";
String html2 = "<html> \n <body> \n Hello world \n </body> \n </html>";

try {
StringWriter finalResult = new StringWriter();
SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler result = tf.newTransformerHandler();
result.getTransformer().setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
result.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
result.getTransformer().setOutputProperty(OutputKeys.METHOD, "html");
result.getTransformer().setOutputProperty(OutputKeys.ENCODING, "UTF-8");
result.setResult(new StreamResult(finalResult));

ContentHandler postProcess = result;
DaisyDiff.diffHTML(new InputSource(new StringReader(html1)), new InputSource(new StringReader(html2)), postProcess, "test", Locale.ENGLISH);
System.out.println(finalResult.toString());
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

这样做,我的输出如下。现在我可以将其粘贴到一个 HTML 文件中,包括正确的 css 和 js 文件并获得漂亮的输出。

<span class="diff-html-removed" id="removed-test-0" previous="first-test" changeId="removed-test-0" next="added-test-0">var v2</span><span class="diff-html-added" previous="removed-test-0" changeId="added-test-0" next="last-test"> </span><span class="diff-html-added" id="added-test-0" previous="removed-test-0" changeId="added-test-0" next="last-test">Hello world </span>

关于java - 我如何调用 DaisyDiff 来比较两个 HTML 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30508000/

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