Java XML First Child Element getFirstChildElement(Node node, String tag)

Here you can find the source of getFirstChildElement(Node node, String tag)

Description

Return the first child element under node that has given given tag name.

License

Open Source License

Declaration

public static Element getFirstChildElement(Node node, String tag) 

Method Source Code

//package com.java2s;
/*/*from w w w  .  j  a v  a  2s  . c  o  m*/
 *   Copyright 2017 The Portico Project
 *
 *   This file is part of portico.
 *
 *   portico is free software; you can redistribute it and/or modify
 *   it under the terms of the Common Developer and Distribution License (CDDL) 
 *   as published by Sun Microsystems. For more information see the LICENSE file.
 *   
 *   Use of this software is strictly AT YOUR OWN RISK!!!
 *   If something bad happens you do not have permission to come crying to me.
 *   (that goes for your lawyer as well)
 *
 */

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

public class Main {
    /**
     * Return the first child element under node that has given given tag name.
     * If there is none that matches, return null.
     */
    public static Element getFirstChildElement(Node node, String tag) {
        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            Node temp = list.item(i);
            if (temp.getNodeType() != Node.ELEMENT_NODE)
                continue;

            Element tempElement = (Element) temp;
            if (tempElement.getTagName().equals(tag))
                return tempElement;
        }

        return null;
    }
}

Related

  1. getFirstChildElement(Node node)
  2. getFirstChildElement(Node node)
  3. getFirstChildElement(Node node)
  4. getFirstChildElement(Node node)
  5. getFirstChildElement(Node node)
  6. getFirstChildElement(Node parent)
  7. getFirstChildElement(Node parent)
  8. getFirstChildElement(Node parent)
  9. getFirstChildElement(Node parent)