Java XML: Exemple de recherche d’un enfant nommé d’un nœud spécifique

Author:

Java XML: Exemple de recherche d'un enfant nommé d'un nœud spécifique
{filelink=8560}

/***** Code de MesExemples.com *******/
/**
 * @(#)DOMFindSpecificChild.java
 *
 *
 * @author 
 *sakoba(java.mesexemples.com) @version 1.00 2013/7/5
 */

import org.w3c.dom.Node;
public class DOMFindSpecificChild {

  /**
   * Search for a named child of a given node
   * @param currentNode Starting point for our search
   * @param tagName Node name to look up
   * @return matching Node (null if none)
   */
  public static Node getChildSiblingByName(Node currentNode, String tagName) {
    Node node = currentNode.getFirstChild();

    while ((node != null) && (!node.getNodeName().equals(tagName))) {
      node = node.getNextSibling();
    }
    return node;
  }

}

Code testé avec le fichier XML Suivant




	Sakoba
	Adams
	Rappel
	Ne m'oubliez pas ce week-end!

Leave a Reply

Your email address will not be published. Required fields are marked *