The full class bellow:
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSSerializer;
import org.xml.sax.SAXException;
public class XMLNormalizeMain {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is1 = Thread.currentThread().getContextClassLoader().getResourceAsStream("ctx1.xml");
InputStream is2 = Thread.currentThread().getContextClassLoader().getResourceAsStream("ctx2.xml");
Document ctx1 = db.parse(is1);
Document ctx2 = db.parse(is2);
ctx1.normalize();
ctx2.normalize();
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
Element a1 = (Element) getElement(ctx1, xPath, "/root/a");
Element a2 = (Element) getElement(ctx2, xPath, "/root/a");
System.out.println("a1: " + nodeToString(ctx1, a1));
System.out.println("a2: " + nodeToString(ctx2, a2));
System.out.println(a1.isEqualNode(a2));
}
private static Node getElement(Node e, XPath xpath, String path) throws XPathExpressionException {
XPathExpression expr = xpath.compile(path);
return (Node) expr.evaluate(e, XPathConstants.NODE);
}
private static String nodeToString(Document ctx, Node n) {
DOMImplementationLS domImplLS = (DOMImplementationLS) ctx.getImplementation();
LSSerializer serializer = domImplLS.createLSSerializer();
return serializer.writeToString(n);
}
}
XML for ctx1:
HTML Code:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<a>
<b>some text here</b>
</a>
</root>
XML for ctx2:
HTML Code:
<?xml version="1.0" encoding="UTF-8"?>
<root><a><b>some text here</b></a></root>