Java XML Element Get getElementByID(Element el, String id)

Here you can find the source of getElementByID(Element el, String id)

Description

DOCUMENTME

License

Apache License

Parameter

Parameter Description
el DOCUMENTME
id DOCUMENTME

Return

DOCUMENTME

Declaration

public static Element getElementByID(Element el, String id) 

Method Source Code

//package com.java2s;
/*//from   www  .  ja v a  2  s.c  o  m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import org.w3c.dom.Attr;

import org.w3c.dom.Element;

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

public class Main {
    /**
     * DOCUMENTME
     *
     * @param el DOCUMENTME
     * @param id DOCUMENTME
     *
     * @return DOCUMENTME
     */
    public static Element getElementByID(Element el, String id) {
        if (el == null) {
            return null;
        }
        String thisId = el.getAttribute("id");
        if (id.equals(thisId)) {
            return el;
        }
        NodeList list = el.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            Node node = list.item(i);
            if (node instanceof Element) {
                Element ret = getElementByID((Element) node, id);
                if (ret != null) {
                    return ret;
                }
            }
        }
        return null;
    }

    /**
     * Returns the value of an attribute of an element. Returns null if the
     * attribute is not found (whereas Element.getAttribute returns "" if an
     * attrib is not found).
     *
     * @param el Element whose attrib is looked for
     * @param attrName name of attribute to look for
     *
     * @return the attribute value
     */
    static public String getAttribute(Element el, String attrName) {
        String sRet = null;
        Attr attr = el.getAttributeNode(attrName);
        if (attr != null) {
            sRet = attr.getValue();
        }
        return sRet;
    }
}

Related

  1. getElementAsInt(Element element)
  2. getElementAttributeValue(Document doc, String tagName, String attributeName)
  3. getElementBooleanValue(Document document, Element parent, String element)
  4. getElementByClass(Element ele, String name)
  5. getElementByFilter(Document doc, NodeFilter filter)
  6. getElementByID(Element el, String id)
  7. getElementById(Element element, String id)
  8. getElementByName(Element ele, String name)
  9. getElementByQualifiedName(Element n, String namespace, String elementName)