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

📄 xmlutil.java

📁 java系统通用框架 很实用的东东 一般人都看的懂,
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	}
	if (type == Node.ELEMENT_NODE) {
		System.out.println();
		System.out.print("</");
		System.out.print(node.getNodeName());
		System.out.print('>');
	}
}
/** Prints the specified node, then prints all of its children. */
public static void printDOMTreeHtmlSafe(PrintWriter out, Node node) {
	if (node == null) {
		out.println("printDOMTree - null node!");
		return;
	}
	//
	int type = node.getNodeType();
	switch (type) {
		// print the document element
		case Node.DOCUMENT_NODE :
			{
				out.println("&lt;?xml version=\"1.0\" ?&gt;");
				printDOMTreeHtmlSafe(out, ((Document) node).getDocumentElement());
				break;
			}
			// print element with attributes
		case Node.ELEMENT_NODE :
			{
				out.print("&lt;");
				out.print(node.getNodeName());
				NamedNodeMap attrs = node.getAttributes();
				for (int i = 0; i < attrs.getLength(); i++) {
					Node attr = attrs.item(i);
					out.print(" " + attr.getNodeName() + "=\"" + attr.getNodeValue() + "\"");
				}
				out.println("&gt;");
				NodeList children = node.getChildNodes();
				if (children != null) {
					int len = children.getLength();
					for (int i = 0; i < len; i++)
						printDOMTreeHtmlSafe(out, children.item(i));
				}
				break;
			}
			// handle entity reference nodes
		case Node.ENTITY_REFERENCE_NODE :
			{
				out.print("&");
				out.print(node.getNodeName());
				out.print(";");
				break;
			}
			// print cdata sections
		case Node.CDATA_SECTION_NODE :
			{
				out.print("&lt;![CDATA[");
				out.print(node.getNodeValue());
				out.print("]]&gt;");
				break;
			}
			// print text.
		case Node.TEXT_NODE :
			{
				out.print(node.getNodeValue());
				break;
			}
			// print processing instruction
		case Node.PROCESSING_INSTRUCTION_NODE :
			{
				out.print("&lt;?");
				out.print(node.getNodeName());
				String data = node.getNodeValue();
				{
					out.print("");
					out.print(data);
				}
				out.print("?&gt;");
				break;
			}
	}
	if (type == Node.ELEMENT_NODE) {
		out.println();
		out.print("&lt;/");
		out.print(node.getNodeName());
		out.print("&gt;");
	}
}
/**
 * XPath like query processor. Supported syntax is severely limited.
 * It starts with matching the the first token of the pattern with the
 * name of the current node, (XPath pattern starts at the immediate children
 * level, as a comparison) and go recursively into the descendants.
 *
 *
 * Creation date: (1/29/2000 7:04:17 PM)
 * @return Vector of Node. Null is not used... (for error later?)
 * @param node org.w3c.dom.Node
 * @param pattern java.lang.String. XPath like pattern
 * supports:
 * 	1. a/b[@att[="..."]]/c[@atte="..."]
 *  2. search for attribute: a/@b
 *  3. logical operator: =, !=
 */
private static Vector selectNodes(Node curNode, String pattern) {
	Vector nodes = new Vector();
	String curTK = null;
	String theRest = null;

	int sepPos = XMLUtil.getSepPos(pattern);

	if (sepPos >= 0) {
		curTK = pattern.substring(0, sepPos).trim();
		theRest = pattern.substring(sepPos + 1).trim();
	} else {
		curTK = pattern.trim();
	}

	//
	// basic format check
	// attr can only be the last token if an attr is on the path
	if (pattern.trim().endsWith("/") ||
		pattern.indexOf("@") == 0 && pattern.indexOf("/") > 0 ||
		curTK.indexOf("@") == 0 && theRest != null ||
		theRest != null && theRest.indexOf("@") == 0 && theRest.indexOf("/") > 0) {
		// better to throw expcetion...
		return nodes;
	}
	String curName = null;
	String attr = null;
	String attrVal = null;
	String operator = "?"; // "=", "!=", "?" - existance, default
	// check condition, [@att="...."] for now

	// parsing, regexp will help...
	int p = curTK.indexOf("[");
	if (p >= 0) {
		curName = curTK.substring(0, p).trim();
		String cond = curTK.substring(p + 1, curTK.indexOf("]")).trim();
		if (cond.indexOf("@") == 0) {
			if (cond.indexOf("=") > 1) { // value comparison. "=" and "!=", for now
				if (cond.indexOf("!=") > 1) { // NE
					operator = "!=";
				} else {
					operator = "=";
				}
				attr = cond.substring(cond.indexOf("@") + 1, cond.indexOf(operator)).trim();
				attrVal = cond.substring(cond.indexOf(operator) + operator.length()).replace('"', ' ').trim();
			} else { // test existence only
				operator = "?";
				attr = cond.substring(cond.indexOf("@") + 1).trim();
			}
		} else { // it's not attr related condition
			; // to be implemented
		}
	} else {
		curName = curTK;
	}
	// try matching current level
	boolean match = false;
	if (curName.equals(curNode.getNodeName())) {
		// see if need to match condition
		if (attr != null && curNode.getNodeType() == Node.ELEMENT_NODE) {
			if (operator.equals("=")) {
				if (attrVal.equals(((Element) curNode).getAttribute(attr))) {
					match = true;
				}
			} else {
				if (operator.equals("!=")) {
					if (!attrVal.equals(((Element) curNode).getAttribute(attr))) {
						match = true;
					}
				} else { // existance ((Element) curNode).getAttribute(attr)
					if (((Element) curNode).getAttributeNode(attr) != null) {
						match = true;
					}
				}
			}
		} else { // no extra condition
			match = true;
		}
	}
	if (match) {
		if (theRest != null && theRest.length() > 0) {
			Vector matches = null;
			// there's more work to do
			if (theRest.indexOf("@") == 0) { // find attr
				NamedNodeMap attrs = curNode.getAttributes();
				for (int m = 0; m < attrs.getLength(); ++m) {
					matches = selectNodes(attrs.item(m), theRest.substring(1));
					// add to the current collection of  matches
					for (int k = 0; k < matches.size(); ++k) {
						nodes.addElement(matches.elementAt(k));
					}
				}
			} else {
				NodeList nl = curNode.getChildNodes();
				for (int j = 0; j < nl.getLength(); ++j) {
					matches = selectNodes(nl.item(j), theRest);
					// add to the current collection of  matches
					for (int k = 0; k < matches.size(); ++k) {
						nodes.addElement(matches.elementAt(k));
					}
				}
			}
		} else {
			// found a match
			nodes.addElement(curNode);
		}
	}
	return nodes;
}
/**
 * XPath like query processor. Supported syntax is severely limited.
 * It starts to match in the children.
 *
 *
 * Creation date: (1/29/2000 7:04:17 PM)
 * @return Vector of Node. Null is not used... (for error later?)
 * @param node org.w3c.dom.Node
 * @param pattern java.lang.String. XPath like pattern
 * supports:
 * 	1. a/b[@att[="..."]]/c[@atte="..."]
 *  2. search for attribute: a/@b
 *  3. logical operator: =, !=
 */
public static Vector selectNodes2(Node curNode, String pattern) {
	Vector nodes = new Vector();
	if (curNode == null || pattern == null) {
		return nodes;
	}
	//
	String curTK = null;
	String theRest = null;
	//
	// TODO: add special processing for : "./", "/...", "../../", etc...
	//
	int sepPos = XMLUtil.getSepPos(pattern);
	if (sepPos >= 0) {
		curTK = pattern.substring(0, sepPos).trim();
		theRest = pattern.substring(sepPos + 1).trim();
	} else {
		curTK = pattern.trim();
	}

	//
	// basic format check
	// attr can only be the last token if an attr is on the path
	if (pattern.trim().endsWith("/") || pattern.indexOf("@") == 0 && pattern.indexOf("/") > 0 || curTK.indexOf("@") == 0 && theRest != null || theRest != null && theRest.indexOf("@") == 0 && theRest.indexOf("/") > 0) {
		// better to throw expcetion...
		return nodes;
	}
	String curName = null;
	String attr = null;
	String attrVal = null;
	String operator = "?"; // "=", "!=", "?" - existance, default
	// check condition, [@att="...."] for now

	// parsing, regexp will help...
	int p = curTK.indexOf("[");
	if (p >= 0) {
		curName = curTK.substring(0, p).trim();
		String cond = curTK.substring(p + 1, curTK.indexOf("]")).trim();
		if (cond.indexOf("@") == 0) {
			if (cond.indexOf("=") > 1) { // value comparison. "=" and "!=", for now
				if (cond.indexOf("!=") > 1) { // NE
					operator = "!=";
				} else {
					operator = "=";
				}
				attr = cond.substring(cond.indexOf("@") + 1, cond.indexOf(operator)).trim();
				attrVal = cond.substring(cond.indexOf(operator) + operator.length()).replace('"', ' ').trim();
			} else { // test existence only
				operator = "?";
				attr = cond.substring(cond.indexOf("@") + 1).trim();
			}
		} else { // it's not attr related condition
			; // to be implemented
		}
	} else {
		curName = curTK;
	}
	// --------------------- preparation work is done-------
	// try matching current level
	//

	// find out if the current token is for an element or an attribute
	if (curName.indexOf("@") == 0) { // find attr
		if (curNode.getNodeType() == Node.ELEMENT_NODE) {
			NamedNodeMap attrs = curNode.getAttributes();
			String att2Match = curName.substring(1);
			for (int m = 0; m < attrs.getLength(); ++m) {
				if (att2Match.equals(attrs.item(m).getNodeName())) {
					nodes.addElement(attrs.item(m));
					break; // att names are unique in an element
				}
			}
			// ignore the rest of the token
		}
	} else { // match an element
		if (".".equals(curName)) {
			// . always match the current node
			boolean match = false; if (attr != null && curNode.getNodeType() == Node.ELEMENT_NODE) {
				if (operator.equals("=")) {
					if (attrVal.equals(((Element) curNode).getAttribute(attr))) {
						match = true; }
				} else {
					if (operator.equals("!=")) {
						if (!attrVal.equals(((Element) curNode).getAttribute(attr))) {
							match = true; }
					} else { // existance only
						if (((Element) curNode).getAttributeNode(attr) != null) {
							match = true; }
					}
				}
			} else { // no extra condition
				match = true; }
			// go recursive?
			if (match) {
				if (theRest != null && theRest.length() > 0) {
					// there's more work to do
					Vector matches = selectNodes2(curNode, theRest);
					// add to the current collection of  matches
					for (int k = 0; k < matches.size(); ++k) {
						nodes.addElement(matches.elementAt(k)); }
				} else {
					// a macth found
					nodes.addElement(curNode); }
			}
		} else {
			NodeList nl = curNode.getChildNodes(); for (int j = 0; j < nl.getLength(); ++j) {
				if (curName.equals(nl.item(j).getNodeName()) || "*".equals(curName)) {
					// see if need to match condition
					boolean match = false; if (attr != null && nl.item(j).getNodeType() == Node.ELEMENT_NODE) {
						if (operator.equals("=")) {
							if (attrVal.equals(((Element) nl.item(j)).getAttribute(attr))) {
								match = true; }
						} else {
							if (operator.equals("!=")) {
								if (!attrVal.equals(((Element) nl.item(j)).getAttribute(attr))) {
									match = true; }
							} else { // existance only
								if (((Element) nl.item(j)).getAttributeNode(attr) != null) {
									match = true; }
							}
						}
					} else { // no extra condition
						match = true; }
					// go recursive?
					if (match) {
						if (theRest != null && theRest.length() > 0) {
							// there's more work to do
							Vector matches = selectNodes2(nl.item(j), theRest);
							// add to the current collection of  matches
							for (int k = 0; k < matches.size(); ++k) {
								nodes.addElement(matches.elementAt(k)); }
						} else {
							// a macth found
							nodes.addElement(nl.item(j)); }
					}
				}
			}
		}
	}
	return nodes;
}
/**
 * Insert the method's description here.
 * Creation date: (1/31/2000 1:59:20 AM)
 * @return java.util.Vector
 * @param xmlArg java.lang.String
 * @param pattern java.lang.String
 */
public static Vector selectNodesfromString(String xmlArg, String pattern) {
	Vector nodes = new Vector();
	try {
		Document doc = XMLUtil.string2Dom(xmlArg);
		if (doc != null) {
			nodes = XMLUtil.selectNodes(doc.getDocumentElement(), pattern);
		}
	} catch (Exception e) {
		System.err.println("Sorry, an error occurred: " + e);
	}
	return nodes;
}
/**
 * XPath like query processor. Supported syntax is severely limited.
 * It starts with matching the the first token of the pattern with the
 * name of the current node, (XPath pattern starts at the immediate children
 * level, as a comparison) and go recursively into the decendants.
 *
 *
 * Creation date: (1/29/2000 7:04:17 PM)
 * @return Node. null if node is not found
 * @param node org.w3c.dom.Node
 * @param pattern java.lang.String. XPath like pattern
 * supports:
 * 	1. a/b[@att[="..."]]/c[@atte="..."]
 *  2. search for attribute: a/@b
 *  3. logical operator: =, !=
 */
private static Node selectSingleNode(Node curNode, String pattern) {
	Node node = null;
	String curTK = null;
	String theRest = null;

	int sepPos = XMLUtil.getSepPos(pattern);

	if (sepPos < 0) {
		curTK = pattern.trim();
	} else {
		curTK = pattern.substring(0, sepPos).trim();
		theRest = pattern.substring(sepPos + 1).trim();
	}

	//
	// basic format check
	// attr can only be the last token if an attr is on the path
	if (pattern.trim().endsWith("/") || pattern.indexOf("@") == 0 && pattern.indexOf("/") > 0 || curTK.indexOf("@") == 0 && theRest != null || theRest != null && theRest.indexOf("@") == 0 && theRest.indexOf("/") > 0) {
		// better to throw expcetion...
		return node;
	}
	String curName = null;
	String attr = null;
	String attrVal = null;
	String operator = "?"; // "=", "!=", "?" - existance, default
	// check condition, [@att="...."] for now

	// parsing, regexp will help...
	int p = curTK.indexOf("[");
	if (p >= 0) {
		curName = curTK.substring(0, p).trim();
		String cond = curTK.substring(p + 1, curTK.indexOf("]")).trim();
		if (cond.indexOf("@") == 0) {
			if (cond.indexOf("=") > 1) { // value comparison. "=" and "!=", for now
				if (cond.indexOf("!=") > 1) { // NE
					operator = "!=";
				} else {
					operator = "=";
				}
				attr = cond.substring(cond.indexOf("@") + 1, cond.indexOf(operator)).trim();
				attrVal = cond.substring(cond.indexOf(operator) + operator.length()).replace('"', ' ').trim();
			} else { // test existence only
				operator = "?";
				attr = cond.substring(cond.indexOf("@") + 1).trim();
			}
		} else { // it's not attr related condition
			; // to be implemented
		}
	} else {
		curName = curTK;
	}
	// try matching current level
	boolean match = false;
	if (curName.equals(curNode.getNodeName())) {
		// see if we need to match condition
		if (attr != null && curNode.getNodeType() == Node.ELEMENT_NODE) {
			if (operator.equals("=")) {
				if (attrVal.equals(((Element) curNode).getAttribute(attr))) {
					match = true;
				}
			} else {
				if (operator.equals("!=")) {
					if (!attrVal.equals(((Element) curNode).getAttribute(attr))) {
						match = true;
					}
				} else { // existance ((Element) curNode).getAttribute(attr)
					if (((Element) curNode).getAttributeNode(attr) != null) {
						match = true;
					}
				}
			}
		} else { // no extra condition
			match = true;
		}
	}
	if (match) {
		if (theRest != null && theRest.length() > 0) {
			// there's more work to do
			Node matched = null;
			if (theRest.indexOf("@") == 0) { // find attr
				NamedNodeMap attrs = curNode.getAttributes();
				for (int m = 0; m < attrs.getLength(); ++m) {
					matched = selectSingleNode(attrs.item(m), theRest.substring(1));
					// add to the current collection of  matches
					if (matched != null) {
						break; // stop now
					}
				}
			} else {
				NodeList nl = curNode.getChildNodes();
				for (int j = 0; j < nl.getLength(); ++j) {
					matched = selectSingleNode(nl.item(j), theRest);
					// add to the current collection of  matches
					if (matched != null) {
						break; // stop now
					}
				}
			}
			node = matched;
		} else {
			// found a match.
			// Since we're interested in onlu one node, we want to interrupt the recursion now
			node = curNode;
		}
	}

⌨️ 快捷键说明

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