|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectnet.wangs.xmlutil.XMLUtil
XMLUtil constructs from either an xml file or a node, and returns detail information of
node by directory style path into easy-to-use data formats, such as String, int, Hashtable
of String, etc.
Demonstration of the usage of XMLUtil:
try {
String path = "/AAA/BBB/CCC";
// You can directly construct an XMLUtil object from XPath like below:
// XMLUtil util = new XMLUtil("./config/sample.xml");
// Vector nodes = util.getNodes(path);
// Or you can construct an XMLUtil object from a node (normally also from XMLUtil)
Vector nodes = XMLUtil.getNodes(path);
for(int i=0; i<nodes.size(); i++) {
Node node = (Node)nodes.elementAt(i);
XMLUtil xMLUtil = new XMLUtil(node);
String val = xMLUtil.getNodeValue("/BBB/CCC", 1);
Hashtable hash = xMLUtil.getChildNodeHashValues("/CCC");
}
} catch (Exception e) {
e.printStackTrace();
}
| Constructor Summary | |
XMLUtil(org.w3c.dom.Node node)
Constructor: constructing internal element from Node object. |
|
XMLUtil(java.lang.String fileName)
Constructor: constructing internal element from XML file |
|
| Method Summary | |
java.util.Hashtable |
getAttrValues(java.lang.String nodeName,
int index)
Given a node name (with path), returns all attributes of this node with name-value pairs. |
java.util.Hashtable |
getAttrValuesHashedByNamedAttr(java.lang.String nodeName,
java.lang.String val_attrName,
java.lang.String key_attrName)
Given node name (with path) and two attribute's names, returns a Hashtable with key is one attribute's value and value is another attribute's value. |
MultiKeyHashtable |
getAttrValuesHashedByNamedAttrs(java.lang.String nodeName,
java.lang.String val_attrName,
java.lang.String[] key_attrName)
Given node name (with path), an attribute name and an array of attribute names, returns a MultiKeyHashtable with keys from two or more attributes and value from another attribute. |
java.util.Hashtable |
getChildNodeHashValues(java.lang.String nodeName)
Given a node name, return all child nodes' name-value pairs into a hashtable. |
java.lang.String |
getNamedAttrValue(java.lang.String nodeName,
java.lang.String attrName,
int index)
Given a node name (with path) and atrribute name, returns attribute value. |
org.w3c.dom.Node |
getNode(java.lang.String nodeName,
int index)
Return node for given node name and index |
org.w3c.dom.Node |
getNodeByAttrValue(java.lang.String nodeName,
java.lang.String attrName,
java.lang.String attrValue)
Return node for given node name and one given attribute values If there are multiple nodes satisfied this condition, the first one is returned. |
int |
getNodeCount(java.lang.String nodeName)
Given a node name (with path), returns number of nodes with the same name under the same path. |
java.util.Vector |
getNodes(java.lang.String nodeName)
Return all nodes for given node name with path. |
java.util.Vector |
getNodesByAttrValue(java.lang.String nodeName,
java.lang.String attrName,
java.lang.String attrValue)
Return Vector of nodes for given node name and one given attribute values More complicated case is to give a hashtable of attribute name-value pairs, but we are not going to provide this since it's rarely used. |
java.util.Hashtable |
getNodesHashedByNamedAttr(java.lang.String nodeName,
java.lang.String attrName)
Given a node name (with path) and one attribute, returns Hashtable of all nodes with the same name in the same path. |
MultiKeyHashtable |
getNodesHashedByNamedAttrs(java.lang.String nodeName,
java.lang.String[] attrNames)
Given a node name (with path) and attributes array, returns MultiKeyHashtable of all nodes with the same name in the same path. |
java.lang.String |
getNodeValue(java.lang.String nodeName,
int index)
For a known node, get its value. |
java.lang.String |
getNodeValueByAttrValue(java.lang.String nodeName,
java.lang.String attrName,
java.lang.String attrValue)
Return Vector of nodes for given node name and one given attribute values A common usage of this method is: to obtain a subset of nodes from a set of sibling nodes. |
java.util.Vector |
getNodeValues(java.lang.String nodeName)
Return all node values for given node name with path. |
java.util.Hashtable |
getNodeValuesAsHashtable(java.lang.String nodeName)
Return all node values in a Hashtable for given node name with path. |
java.util.Hashtable |
getNodeValuesHashedByNamedAttr(java.lang.String nodeName,
java.lang.String attrName)
Given a node name (with path) and one attribute, returns Hashtable of all nodes with the same name in the same path. |
MultiKeyHashtable |
getNodeValuesHashedByNamedAttrs(java.lang.String nodeName,
java.lang.String[] attrNames)
Given a node name (with path) and attributes array, returns all node values with the same name in the same path. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
public XMLUtil(org.w3c.dom.Node node)
throws XMLUtilException
public XMLUtil(java.lang.String fileName)
throws XMLUtilException
| Method Detail |
public java.util.Vector getNodes(java.lang.String nodeName)
throws XMLUtilException
nodeName -
XMLUtilException
public java.util.Vector getNodeValues(java.lang.String nodeName)
throws XMLUtilException
XMLUtilException
public java.util.Hashtable getNodeValuesAsHashtable(java.lang.String nodeName)
throws XMLUtilException
XMLUtilException
public org.w3c.dom.Node getNode(java.lang.String nodeName,
int index)
throws XMLUtilException
index - - the index number of node that satisfies the nodeName, started from 1, top-down
XMLUtilException
public java.util.Vector getNodesByAttrValue(java.lang.String nodeName,
java.lang.String attrName,
java.lang.String attrValue)
throws XMLUtilException
nodeName - - name of the node with full pathattrName - - name of attribute in the nodeattrValue - - value of the given attribute
XMLUtilException
public org.w3c.dom.Node getNodeByAttrValue(java.lang.String nodeName,
java.lang.String attrName,
java.lang.String attrValue)
throws XMLUtilException
If there are multiple nodes satisfied this condition, the first one is returned.
A common usage of this method is: to obtain a particular node from a set of sibling nodes.
All nodes have the same tag name and attribute names. The only way to differentiate one
from them is the attribute value. For example,
<service name="ServiceAAA">
// data belongs to ServiceAAA
</service>
<service name="ServiceBBB">
// data belongs to ServiceBBB
</service>
<service name="ServiceCCC">
// data belongs to ServiceCCC
</service>
More complicated case is to give a hashtable of attribute name-value pairs, but
we are not going to provide this since it's rarely used.
nodeName - - name of the node with full pathattrName - - name of attribute in the nodeattrValue - - value of the given attribute
XMLUtilException
public java.lang.String getNodeValueByAttrValue(java.lang.String nodeName,
java.lang.String attrName,
java.lang.String attrValue)
throws XMLUtilException
A common usage of this method is: to obtain a subset of nodes from a set of sibling nodes.
All nodes have the same tag name and attribute names. The only way to differentiate desired
ones from them is the attribute value. For example,
<service name="ServiceAAA">
// data belongs to ServiceAAA
</service>
<service name="ServiceBBB">
// data belongs to ServiceBBB
</service>
<service name="ServiceCCC">
// data belongs to ServiceCCC
</service>
More complicated case is to give a hashtable of attribute name-value pairs, but
we are not going to provide this since it's rarely used.
nodeName - - name of the node with full pathattrName - - name of attribute in the nodeattrValue - - value of the given attribute
XMLUtilException
public java.lang.String getNodeValue(java.lang.String nodeName,
int index)
throws XMLUtilException
index - - the index number of node that satisfies the nodeName
XMLUtilException
public int getNodeCount(java.lang.String nodeName)
throws XMLUtilException
XMLUtilException
public java.util.Hashtable getAttrValues(java.lang.String nodeName,
int index)
throws XMLUtilException
index - - the index number of node that satisfies the nodeName
XMLUtilException
public java.lang.String getNamedAttrValue(java.lang.String nodeName,
java.lang.String attrName,
int index)
throws XMLUtilException
attrName - - name of the attribute in this nodeindex - - the index number of node that satisfies the nodeName
XMLUtilException
public MultiKeyHashtable getNodesHashedByNamedAttrs(java.lang.String nodeName,
java.lang.String[] attrNames)
throws XMLUtilException
"/AAA/BBB/CCC"
is defined as:
...
<CCC>
<DDD attrA="a1" attrB="b1">
<EEE>Value1 of EEE</EEE>
<FFF/>
</DDD>
<DDD attrA="a1" attrB="b2">
<EEE>Value2 of EEE</EEE>
<FFF/>
</DDD>
<DDD attrA="a2" attrB="b1">
<EEE>Value3 of EEE</EEE>
<FFF/>
</DDD>
</CCC>
...
Then
getNodeValuesHashedByNamedAttrs("/AAA/BBB/CCC", new String[] {"attrA", "attrB"})
returns:
Keys Values
-------------------------- -------------------------
{"a1", "b1"} first node of DDD
{"a1", "b2"} second node of DDD
{"a2", "b1"} third node of DDD
XMLUtilException
public MultiKeyHashtable getNodeValuesHashedByNamedAttrs(java.lang.String nodeName,
java.lang.String[] attrNames)
throws XMLUtilException
"/AAA/BBB/CCC"
is defined as:
...
<CCC>
<DDD attrA="a1" attrB="b1">v1</DDD>
<DDD attrA="a2" attrB="b2">v2</DDD>
<DDD attrA="a3" attrB="b3">v3</DDD>
<DDD attrA="a4" attrB="b4">v4</DDD>
<DDD attrA="a5" attrB="b5">v5</DDD>
</CCC>
...
Then getNodeValuesHashedByNamedAttrs("/AAA/BBB/CCC", new String[] {"attrA", "attrB"})
returns:
Keys Values
-------------------------- -------------------------
{"a1", "b1"} v1
{"a2", "b2"} v2
{"a3", "b3"} v3
{"a4", "b4"} v4
{"a5", "b5"} v5
This method is a sub case of getNodesHashedByNamedAttrs(), in case the node is leaf.
XMLUtilException
public java.util.Hashtable getNodesHashedByNamedAttr(java.lang.String nodeName,
java.lang.String attrName)
throws XMLUtilException
"/AAA/some-tag"
is defined as:
...
<some-tag>
<some-node attrA="a1">
<sub-node>value1</sub-node>
</some-node>
<some-node attrA="a2">
<sub-node>value2</sub-node>
</some-node>
<some-node attrA="a3">
<sub-node>value3</sub-node>
</some-node>
</some-tag>
...
Then
getNodeValuesHashedByNamedAttr("/AAA/some-tag", "attrA")
returns:
Keys Values
-------------------------- -------------------------
a1 first node of some-node
a2 second node of some-node
a3 third node of some-node
XMLUtilException
public java.util.Hashtable getNodeValuesHashedByNamedAttr(java.lang.String nodeName,
java.lang.String attrName)
throws XMLUtilException
"/AAA/some-tag"
is defined as:
...
<some-tag>
<some-node attrA="a1">
<sub-node>value1</sub-node>
</some-node>
<some-node attrA="a2">
<sub-node>value2</sub-node>
</some-node>
<some-node attrA="a3">
<sub-node>value3</sub-node>
</some-node>
</some-tag>
...
Then
getNodeValuesHashedByNamedAttr("/AAA/some-tag", "attrA")
returns:
Keys Values
-------------------------- -------------------------
a1 value1
a2 value2
a3 value3
XMLUtilException
public java.util.Hashtable getAttrValuesHashedByNamedAttr(java.lang.String nodeName,
java.lang.String val_attrName,
java.lang.String key_attrName)
throws XMLUtilException
XMLUtilException
public MultiKeyHashtable getAttrValuesHashedByNamedAttrs(java.lang.String nodeName,
java.lang.String val_attrName,
java.lang.String[] key_attrName)
throws XMLUtilException
XMLUtilException
public java.util.Hashtable getChildNodeHashValues(java.lang.String nodeName)
throws XMLUtilException
XMLUtilException
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||