get XML Node Type - Java XML

Java examples for XML:XML Node

Description

get XML Node Type

Demo Code


import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main{
    public static NodeType getNodeType(Node node) {
        switch (node.getNodeType()) {
        case Node.ATTRIBUTE_NODE:
            return NodeType.ATTRIBUTE_NODE;

        case Node.ELEMENT_NODE:
            return NodeType.ELEMENT_NODE;

        case Node.TEXT_NODE:
            return NodeType.TEXT_NODE;

        case Node.CDATA_SECTION_NODE:
            return NodeType.CDATA_SECTION_NODE;

        case Node.ENTITY_REFERENCE_NODE:
            return NodeType.ENTITY_REFERENCE_NODE;

        case Node.ENTITY_NODE:
            return NodeType.ENTITY_NODE;

        case Node.DOCUMENT_NODE:
            return NodeType.DOCUMENT_NODE;

        case Node.DOCUMENT_TYPE_NODE:
            return NodeType.DOCUMENT_TYPE_NODE;

        case Node.DOCUMENT_FRAGMENT_NODE:
            return NodeType.DOCUMENT_FRAGMENT_NODE;

        case Node.NOTATION_NODE:
            return NodeType.NOTATION_NODE;

        case Node.PROCESSING_INSTRUCTION_NODE:
            return NodeType.PROCESSING_INSTRUCTION_NODE;

        case Node.COMMENT_NODE:
            return NodeType.COMMENT_NODE;

        default://  www. j  a  v a  2  s .co  m
            break;
        }

        throw new RuntimeException("No NodeType detected");
    }
}

Related Tutorials