| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 
 |  
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory;  
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;  
import java.io.File;
import java.io.IOException;
import org.w3c.dom.Document;
import org.w3c.dom.Element ; 
import org.w3c.dom.Node ;
import org.w3c.dom.NodeList ; 
 
public class TestXML{
 
    static Document document; 
 
	public static void printNode(Node node){
		Element e ;
		if(node.getNodeType() == Node.ELEMENT_NODE)
		{
			System.out.println("Element :"+node.getNodeName());
		}
		else if(node.getNodeType() == Node.TEXT_NODE)
		{
			if(!(node.getNodeValue().equals("")))
			{
				System.out.println("Texte :"+node.getNodeValue());
			}
		}
		NodeList nodes = node.getChildNodes();
		for(int i=0; i<nodes.getLength(); i++){
			Node n = nodes.item(i);
			printNode(n);
		}
	}
 
	public static void searchNode(Element node,String nodeValueSearched)
	{
		NodeList nodeList = node.getElementsByTagName("conditionsSet") ;
		System.out.println("Il y a "+nodeList.getLength()+" conditionsSet ds le fichier") ;
		for(int indexNode = 0 ; indexNode < nodeList.getLength() ; indexNode ++)
		{
			Element element = (Element) nodeList.item(indexNode) ;
			NodeList childList = element.getChildNodes() ;
			for(int i = 0 ; i < childList.getLength() ; i ++)
			{
				Node e = (Node) childList.item(i) ;
				e.normalize() ;
				if(e.getNodeType() == Node.ELEMENT_NODE)
				{			
				}
				else if(e.getNodeType() == Node.TEXT_NODE)
				{
					System.out.println("child "+i+" "+e.getNodeValue()) ;
					if(e.getNodeValue().equals(nodeValueSearched))
					{
						System.out.println("Trouvé") ;
					}
				}
			}
		}
	}
 
    public static void main(String argv[])
    {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setIgnoringElementContentWhitespace(true) ;
        try {
           DocumentBuilder builder = factory.newDocumentBuilder();
           document = builder.parse( new File("/C:/Documents%20and%20Settings/le/Mes%20documents/eclipse-SDK-3.0.2-win32/eclipse/workspace/Test_performance/configuration.xml") );
           document.normalize() ;
           Element root = document.getDocumentElement() ;
           root.normalize() ;
           //printNode(root) ;
           searchNode(root,"MFDP/general/FreeTexts") ;
        } catch (SAXException sxe) {
           Exception  x = sxe;
           if (sxe.getException() != null)
               x = sxe.getException();
           x.printStackTrace();
        } catch (ParserConfigurationException pce) {
            // Parser with specified options can't be built
            pce.printStackTrace();
        } catch (IOException ioe) {
           // I/O error
           ioe.printStackTrace();
        }
    } // main
} | 
Partager