Java XML Element Get by Name getElement(Document owner, String elementName, String nsURI)

Here you can find the source of getElement(Document owner, String elementName, String nsURI)

Description

Returns the Element object corresponding to the given element name.

License

Open Source License

Parameter

Parameter Description
owner Document object to search.
elementName local name to search for.
nsURI namespaceURI to used (may be null).

Return

the Element object corresponding to the given element name or null if none is found.

Declaration

public static Element getElement(Document owner, String elementName, String nsURI) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

public class Main {
    /**//from  w w w. j a va  2s  .c  om
     * Returns the Element object corresponding to the given element name.
     *
     * @param owner       Document object to search.
     * @param elementName local name to search for.
     * @param nsURI       namespaceURI to used (may be null).
     * @return the Element object corresponding to the given element name or
     *         <code>null</code> if none is found.
     */
    public static Element getElement(Document owner, String elementName, String nsURI) {
        NodeList nodes = nsURI != null ? owner.getElementsByTagNameNS(nsURI, elementName)
                : owner.getElementsByTagName(elementName);

        if (nodes != null && nodes.getLength() > 0) {
            return ((Element) nodes.item(0));
        }

        return (null);
    }
}

Related

  1. getElement(Document doc, String tagName, int index)
  2. getElement(Document doc, String tagName, int index)
  3. getElement(Document document, Element parent, String element)
  4. getElement(Document document, String elementName)
  5. getElement(Document parent, String name)
  6. getElement(Element parent, String elementName)
  7. getElement(Element parent, String elementName)
  8. getElement(Element parent, String localname, int position)