public String getNormalizedXml ( String filePath ) { // Read the provided filename final StringWriter toReturn = new StringWriter ( ) ; final BufferedReader in ; try { in = new BufferedReader ( new FileReader ( new File ( filePath ) ) ) ; } catch ( FileNotFoundException e ) { throw new IllegalArgumentException ( "Could not find file [" + filePath + "]" , e ) ; } // Parse into a Document final DocumentBuilderFactory factory = DocumentBuilderFactory . newInstance ( ) ; factory . setNamespaceAware ( true ) ; Document document = null ; try { document = factory . newDocumentBuilder ( ) . parse ( new InputSource ( in ) ) ; Transformer transformer = FACTORY . newTransformer ( ) ; transformer . setOutputProperty ( OutputKeys . INDENT , "yes" ) ; transformer . transform ( new DOMSource ( document . getFirstChild ( ) ) , new StreamResult ( toReturn ) ) ; } catch ( Exception e ) { throw new IllegalArgumentException ( "Could not transform DOM Document" , e ) ; } // All done. return toReturn . toString ( ) ; }
Reads the XML file found at the provided filePath returning a normalized XML form suitable for comparison .