📄 xmlutil.java
字号:
return node;
}
/**
* 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: =, !=
*/
public static Node selectSingleNode2(Node curNode, String pattern) {
Node node = null;
if (curNode == null || pattern == null) {
return 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
// 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())) {
node = attrs.item(m);
break;
}
}
// 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
Node aMatch = selectSingleNode2(curNode, theRest);
// add to the current collection of matches
if (aMatch != null) {
node = aMatch;
}
} else {
// a macth found
node = 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;
}
if (match) {
// go recursive?
if (theRest != null && theRest.length() > 0) {
// there's more work to do
Node aMatch = selectSingleNode2(nl.item(j), theRest);
// add to the current collection of matches
if (aMatch != null) {
node = aMatch;
break;
}
} else {
// a match found
node = nl.item(j);
break;
}
}
}
}
}
}
return node;
}
/**
* 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
*/
private static String selectSingleValue(String xmlArg, String pattern) {
Node node = null;
String value = null;
try {
Document doc = XMLUtil.string2Dom(xmlArg);
if (doc != null) {
node = XMLUtil.selectSingleNode(doc.getDocumentElement(), pattern);
}
} catch (Exception e) {
System.err.println("Sorry, an error occurred: " + e);
}
if (node != null) {
switch (node.getNodeType()) {
case Node.ELEMENT_NODE :
Node child = node.getFirstChild();
if ("#text".equals(child.getNodeName())) {
value = child.getNodeValue();
}
break;
case Node.ATTRIBUTE_NODE :
value = node.getNodeValue();
break;
default :
break;
}
}
return value;
}
/**
* 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
*/
private static Object selectSingleValue(String xmlArg, String pattern, int dataType) {
String valueString = selectSingleValue(xmlArg, pattern);
if (valueString == null || valueString.length() == 0) {
return null;
}
Object o = null;
switch (dataType) {
case STRING:
o = valueString;
break;
case INTEGER:
try {
o = new Integer(valueString);
}
catch (NumberFormatException ne) {
System.out.println("selectSingleValue - " + ne);
}
break;
case LONG:
try {
o = new Long(valueString);
}
catch (NumberFormatException ne) {
System.out.println("selectSingleValue - " + ne);
}
break;
case FLOAT:
break;
case DOUBLE:
break;
case CHAR:
break;
case BOOLEAN:
break;
case CALENDAR:
// assmue that input is a long number representing the time
// otherwise I need to use local to convert a string to time
try {
long l = Long.parseLong(valueString);
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(l));
o = cal;
}
catch (NumberFormatException ne) {
System.out.println("selectSingleValue - " + ne);
}
break;
default:
break;
}
return o;
}
/**
* 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
*/
private static String selectSingleValue(Node node, String pattern) {
Node matchedNode = null;
String value = null;
try {
matchedNode = XMLUtil.selectSingleNode(node, pattern);
} catch (Exception e) {
System.err.println("Sorry, an error occurred: " + e);
}
if (matchedNode != null) {
switch (matchedNode.getNodeType()) {
case Node.ELEMENT_NODE :
Node child = matchedNode.getFirstChild();
if ("#text".equals(child.getNodeName())) {
value = child.getNodeValue();
}
break;
case Node.ATTRIBUTE_NODE :
value = matchedNode.getNodeValue();
break;
default :
break;
}
}
return value;
}
/**
* 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 String selectSingleValue2(String xmlArg, String pattern) {
Node node = null;
String value = null;
try {
Document doc = XMLUtil.string2Dom(xmlArg);
if (doc != null) {
node = XMLUtil.selectSingleNode2(doc.getDocumentElement(), pattern);
}
} catch (Exception e) {
System.err.println("Sorry, an error occurred: " + e);
}
value = getNodeTextValue(node);
return value;
}
/**
* 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 Object selectSingleValue2(String xmlArg, String pattern, int dataType) {
String valueString = selectSingleValue2(xmlArg, pattern);
if (valueString == null || valueString.length() == 0) {
return null;
}
Object o = null;
switch (dataType) {
case STRING:
o = valueString;
break;
case INTEGER:
try {
o = new Integer(valueString);
}
catch (NumberFormatException ne) {
System.out.println("selectSingleValue - " + ne);
}
break;
case LONG:
try {
o = new Long(valueString);
}
catch (NumberFormatException ne) {
System.out.println("selectSingleValue - " + ne);
}
break;
case FLOAT:
break;
case DOUBLE:
break;
case CHAR:
break;
case BOOLEAN:
break;
case CALENDAR:
// assmue that input is a long number representing the time
// otherwise I need to use local to convert a string to time
try {
long l = Long.parseLong(valueString);
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(l));
o = cal;
}
catch (NumberFormatException ne) {
System.out.println("selectSingleValue - " + ne);
}
break;
default:
break;
}
return o;
}
/**
* 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 String selectSingleValue2(Node node, String pattern) {
Node matchedNode = null;
String value = null;
try {
matchedNode = XMLUtil.selectSingleNode2(node, pattern);
} catch (Exception e) {
System.err.println("Sorry, an error occurred: " + e);
}
value = getNodeTextValue(matchedNode);
return value;
}
/**
* 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
*/
private static Vector selectValues(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);
}
//
Vector values = new Vector();
Node node;
for (int i = 0; i < nodes.size(); ++i) {
node = ((Node) nodes.elementAt(i)).getFirstChild();
if ("#text".equals(node.getNodeName())) {
values.addElement(node.getNodeValue());
}
}
return values;
}
/**
* 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 selectValues2(String xmlArg, String pattern) {
Vector nodes = new Vector();
try {
Document doc = XMLUtil.string2Dom(xmlArg);
if (doc != null) {
nodes = XMLUtil.selectNodes2(doc.getDocumentElement(), pattern);
}
} catch (Exception e) {
System.err.println("Sorry, an error occurred: " + e);
}
//
Vector values = new Vector();
Node node;
for (int i = 0; i < nodes.size(); ++i) {
values.addElement(XMLUtil.getNodeTextValue((Node)nodes.elementAt(i)));
}
return values;
}
/**
* 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 selectValues2(Node node, String pattern) {
Vector nodes = new Vector();
try {
nodes = XMLUtil.selectNodes2(node, pattern);
} catch (Exception e) {
System.err.println("Sorry, an error occurred: " + e);
}
//
Vector values = new Vector();
for (int i = 0; i < nodes.size(); ++i) {
values.addElement(XMLUtil.getNodeTextValue((Node)nodes.elementAt(i)));
}
return values;
}
public static Document string2Dom(String xmlArg) {
Document doc = null;
try {
StringReader sr = new StringReader(xmlArg);
InputSource iSrc = new InputSource(sr);
DOMParser parser = new DOMParser();
parser.parse(iSrc);
doc = parser.getDocument();
if (doc == null) {
System.err.println("null doc?");
}
} catch (Exception e) {
System.err.println("string2Dom - Sorry, an error occurred: " + e);
}
return doc;
}
/**
* All the names starting with "_" are ignored!
* Creation date: (1/31/2000 2:10:05 AM)
* @return java.lang.String
* @param enum java.util.Enumeration
*/
public static String stringToXMLString(String source) {
StringBuffer buf = new StringBuffer();
int sLength = source.length();
for (int i = 0; i < sLength; i++) {
buf.append( char2XMLString( source.charAt(i) ) );
}
return buf.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -