Java xerces: Parcourir un document avec DOM

Author:

Java xerces: Parcourir un document avec DOM
{filelink=8701}

/***** Code de MesExemples.com *******/
 import org.apache.xerces.parsers.DOMParser;import org.xml.sax.SAXException;import java.io.IOException;import org.w3c.dom.Document;import org.w3c.dom.NodeList;import org.w3c.dom.Node;public class MainClass {  public static void main(String args[]) throws IOException, SAXException {    DOMParser parser = new DOMParser();    parser.parse("games.xml");    Document dom = parser.getDocument();    walkNode(dom.getDocumentElement());  }  private static void walkNode(Node theNode) {    NodeList children = theNode.getChildNodes();    printNode(theNode);    for (int i = 0; i < children.getLength(); i++) {      Node aNode = children.item(i);      if (aNode.hasChildNodes())        walkNode(aNode);      else        printNode(aNode);    }  }  private static void printNode(Node aNode) {    System.out.println(aNode.getNodeName() + "," + aNode.getNodeValue());  }} 

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 *