25
25
import javax .xml .xpath .XPathExpression ;
26
26
import javax .xml .xpath .XPathExpressionException ;
27
27
import javax .xml .xpath .XPathFactory ;
28
+ import javax .xml .xpath .XPathFactoryConfigurationException ;
28
29
import org .w3c .dom .Document ;
29
30
import org .w3c .dom .NodeList ;
30
31
import org .xml .sax .InputSource ;
@@ -41,7 +42,8 @@ public class XMLPrettyPrinter {
41
42
public static int IDENT_AMOUNT = 4 ;
42
43
43
44
public static String prettyPrintXML (String input ) throws TransformerConfigurationException ,
44
- ParserConfigurationException , SAXException , IOException , TransformerException , XPathExpressionException {
45
+ ParserConfigurationException , SAXException , IOException , TransformerException , XPathExpressionException ,
46
+ XPathFactoryConfigurationException {
45
47
Transformer transformer = TransformerFactory .newInstance ().newTransformer ();
46
48
transformer .setOutputProperty (OutputKeys .INDENT , "yes" );
47
49
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
53
55
54
56
// XPath for selecting all text contents
55
57
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 ]*$')]]");
56
62
// XPath for counting the number of ancestors of a current element
57
63
XPathExpression xpathDepth = XPathFactory .newInstance ().newXPath ().compile ("count(ancestor-or-self::*)" );
58
64
0 commit comments