Java XML Node Serialize serializeNode(Node node)

Here you can find the source of serializeNode(Node node)

Description

serialize Node

License

Open Source License

Declaration

private static String serializeNode(Node node) 

Method Source Code

//package com.java2s;
/*/*from  w w  w.j  av a  2 s.  c o m*/
 * This file is part of Pustefix.
 *
 * Pustefix is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Pustefix is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Pustefix; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    private static String serializeNode(Node node) {
        StringBuffer buffer = new StringBuffer();
        String nodeType = null;
        Short nodeTypeNum = node.getNodeType();
        switch (nodeTypeNum) {
        case Node.ATTRIBUTE_NODE:
            nodeType = "ATTRIBUTE";
            break;
        case Node.CDATA_SECTION_NODE:
            nodeType = "CDATA";
            break;
        case Node.COMMENT_NODE:
            nodeType = "COMMENT";
            break;
        case Node.DOCUMENT_FRAGMENT_NODE:
            nodeType = "FRAGMENT";
            break;
        case Node.DOCUMENT_NODE:
            nodeType = "DOCUMENT";
            break;
        case Node.DOCUMENT_TYPE_NODE:
            nodeType = "DOCUMENT_TYPE";
            break;
        case Node.ELEMENT_NODE:
            nodeType = "ELEMENT";
            break;
        case Node.ENTITY_NODE:
            nodeType = "ENTITY";
            break;
        case Node.ENTITY_REFERENCE_NODE:
            nodeType = "ENTITY_REFERENCE";
            break;
        case Node.NOTATION_NODE:
            nodeType = "NOTATION";
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            nodeType = "PROCESSING_INSTRUCTION";
            break;
        case Node.TEXT_NODE:
            nodeType = "TEXT";
            break;
        default:
            throw new RuntimeException("Unexpected node type!");
        }
        buffer.append(nodeType);
        buffer.append('[');
        String nodeNamespace = node.getNamespaceURI();
        if (nodeNamespace != null) {
            buffer.append("NAMESPACE[\"");
            buffer.append(nodeNamespace);
            buffer.append("\"]");
        }
        String nodeName = node.getNodeName();
        if (nodeName != null) {
            buffer.append("NAME[\"");
            buffer.append(nodeName);
            buffer.append("\"]");
        }
        String nodeValue = node.getNodeValue();
        if (nodeValue != null) {
            buffer.append("VALUE[\"");
            buffer.append(nodeValue);
            buffer.append("\"]");
        }
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            buffer.append(serializeNode(children.item(i)));
        }
        buffer.append(']');
        return buffer.toString();
    }
}

Related

  1. serializeElement(Element element, boolean omitXmlDeclaration, boolean indent, String method)
  2. serializeNode(Node node)
  3. serializeNode(Node node)
  4. serializeNode(Node node)
  5. serializeNode(Node node)
  6. serializeNode(Node node, String filename)
  7. serializeNode(Node node, String indentLevel, StringWriter writer)
  8. serializeNode(Node xmlNode)
  9. serializeNoNS(Node node)