Java XML Element Print printElementBody(Element elem, StringWriter sw)

Here you can find the source of printElementBody(Element elem, StringWriter sw)

Description

Print a single element node

License

Open Source License

Parameter

Parameter Description
elem the element in question
versions the set of versions to write to
map the map of versions to buffers

Declaration

static void printElementBody(Element elem, 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 w w . j  av  a2  s. c  om
     * 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();
        }
    }

    /**
     * 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 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. printElement(Element e)
  2. printElement(SOAPElement el)
  3. printElementEnd(Element elem, StringWriter sw)
  4. printElementStart(Element elem, StringWriter sw)
  5. printElemShallow(Element elem, String endient, PrintStream pstrm)
  6. printMethodArgs(Element elem, String endient, PrintStream pstrm)