Java XML Element Find findElement(final String idValue, final String idTagName, final String tagName, final Element root)

Here you can find the source of findElement(final String idValue, final String idTagName, final String tagName, final Element root)

Description

find element with tagName whose id tag is 'idTagName' and id value is 'id_value'

License

Apache License

Parameter

Parameter Description
root a parameter

Declaration

public static Element findElement(final String idValue, final String idTagName, final String tagName,
        final Element root) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import org.w3c.dom.*;

public class Main {
    /**/*from  w w w.ja va  2  s  .c  om*/
     * find element with tagName whose id tag is 'idTagName' and id value is 'id_value'
     *
     * @param root
     */
    public static Element findElement(final String idValue, final String idTagName, final String tagName,
            final Element root) {
        String textVal = null;
        final NodeList nl = root.getElementsByTagName(tagName);
        if (nl != null && nl.getLength() > 0) {
            // iterate over these
            for (int i = 0; i < nl.getLength(); i++) {
                final Element ei = (Element) nl.item(i);
                final NodeList n2 = ei.getElementsByTagName(idTagName);
                if (n2 != null && n2.getLength() > 0) {
                    for (int j = 0; j < n2.getLength(); j++) {
                        textVal = getElementValue((Element) n2.item(j));
                        if (textVal.equals(idValue)) {
                            return ei;
                        }
                    }
                }
            }
        }
        return null;
    }

    /**
     * find the unique element, returns null if not found or if there is more than one
     *
     * @param tagName - tag name
     * @param root    - where to start looking
     * @return the element
     */
    public static Element findElement(final String tagName, final Element root) {
        if (root.getTagName().equals(tagName)) {
            return root;
        }
        final NodeList nl = root.getElementsByTagName(tagName);
        if (nl != null && nl.getLength() == 1) {
            return (Element) nl.item(0);
        }
        return null;
    }

    /**
     * returns the text value associated with the element
     *
     * @param target - the element
     * @return - the text value
     */
    public static String getElementValue(final Element target) {

        final NodeList nodeList = target.getChildNodes();
        if (nodeList == null) {
            return null;
        }
        for (int current = 0; current < nodeList.getLength(); current++) {
            final Node node = nodeList.item(current);
            if (node instanceof Text) {
                final Text text = (Text) node;
                final String value = text.getNodeValue();
                if ((value != null) && (!value.isEmpty())) {
                    return value;
                }
            }
        }
        return "";
    }
}

Related

  1. findElement(Element element, String elementName)
  2. findElement(Element element, String name)
  3. findElement(Element root, String localName, String namespace)
  4. findElement(Element topElm, String localName, String namespace)
  5. findElement(final Element e, final String find)
  6. findElementAsString(final Element e, final String find)
  7. findElementWithId(String id, Element root)
  8. findNodesByType(Element topElm, int type)
  9. findNodeValue(Element firstElement, String name)