Java Utililty Methods XML Node Find

List of utility methods to do XML Node Find

Description

The list of methods to do XML Node Find are organized into topic(s).

Method

NodefindNodeByTagName(Document document, String tagName)
find Node By Tag Name
Node foundNode = null;
NodeList nodes = document.getElementsByTagName(tagName);
if (nodes.getLength() > 0)
    foundNode = nodes.item(0);
return foundNode;
TfindNodeByXpath(org.w3c.dom.Document doc, String xpathEx)
find Node By Xpath
T userNode = null;
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile(xpathEx);
org.w3c.dom.Node result = (org.w3c.dom.Node) expr.evaluate(doc, XPathConstants.NODE);
if (result != null) {
    userNode = (T) result.getUserData(USER_KEY_NODE);
return userNode;
intfindNodeIndex(Node node)
find Node Index
String nm = node.getLocalName();
String ns = node.getNamespaceURI();
short nt = node.getNodeType();
Node parentNode = node.getParentNode();
if (parentNode.getNodeType() != Node.ELEMENT_NODE) {
    return 1;
Node child = parentNode.getFirstChild();
...
intfindNodeIndex(Node node)
find Node Index
String nm = node.getLocalName();
String ns = node.getNamespaceURI();
short nt = node.getNodeType();
Node parentNode = node.getParentNode();
if (parentNode.getNodeType() != Node.ELEMENT_NODE)
    return 1;
Node child = parentNode.getFirstChild();
int ix = 0;
...
longfindNodeLong(Node node)
find Node Long
long value = 0;
String text = findNodeText(node);
if (text != null && !text.equals(""))
    value = Long.parseLong(text);
return value;
Node[]findNodesByTagName(Document document, String tagName)
find Nodes By Tag Name
ArrayList<Node> nodes = new ArrayList<Node>();
NodeList foundNodes = document.getElementsByTagName(tagName);
for (int i = 0; i < foundNodes.getLength(); i++)
    nodes.add(foundNodes.item(i));
Node[] returnNodes = new Node[nodes.size()];
return nodes.toArray(returnNodes);
voidfindNodesNamed(Node node, String lookForName, Collection ret)
find Nodes Named
if (node.getNodeName().equals(lookForName)) {
    ret.add(node);
} else {
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); ++i) {
        findNodesNamed(list.item(i), lookForName, ret);
StringfindNodeValue(Node aNode, String aName)
------------------------------------------------------------ Gets the value of the named node
String value = null;
Node node = findNode(aNode, aName);
if (node != null) {
    value = getNodeValue(node);
return value;
NodefindNodeWithAttrValue(Document doc, String elementName, String attrName, String attrValue)
find Node With Attr Value
NodeList elmts = doc.getElementsByTagNameNS("*", elementName);
;
for (int i = 0; i < elmts.getLength(); i++) {
    Node n = elmts.item(i);
    if (n == null) {
        continue; 
    NamedNodeMap attrs = n.getAttributes();
...