Java XML First Child Element getFirstChildByTagNameNS(Node contextNode, String nsuri, String tag)

Here you can find the source of getFirstChildByTagNameNS(Node contextNode, String nsuri, String tag)

Description

returns the first child of the contextnode which has the specified tagname and namespace uri regardless of the depth in the tree.

License

BSD License

Parameter

Parameter Description
contextNode where to start the search
nsuri the namespace uri
tag the local name part of the wanted child

Return

the first child found under the contextnode

Declaration

public static Node getFirstChildByTagNameNS(Node contextNode, String nsuri, String tag) 

Method Source Code


//package com.java2s;
/*//from w ww.j  a v  a  2 s  .c  o  m
 * Copyright (c) 2012. betterFORM Project - http://www.betterform.de
 * Licensed under the terms of BSD License
 */

import org.w3c.dom.*;

public class Main {
    /**
     * returns the first child of the contextnode which has the specified tagname and namespace uri regardless of the
     * depth in the tree.
     *
     * @param contextNode where to start the search
     * @param nsuri       the namespace uri
     * @param tag         the local name part of the wanted child
     * @return the first child found under the contextnode
     */
    public static Node getFirstChildByTagNameNS(Node contextNode, String nsuri, String tag) {
        Node n = null;

        if (contextNode.getNodeType() == Node.DOCUMENT_NODE) {
            n = ((Document) contextNode).getDocumentElement();

            if (!(n.getNamespaceURI().equals(nsuri) && n.getNodeName().equals(tag))) {
                n = null;
            }
        } else {
            NodeList nodes = ((Element) contextNode).getElementsByTagNameNS(nsuri, tag);

            if (nodes != null) {
                n = nodes.item(0);
            }
        }

        return n;
    }
}

Related

  1. getFirstChildByNodeName(Node parent, String nodeName)
  2. getFirstChildByNS(Element node, String ns, String localName)
  3. getFirstChildByRegex(Element ele, String pattern)
  4. getFirstChildByTagName(Element parent, String tagName)
  5. getFirstChildByTagName(Node parent, String tagName)
  6. getFirstChildByType(Element element, int nodeType)
  7. getFirstChildByType(Element element, short nodeType)
  8. getFirstChildElement(@Nonnull final Node aStartNode)
  9. getFirstChildElement(Element e)