Java XML Node to String toString(final short nodeType)

Here you can find the source of toString(final short nodeType)

Description

to String

License

Open Source License

Declaration

public final static String toString(final short nodeType) 

Method Source Code

//package com.java2s;
/**//ww  w.  j a  v a 2s  .c om
 * The contents of this file are subject to the Regenstrief Public License
 * Version 1.0 (the "License"); you may not use this file except in compliance with the License.
 * Please contact Regenstrief Institute if you would like to obtain a copy of the license.
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) Regenstrief Institute.  All Rights Reserved.
 */

import org.w3c.dom.*;

public class Main {
    /**
     * Retrieves the String of a Node
     * 
     * @param n the Node
     * @return the String
     **/
    public final static String toString(final Node n) {
        if (n == null) {
            return null;
        }

        return n instanceof Text ? n.getNodeValue() : n.getNodeName();
    }

    public final static String toString(final short nodeType) {
        return toClass(nodeType).getSimpleName();
    }

    public final static Class<? extends Node> toClass(final short nodeType) {
        switch (nodeType) {
        case Node.ATTRIBUTE_NODE:
            return Attr.class;
        case Node.CDATA_SECTION_NODE:
            return CDATASection.class;
        case Node.COMMENT_NODE:
            return Comment.class;
        case Node.DOCUMENT_FRAGMENT_NODE:
            return DocumentFragment.class;
        case Node.DOCUMENT_NODE:
            return Document.class;
        case Node.DOCUMENT_TYPE_NODE:
            return DocumentType.class;
        case Node.ELEMENT_NODE:
            return Element.class;
        case Node.ENTITY_NODE:
            return Entity.class;
        case Node.ENTITY_REFERENCE_NODE:
            return EntityReference.class;
        case Node.NOTATION_NODE:
            return Notation.class;
        case Node.PROCESSING_INSTRUCTION_NODE:
            return ProcessingInstruction.class;
        case Node.TEXT_NODE:
            return Text.class;
        }
        throw new RuntimeException("Unrecognized node type " + nodeType);
    }
}

Related

  1. toHtml(StringBuffer html, Node node)
  2. toString(final Node node)
  3. toString(final Node node)
  4. toString(final Node node)
  5. toString(final Node xml)
  6. toString(Node element)
  7. toString(Node n)
  8. toString(Node n)
  9. toString(Node node)