Java Utililty Methods XML Document Print

List of utility methods to do XML Document Print

Description

The list of methods to do XML Document Print are organized into topic(s).

Method

StringprintDocument(File file)
print Document
BufferedReader br = new BufferedReader(new FileReader(file));
String line = null, fileContent = "";
while ((line = br.readLine()) != null) {
    fileContent += line + "--";
return fileContent;
voidprintDocument(final Document doc)
print Document
try {
    final TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;
    transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
...
voidprintDocument(Node _docOrNode, OutputStream _outStream)
Dump a Document or Node -compatible object to the given OutputStream (e.g.
if (_docOrNode == null || _outStream == null) {
    throw new IOException("Cannot print (on) 'null' object");
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer;
try {
    transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
...
StringprintDocument(Node doc)
print Document
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
StringWriter sw = new StringWriter();
...
voidprintDocumentToStdout(final Document document)
print Document To Stdout
final Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
final StreamResult result = new StreamResult(new StringWriter());
final DOMSource source = new DOMSource(document);
transformer.transform(source, result);
final String xmlString = result.getWriter().toString();
System.out.println(xmlString);
voidprintDocumentTree(Node el)
print Document Tree
System.out.println(el.toString());
for (int i = 0; i < el.getChildNodes().getLength(); i++)
    printDocumentTree(el.getChildNodes().item(i));
voidprintDOM(File file, Document doc, String encoding)
print DOM
printDOM(file != null ? new FileOutputStream(file) : System.out, doc, encoding);
System.err.println("File " + file + " written.");
voidprintElements(Document doc)
print Elements
NodeList nodelist = doc.getElementsByTagName("*");
Node node;
for (int i = 0; i < nodelist.getLength(); i++) {
    node = nodelist.item(i);
    System.out.print(node.getNodeName() + " ");
System.out.println();
StringprintElements(Document dom)
print Elements
if (dom == null)
    return null;
StringBuffer sb = new StringBuffer();
walkNodes(dom, sb, "");
return sb.toString();
voidprintPI(Document doc, PrintStream pstrm)
print PI
NodeList nodes = doc.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
    Node node = nodes.item(i);
    if (node instanceof ProcessingInstruction) {
        ProcessingInstruction pi = (ProcessingInstruction) node;
        pstrm.println("<?" + pi.getTarget() + " " + pi.getData() + "?>");