Java XML First Child Element getFirstChildElmtByTagAndAttribut(Node aNode, String aTagName, String aAttrId, String aAttrValue)

Here you can find the source of getFirstChildElmtByTagAndAttribut(Node aNode, String aTagName, String aAttrId, String aAttrValue)

Description

get First Child Elmt By Tag And Attribut

License

Open Source License

Parameter

Parameter Description
aNode a parameter
aTagName a parameter
aAttrId a parameter
aAttrValue a parameter

Declaration

public static Element getFirstChildElmtByTagAndAttribut(Node aNode,
        String aTagName, String aAttrId, String aAttrValue) 

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:/* www  .  ja  va  2 s.c  o  m*/
 *    ogattaz (isandlaTech) - initial API and implementation
 *******************************************************************************/

import org.w3c.dom.Element;

import org.w3c.dom.Node;

public class Main {
    /**
     * @param aNode
     * @param aTagName
     * @param aAttrId
     * @param aAttrValue
     * @return
     */
    public static Element getFirstChildElmtByTagAndAttribut(Node aNode,
            String aTagName, String aAttrId, String aAttrValue) {

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

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

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

Related

  1. getFirstChildElementOfName(@Nonnull final Node aStartNode, @Nullable final String sName)
  2. getFirstChildElementsByTagName( Node contextNode, String elementName)
  3. getFirstChildElementsByTagName(Node contextNode, String elementName)
  4. getFirstChildElementWithName(Element elem, String name)
  5. getFirstChildElmtByTag(String aTagName)
  6. getFirstChildNamed(Element elem, String childName)
  7. getFirstChildNamed(Node node, String name)
  8. getFirstChildNode(Node parentNode, String childNodeName)
  9. getFirstChildNodeWithName(String nodeName, Node parentNode)