需要引用 #Daisy Diff# 这个项目的jar包 /** * 比较两段 HTML 的差异 * @param html1 * @param html2 * @return * @throws SAXException * @throws IOException * @throws TransformerConfigurationException */public static String diff(S
/** * 比较两段 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 " " }