Java XML Element Get by Name getElementByTagAndName(Document document, String tagName, String elementName)

Here you can find the source of getElementByTagAndName(Document document, String tagName, String elementName)

Description

get Element By Tag And Name

License

BSD License

Declaration

public static Element getElementByTagAndName(Document document,
            String tagName, String elementName) 

Method Source Code

//package com.java2s;
/*L//from  w  ww. j av a 2s.  c om
 * Copyright Georgetown University, Washington University.
 *
 * Distributed under the OSI-approved BSD 3-Clause License.
 * See http://ncip.github.com/cab2b/LICENSE.txt for details.
 */

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

public class Main {
    public static Element getElementByTagAndName(Document document,
            String tagName, String elementName) {
        NodeList nodeList = document.getElementsByTagName(tagName);
        Element element = null;
        if (elementName != null) {
            Element _element = null;
            for (int nodeIndex = 0; nodeIndex < nodeList.getLength(); nodeIndex++) {
                _element = (Element) nodeList.item(nodeIndex);
                String name = _element.getAttribute("name");
                if (name != null && name.equals(elementName)) {
                    element = _element;
                    break;
                }
            }
        } else {
            element = (Element) document.getElementsByTagName(tagName)
                    .item(0);
        }
        return element;
    }
}

Related

  1. getElement(String name, int index, Element parent)
  2. getElementByName(Document document, String name)
  3. getElementByName(Element eParent, String elementName)
  4. getElementByName(Element parent, String nodeName)
  5. getElementByTag(HTMLDocument htmlDoc, int offset, Tag tag)
  6. getElementByTagName(Document doc, String eleName)
  7. getElementByTagName(Document document, String tagName)
  8. getElementByTagName(Element aParent, String aName)
  9. getElementByTagName(Element parent, String elementName)