Java XML First Child Element getFirstChildElmtByTag(String aTagName)

Here you can find the source of getFirstChildElmtByTag(String aTagName)

Description

get First Child Elmt By Tag

License

Open Source License

Parameter

Parameter Description
aTagName a parameter

Declaration

public Element getFirstChildElmtByTag(String aTagName) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 www.isandlatech.com (www.isandlatech.com)
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from   w  ww.j  av a 2 s.c  o  m*/
 *    ogattaz (isandlaTech) - initial API and implementation
 *******************************************************************************/

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

import org.w3c.dom.Node;

public class Main {
    private Document pDom = null;

    /**
     * @param aNode
     * @param aTagName
     * @return
     */
    public static Element getFirstChildElmtByTag(Node aNode, String aTagName) {

        if (aNode == null) {
            return null;
        }
        Node wNode = aNode.getFirstChild();
        if (wNode == null
                || (wNode.getNodeType() == Node.ELEMENT_NODE && wNode
                        .getNodeName().equals(aTagName))) {
            return (Element) wNode;
        } else {
            return getFirstSiblingElmtByTag(wNode, aTagName);
        }
    }

    /**
     * @param aTagName
     * @return
     */
    public Element getFirstChildElmtByTag(String aTagName) {

        return getFirstChildElmtByTag(this.getRootElmt(), aTagName);
    }

    /**
     * @param aNode
     * @param aTagName
     * @return
     */
    public static Element getFirstSiblingElmtByTag(Node aNode,
            String aTagName) {

        if (aNode == null) {
            return null;
        }
        Node wNode = aNode.getNextSibling();
        while (wNode != null
                && (wNode.getNodeType() != Node.ELEMENT_NODE || !wNode
                        .getNodeName().equals(aTagName))) {
            wNode = wNode.getNextSibling();
        }
        return (Element) wNode;
    }

    /**
     * @return
     */
    public Element getRootElmt() {

        return getDom().getDocumentElement();
    }

    /**
     * @return
     */
    public Document getDom() {

        return pDom;
    }
}

Related

  1. getFirstChildElementNS(Node parent, String[][] elemNames)
  2. getFirstChildElementOfName(@Nonnull final Node aStartNode, @Nullable final String sName)
  3. getFirstChildElementsByTagName( Node contextNode, String elementName)
  4. getFirstChildElementsByTagName(Node contextNode, String elementName)
  5. getFirstChildElementWithName(Element elem, String name)
  6. getFirstChildElmtByTagAndAttribut(Node aNode, String aTagName, String aAttrId, String aAttrValue)
  7. getFirstChildNamed(Element elem, String childName)
  8. getFirstChildNamed(Node node, String name)
  9. getFirstChildNode(Node parentNode, String childNodeName)