Java Utililty Methods XML Node Sibiling

List of utility methods to do XML Node Sibiling

Description

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

Method

NodegetPreviousSiblingByName(Node currentNode, String tagName)
Search earlier siblings for a given node
Node node = currentNode.getPreviousSibling();
while ((node != null) && (!node.getNodeName().equals(tagName))) {
    node = node.getPreviousSibling();
return node;
NodegetPreviousSiblingElement(Node node)
get Previous Sibling Element
if (node == null)
    return null;
Node prevSibling = node.getPreviousSibling();
while ((prevSibling != null) && (prevSibling.getNodeType() != Node.ELEMENT_NODE))
    prevSibling = prevSibling.getPreviousSibling();
if ((prevSibling != null) && (prevSibling.getNodeType() == Node.ELEMENT_NODE))
    return prevSibling;
return null;
...
ElementgetPreviousSiblingElement(Node node)
Get the previous sibling element.
Node n = node.getPreviousSibling();
while (n != null && n.getNodeType() != Node.ELEMENT_NODE) {
    n = n.getPreviousSibling();
return (Element) n;
NodegetSibling(Node current)
Get the sibling in next context
Node sibling = current.getNextSibling();
Node tmpParent = current;
while (tmpParent != null && sibling == null) {
    tmpParent = tmpParent.getParentNode();
    if (tmpParent != null) {
        sibling = tmpParent.getNextSibling();
return sibling;
MapgetSiblingValues(Node currentNode, Set siblingTags)
This method looks at the siblings of a given node, matches the ones in the passed Set, then returns a map of the matches with their values
Map<String, String> responseMap = new HashMap<String, String>();
String testString = currentNode.getLocalName().toLowerCase();
for (String tagName : siblingTags) {
    if (testString.contains(tagName.toLowerCase())) {
        responseMap.put(tagName, currentNode.getTextContent().trim());
        return responseMap;
return responseMap;