如何高效识别并描述两段HTML代码之间的具体差异?
- 内容介绍
- 文章标签
- 相关推荐
本文共计156个文字,预计阅读时间需要1分钟。
需要引用+
/** * 比较两段 HTML 的差异 * @param html1 * @param html2 * @return * @throws SAXException * @throws IOException * @throws TransformerConfigurationException */ public static String diff(String html1, String html2) throws IOException, SAXException, TransformerConfigurationException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance(); TransformerHandler result = tf.newTransformerHandler(); result.setResult(new StreamResult(baos)); Locale locale = Locale.getDefault(); HtmlCleaner cleaner = new HtmlCleaner(); InputSource oldSource = new InputSource(new StringReader(html1)); InputSource newSource = new InputSource(new StringReader(html2)); DomTreeBuilder oldHandler = new DomTreeBuilder(); cleaner.cleanAndParse(oldSource, oldHandler); TextNodeComparator leftComparator = new TextNodeComparator(oldHandler, locale); DomTreeBuilder newHandler = new DomTreeBuilder(); cleaner.cleanAndParse(newSource, newHandler); TextNodeComparator rightComparator = new TextNodeComparator(newHandler, locale); result.startDocument(); HtmlSaxDiffOutput output = new HtmlSaxDiffOutput(result, null); HTMLDiffer differ = new HTMLDiffer(output); differ.diff(leftComparator, rightComparator); result.endDocument(); String html = baos.toString(); return html.substring(38); //get rid of " " }
本文共计156个文字,预计阅读时间需要1分钟。
需要引用+
/** * 比较两段 HTML 的差异 * @param html1 * @param html2 * @return * @throws SAXException * @throws IOException * @throws TransformerConfigurationException */ public static String diff(String html1, String html2) throws IOException, SAXException, TransformerConfigurationException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance(); TransformerHandler result = tf.newTransformerHandler(); result.setResult(new StreamResult(baos)); Locale locale = Locale.getDefault(); HtmlCleaner cleaner = new HtmlCleaner(); InputSource oldSource = new InputSource(new StringReader(html1)); InputSource newSource = new InputSource(new StringReader(html2)); DomTreeBuilder oldHandler = new DomTreeBuilder(); cleaner.cleanAndParse(oldSource, oldHandler); TextNodeComparator leftComparator = new TextNodeComparator(oldHandler, locale); DomTreeBuilder newHandler = new DomTreeBuilder(); cleaner.cleanAndParse(newSource, newHandler); TextNodeComparator rightComparator = new TextNodeComparator(newHandler, locale); result.startDocument(); HtmlSaxDiffOutput output = new HtmlSaxDiffOutput(result, null); HTMLDiffer differ = new HTMLDiffer(output); differ.diff(leftComparator, rightComparator); result.endDocument(); String html = baos.toString(); return html.substring(38); //get rid of " " }

