Java Utililty Methods XML Element Get from Parent

List of utility methods to do XML Element Get from Parent

Description

The list of methods to do XML Element Get from Parent are organized into topic(s).

Method

StringgetNamespacePrefix(Node parentNode, String preferredPrefix, String nsUri)
Traverse a node, and parent nodes, for a namespace URI.
Node currentNode = parentNode;
do {
    NamedNodeMap nodeMap = currentNode.getAttributes();
    for (int i = 0; nodeMap != null && i < nodeMap.getLength(); i++) {
        Node item = nodeMap.item(i);
        String itemName = item.getNodeName();
        if (itemName.startsWith("xmlns")) { 
            String uri = getTextValue(item);
...
NodegetNameToFirstNode(Node parent, String node_name)
get Name To First Node
if (parent == null || node_name == null)
    return null;
Node[] nodes = getNameToNodeList(parent, node_name);
return node_name.length() > 0 ? nodes[0] : null;
Node[]getNameToNodeList(Node parent, String node_name)
get Name To Node List
List<Node> nodes = new ArrayList<>();
if (parent == null || node_name == null)
    return nodes.toArray(new Node[0]);
NodeList list = parent.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
    Node node = list.item(i);
    if (node.getNodeName().equals(node_name)) {
        nodes.add(node);
...
NodegetNodeAtPosition(Node parent, int offset)
get Node At Position
if (offset < 0) {
    return null;
} else if (offset == 0) {
    return parent.getFirstChild();
NodeList nodes = parent.getChildNodes();
int count = nodes.getLength();
if (offset < count) {
...
MapgetNodeBean(Node parent)
get Node Bean
Map<Object, Object> rtn = new HashMap<Object, Object>();
if (parent != null) {
    Map<Object, Object> attrMap = new HashMap<Object, Object>();
    if (parent.hasAttributes()) {
        NamedNodeMap attrs = parent.getAttributes();
        for (int j = 0; j < attrs.getLength(); j++) {
            Node attr = attrs.item(j);
            attr.getNodeName();
...
NodegetNodeByLocalName(final Node parent, final String name)
Return the first node in the given node children which localName matchs the given name.
if (name.equalsIgnoreCase(parent.getLocalName()))
    return parent;
final NodeList lst = parent.getChildNodes();
for (int i = 0, n = lst.getLength(); i < n; i++) {
    final Node child = lst.item(i);
    if (name.equalsIgnoreCase(child.getLocalName())) {
        return child;
return null;
StringgetNodeContents(Node parentNode)
get Node Contents
StringBuilder sb = new StringBuilder();
NodeList childNodes = parentNode.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
    Node node = childNodes.item(i);
    String nodeValue = node.getNodeValue();
    if (nodeValue != null)
        sb.append(nodeValue);
return sb.toString();
StringgetNodeContents(String nodeName, Node parentNode)
get Node Contents
String nodeContents = null;
NodeList childNodes = parentNode.getChildNodes();
int totalChildNodes = childNodes.getLength();
for (int i = 0; i < totalChildNodes; i++) {
    Node childNode = childNodes.item(i);
    nodeContents = getNodeContents(nodeName, childNode);
    if (nodeContents != null) {
        break;
...
NodeListgetNodeList(Element parent)
Get all child nodes of a parent node
return parent.getChildNodes();
ListgetNodes(final Node parent, final String nodeName)
Scans a node and all of its children for nodes of a particular type.
final List<Node> nodes = new ArrayList<Node>();
final NodeList children = parent.getChildNodes();
for (int i = 0; i < children.getLength(); ++i) {
    final Node child = children.item(i);
    if (child.getNodeName().equals(nodeName)) {
        nodes.add(child);
    } else {
        nodes.addAll(getNodes(child, nodeName));
...