Java Utililty Methods XML Node Format

List of utility methods to do XML Node Format

Description

The list of methods to do XML Node Format are organized into topic(s).

Method

StringBufferformatNode(Node node, int indent, StringBuffer stringBuffer)
format Node
int type = node.getNodeType();
switch (type) {
case Node.DOCUMENT_NODE: {
    stringBuffer.append("<?xml version=\"1.0\" ?>\n"); 
    stringBuffer = formatNode(((Document) node).getDocumentElement(), indent, stringBuffer);
    break;
case Node.ELEMENT_NODE: {
...
voidprint(PrintStream out, Node node)
print
if (node == null)
    return;
short type = node.getNodeType();
switch (type) {
case Node.DOCUMENT_NODE: {
    out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    NodeList nodelist = node.getChildNodes();
    int size = nodelist.getLength();
...
voidprintNode(StringBuffer sBuffer, Node node)
Takes XML node and prints to String.
if (node.getNodeType() == Node.TEXT_NODE) {
    sBuffer.append(encodeXMLText(node.getNodeValue().trim()));
} else if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
    sBuffer.append("<![CDATA[");
    sBuffer.append(node.getNodeValue());
    sBuffer.append("]]>");
} else if (node.getNodeType() == Node.ELEMENT_NODE) {
    Element el = (Element) node;
...
voidprintTree(Writer ps, Node node, String indent, String lineSeparator)
Print the tree of a node.
switch (node.getNodeType()) {
case Node.DOCUMENT_NODE:
    NodeList nodes = node.getChildNodes();
    if (nodes != null) {
        for (int i = 0; i < nodes.getLength(); i++) {
            printTree(ps, nodes.item(i), "", lineSeparator);
    break;
case Node.ELEMENT_NODE:
    String name = node.getNodeName();
    ps.write(indent);
    ps.write("<");
    ps.write(name);
    NamedNodeMap attributes = node.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Node current = attributes.item(i);
        ps.write(" ");
        ps.write(current.getNodeName());
        ps.write("=\"");
        ps.write(current.getNodeValue());
        ps.write("\"");
    NodeList children = node.getChildNodes();
    if (children != null) {
        ps.write(">");
        ps.write(lineSeparator);
        for (int i = 0; i < children.getLength(); i++)
            printTree(ps, children.item(i), indent + "  ", lineSeparator);
        ps.write(indent);
        ps.write("</");
        ps.write(name);
        ps.write(">");
        ps.write(lineSeparator);
    } else {
        ps.write("/>");
        ps.write(lineSeparator);
    break;
case Node.CDATA_SECTION_NODE:
    ps.write(indent);
    ps.write("<![CDATA[");
    ps.write(node.getNodeValue());
    ps.write("]]>");
    ps.write(lineSeparator);
    break;
case Node.TEXT_NODE:
    String text = node.getNodeValue().trim();
    if (text.length() > 0) {
        ps.write(indent);
        ps.write(text);
        ps.write(lineSeparator);
    break;
case Node.PROCESSING_INSTRUCTION_NODE:
    break;
case Node.ENTITY_REFERENCE_NODE:
    break;
case Node.DOCUMENT_TYPE_NODE:
    break;
default:
    break;
voidrenderNode(StringBuffer sb, Node node)
render Node
if (node == null) {
    sb.append("null");
    return;
switch (node.getNodeType()) {
case Node.DOCUMENT_NODE:
    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    Node root = ((Document) node).getDocumentElement();
...
voidrenderNode(StringBuffer sb, Node node, String margin, String indent, String lab, String rab, String nl)
render Node
if (node == null) {
    sb.append("null");
    return;
switch (node.getNodeType()) {
case Node.DOCUMENT_NODE:
    Node root = ((Document) node).getDocumentElement();
    renderNode(sb, root, margin, indent, lab, rab, nl);
...