Java XML Node Print printNode(Node node, StringWriter sw)

Here you can find the source of printNode(Node node, StringWriter sw)

Description

Write a node to the current output buffer

License

Open Source License

Parameter

Parameter Description
node the node to start from

Declaration

static void printNode(Node node, StringWriter sw) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.StringWriter;

import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;

public class Main {
    /**/*from w  ww  . ja  v  a 2 s.com*/
     * Write a node to the current output buffer
     * @param node the node to start from
     */
    static void printNode(Node node, StringWriter sw) {
        if (node.getNodeType() == Node.TEXT_NODE) {
            String content = node.getTextContent();
            if (content.contains("&"))
                content = content.replace("&", "&");
            sw.write(content);
        } else if (node.getNodeType() == Node.ELEMENT_NODE) {
            printElementStart((Element) node, sw);
            printElementBody((Element) node, sw);
            printElementEnd((Element) node, sw);
        } else if (node.getNodeType() == Node.COMMENT_NODE) {
            sw.write("<!--");
            sw.write(node.getTextContent());
            sw.write("-->");
        }
    }

    /**
     * Print a single element node to the current buffer
     * @param elem the element in question
     */
    static void printElementStart(Element elem, StringWriter sw) {
        sw.write("<");
        sw.write(elem.getNodeName());
        NamedNodeMap attrs = elem.getAttributes();
        for (int j = 0; j < attrs.getLength(); j++) {
            Node attr = attrs.item(j);
            sw.write(" ");
            sw.write(attr.getNodeName());
            sw.write("=\"");
            sw.write(attr.getNodeValue());
            sw.write("\"");
        }
        if (elem.getFirstChild() == null)
            sw.write("/>");
        else
            sw.write(">");
    }

    /**
     * Print a single element node
     * @param elem the element in question
     * @param versions the set of versions to write to
     * @param map the map of versions to buffers
     */
    static void printElementBody(Element elem, StringWriter sw) {
        // recurse into its children
        Node child = elem.getFirstChild();
        while (child != null) {
            printNode(child, sw);
            child = child.getNextSibling();
        }
    }

    /**
     * Print an element's end-code to the current buffer
     * @param elem the element in question
     */
    static void printElementEnd(Element elem, StringWriter sw) {
        if (elem.getFirstChild() != null) {
            sw.write("</");
            sw.write(elem.getNodeName());
            sw.write(">");
        }
    }
}

Related

  1. printNode(Node node)
  2. printNode(Node node)
  3. printNode(Node node)
  4. printNode(Node node, int level)
  5. printNode(Node node, PrintStream out)
  6. printNodeBasics(Node node)
  7. printNodeIterator(NodeIterator iterator)
  8. printNodes(Node node, int level)
  9. printNodeType(Node node, int ident)