Finds and returns the first child node with the given name and attribute name, value pair. : DOM Element « XML « Java Tutorial






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

/*
 * 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.
 */

public class Main {
  /**
   * Finds and returns the first child node with the given name and
   * attribute name, value pair.
   */
  public static Element getFirstChildElement(Node   parent,
          String elemName,
          String attrName,
          String attrValue) {
      
      // search for node
      Node child = parent.getFirstChild();
      while (child != null) {
          if (child.getNodeType() == Node.ELEMENT_NODE) {
              Element element = (Element)child;
              if (element.getNodeName().equals(elemName) &&
                      element.getAttribute(attrName).equals(attrValue)) {
                  return element;
              }
          }
          child = child.getNextSibling();
      }
      
      // not found
      return null;
      
  } // getFirstChildElement(Node,String,String,String):Element
}








33.6.DOM Element
33.6.1.Create a new element
33.6.2.Create Empty DOM Document
33.6.3.Add Text object to an Element.
33.6.4.Add a new element to the given parent
33.6.5.Add an entity to a specified Element.
33.6.6.Build a QName from the element name
33.6.7.Get the name of this element
33.6.8.Get the next sibling with the same name and type
33.6.9.Copy an XML document
33.6.10.Create New Element And Set Attribute
33.6.11.Find All Elements By Tag Name Name Space
33.6.12.Find Element And Set Or Create And Set
33.6.13.Get Element Boolean Value
33.6.14.Get Element Date Value
33.6.15.Get Element Float Value
33.6.16.Get Element Int Value
33.6.17.Get Element Long Value
33.6.18.Get Element QName
33.6.19.Get Element String Value
33.6.20.Get Element Text
33.6.21.Get Elements by parent element
33.6.22.Get First Element
33.6.23.Get Next Element
33.6.24.Get the first element child.
33.6.25.Return the first element child with the specified qualified name.
33.6.26.Find Element Or Container
33.6.27.Returns the text of the element
33.6.28.Return child elements with specified name.
33.6.29.Return the first named Element found. Null if none.
33.6.30.Returns an iterator over the children of the given element with the given tag name.
33.6.31.Moves the content of the given element to the given element
33.6.32.Return the next sibling with a given name and type
33.6.33.Returns the concatenated child text of the specified node.
33.6.34.Finds and returns the first child element node.
33.6.35.Finds and returns the first child node with the given name and attribute name, value pair.
33.6.36.Finds and returns the last child element node.
33.6.37.Finds and returns the last child node with the given name and attribute name, value pair.
33.6.38.Finds and returns the next sibling element node.
33.6.39.Finds and returns the next sibling node with the given name and attribute name, value pair.
33.6.40.Document To String