⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xpathapi.java

📁 这是一个轻便的j2ee的web应用框架,是一个在多个项目中运用的实际框架,采用struts,hebinate,xml等技术,有丰富的tag,role,navigation,session,dictio
💻 JAVA
字号:
package com.esimple.framework.util.xml;

import java.util.ListIterator;
import org.apache.xpath.NodeSet;
import org.w3c.dom.*;
import org.w3c.dom.traversal.NodeIterator;

public class XPathAPI
{

    public XPathAPI()
    {
    }

    public static Node selectSingleNode(Node contextNode, String xPath)
    {
        if(!checkNode(contextNode))
            return null;
        Node searchNode = realNode(contextNode);
        XPathPaser paser = new XPathPaser(xPath);
        EOSNodeList nlResult = new EOSNodeList();
        findNodeList((Element)searchNode, paser.listIterator(), nlResult, true);
        if(nlResult.getLength() > 0)
            return nlResult.item(0);
        else
            return null;
    }

    public static NodeList selectNodeList(Node contextNode, String xPath)
    {
        if(!checkNode(contextNode))
        {
            return null;
        } else
        {
            Node searchNode = realNode(contextNode);
            XPathPaser paser = new XPathPaser(xPath);
            EOSNodeList nlResult = new EOSNodeList();
            findNodeList((Element)searchNode, paser.listIterator(), nlResult, false);
            return nlResult;
        }
    }

    public static NodeIterator selectNodeIterator(Node contextNode, String xPath)
    {
        NodeSet ns = new NodeSet(selectNodeList(contextNode, xPath));
        return ns;
    }

    private static void findNodeList(Element currNode, ListIterator itXPath, EOSNodeList result, boolean single)
    {
        if(single && result.getLength() > 0)
            return;
        if(!itXPath.hasNext())
            return;
        NodeDescription curDesc = (NodeDescription)itXPath.next();
        if(curDesc.getType() == 1)
        {
            Node node = currNode.getParentNode();
            if(node == null || node.getNodeType() == 9)
            {
                Element parent = currNode;
                if(curDesc.equals(parent))
                    result.append(curDesc.m_attr);
            }
            return;
        }
        if(!curDesc.equals(currNode))
            return;
        if(itXPath.hasNext())
        {
            NodeDescription nextDesc = (NodeDescription)itXPath.next();
            if(itXPath.hasPrevious())
                itXPath.previous();
            if(nextDesc.getType() == 1)
            {
                if(nextDesc.equals(currNode))
                    result.append(nextDesc.m_attr);
                return;
            }
        }
        if(itXPath.hasNext())
        {
            NodeList nlChilds = currNode.getChildNodes();
            int nCount = nlChilds.getLength();
            for(int i = 0; i < nCount; i++)
            {
                Node currChild = nlChilds.item(i);
                if(checkNode(currChild))
                {
                    findNodeList((Element)currChild, itXPath, result, single);
                    if(itXPath.hasPrevious())
                        itXPath.previous();
                }
            }

            return;
        } else
        {
            result.append(currNode);
            return;
        }
    }
    
	private static boolean checkNode(Node node)
	{
		return node.getNodeType() == 9 || node.getNodeType() == 1;
	}

	private static Node realNode(Node node)
	{
		if(node.getNodeType() == 9)
			return ((Document)node).getDocumentElement();
		else
			return node;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -