Java 怎么获取XML节点类型

2019年8月9日22:20:57Java 怎么获取XML节点类型已关闭评论 425 1368字阅读4分33秒

1.简介

Java 怎么获取XML节点类型?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

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:
            break;
        }

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

 
继续阅读
编程十万个怎么办