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

📄 xmlutil.java

📁 Jodd是一个开源的公用Java基础类库
💻 JAVA
字号:
package jodd.util;

import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;

import org.apache.xpath.XPathAPI;
import javax.xml.transform.TransformerException;

/**
 * Some XML and XPath utilities.
 */
public class XMLUtil {

	/**
	 * Returns a map of node attributes. All non-attribute nodes are ignored.
	 *
	 * @param node   node
	 *
	 * @return map of node attributes
	 */
	public static Map getNodeAttributes(Node node) {
		HashMap attrs = new HashMap();
		NamedNodeMap nmm = node.getAttributes();
		for (int j = 0; j < nmm.getLength(); j++) {
			Node attribute = nmm.item(j);
			if (attribute.getNodeType() != Node.ATTRIBUTE_NODE) {
				continue;
			}
			attrs.put(attribute.getNodeName(), attribute.getNodeValue());
		}
		return attrs;
	}

	/**
	 * Returns a list of all contained nodes in the node list. All non-element nodes are ignored.
	 *
	 * @param nodeList node list
	 *
	 * @return list of contained nodes
	 */
	public static List getNodes(NodeList nodeList) {
		ArrayList nodes = new ArrayList();
		for (int k = 0; k < nodeList.getLength(); k++) {
			Node node = nodeList.item(k);
			if (node.getNodeType() != Node.ELEMENT_NODE) {
				continue;
			}
			nodes.add(node);
		}
		return nodes;
	}

	/**
	 * Returns a list of all contained nodes in the node list.
	 *
	 * @param doc    xml document
	 * @param xpath  xpath
	 *
	 * @return list of contained nodes
	 * @exception TransformerException
	 */
	public static List getNodes(Document doc, String xpath) throws TransformerException {
		return getNodes(XPathAPI.selectNodeList(doc, xpath));
	}

}

⌨️ 快捷键说明

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