Showing a XML

The following has 2 ways for indenting to show the xml.
1. using the class TransformerFactory's setAttribute("indent-number", new Integer(20));
2. using the class Transformer's setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");  This way is higher priority than the first way.


// Requires Java SE 1.7.0

           TransformerFactory tf = TransformerFactory.newInstance();
//1. set the indented format of the additional whitespace when outputting the result tree;
            tf.setAttribute("indent-number", new Integer(20));  // It works.

            Transformer transformer = tf.newTransformer();
//2. set the indented format of the additional whitespace when outputting the result tree; This way is higher priority than the 1.
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
      
//OutputKeys.INDENT, indent specifies whether the Transformer may add additional whitespace when outputting the result tree; the value must be yes or no.
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
      
            StreamResult result = new StreamResult(new StringWriter());

            DOMSource source = new DOMSource(doc); //doc is a Document object.
            transformer.transform(source, result);
            String xmlString = result.getWriter().toString();
            System.out.println(xmlString);



评论

此博客中的热门博文

XML, XSL, HTML

Input in element.eleme.io

Data URI是由RFC 2397 ACE