Java XML Element Get findElement(Document doc, String tagName, Properties props)

Here you can find the source of findElement(Document doc, String tagName, Properties props)

Description

Find an element

License

BSD License

Parameter

Parameter Description
doc XML document
tagName Tag name
props Properties corresponding to attributes in tag

Return

Element or null if not found

Declaration

public static Element findElement(Document doc, String tagName, Properties props) 

Method Source Code

//package com.java2s;
/*L/*from  w  w w. j  a v  a2 s.  co m*/
 *  Copyright RTI International
 *
 *  Distributed under the OSI-approved BSD 3-Clause License.
 *  See http://ncip.github.com/webgenome/LICENSE.txt for details.
 */

import java.util.Enumeration;
import java.util.Properties;

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

public class Main {
    /**
     * Find element in XML document with given property
     * @param doc XML document
     * @param tagName Tag name
     * @param propName Property name
     * @param propValue Property value
     * @return Element or null if not found
     */
    public static Element findElement(Document doc, String tagName, String propName, String propValue) {
        Properties props = new Properties();
        props.put(propName, propValue);
        return findElement(doc, tagName, props);
    }

    /**
     * Find an element
     * @param doc XML document
     * @param tagName Tag name
     * @param props Properties corresponding to attributes in tag
     * @return Element or null if not found
     */
    public static Element findElement(Document doc, String tagName, Properties props) {
        Element elmt = null;
        NodeList nlist = doc.getElementsByTagName(tagName);
        for (int i = 0; i < nlist.getLength() && elmt == null; i++) {
            Node node = nlist.item(i);
            if (node instanceof Element) {
                Element temp = (Element) node;
                boolean matches = true;
                for (Enumeration en = props.keys(); en.hasMoreElements() && matches;) {
                    String name = (String) en.nextElement();
                    String value = props.getProperty(name);
                    String attValue = temp.getAttribute(name);
                    if (attValue == null)
                        matches = false;
                    else if (!value.equals(attValue))
                        matches = false;
                }
                if (matches)
                    elmt = temp;
            }
        }
        return elmt;
    }
}

Related

  1. findElement(Document doc, String elementNS, String elementName, String attrName, String attrValue)
  2. findElement(String name, Document doc)
  3. findElementAndSetElseCreateAndSet(Document document, Element parent, String child, boolean value)
  4. findElementElseCreateAndSet(Document document, Element parent, String child, boolean value)
  5. findElementList(String name, String attrName, String attrValue, Document doc)