Java 怎么判断节点类型是否匹配

2019年8月7日21:03:35Java 怎么判断节点类型是否匹配已关闭评论 607 1487字阅读4分57秒

1.简介

Java 怎么判断节点类型是否匹配?——请看下文。

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 boolean isNodeTypeMatches(Node node, NodeType nodeType) {
        return ((int) node.getNodeType()) == nodeType.getValue();
    }
    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");
    }
}

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