2525import javax .xml .xpath .XPathExpression ;
2626import javax .xml .xpath .XPathExpressionException ;
2727import javax .xml .xpath .XPathFactory ;
28+ import javax .xml .xpath .XPathFactoryConfigurationException ;
2829import org .w3c .dom .Document ;
2930import org .w3c .dom .NodeList ;
3031import org .xml .sax .InputSource ;
@@ -41,7 +42,8 @@ public class XMLPrettyPrinter {
4142 public static int IDENT_AMOUNT = 4 ;
4243
4344 public static String prettyPrintXML (String input ) throws TransformerConfigurationException ,
44- ParserConfigurationException , SAXException , IOException , TransformerException , XPathExpressionException {
45+ ParserConfigurationException , SAXException , IOException , TransformerException , XPathExpressionException ,
46+ XPathFactoryConfigurationException {
4547 Transformer transformer = TransformerFactory .newInstance ().newTransformer ();
4648 transformer .setOutputProperty (OutputKeys .INDENT , "yes" );
4749 transformer .setOutputProperty ("{http://xml.apache.org/xslt}indent-amount" , Integer .toString (IDENT_AMOUNT ));
@@ -53,6 +55,10 @@ public static String prettyPrintXML(String input) throws TransformerConfiguratio
5355
5456 // XPath for selecting all text contents
5557 XPathExpression xpath = XPathFactory .newInstance ().newXPath ().compile ("//*[text()]/*" );
58+ // this is better but it does not work, because the default java xpath
59+ // transformer does not support XPath 2.0
60+ // XPathExpression xpath =
61+ // XPathFactory.newInstance().newXPath().compile("//*[text()[matches(.,'^[0-9A-F ]*$')]]");
5662 // XPath for counting the number of ancestors of a current element
5763 XPathExpression xpathDepth = XPathFactory .newInstance ().newXPath ().compile ("count(ancestor-or-self::*)" );
5864
0 commit comments