DOM et XML : Problème récupérer un Attribut
Bonjour, je suis en train de sécher. Je voudrais récupérer dans mon fichier XML ci-joint, les parties "nw:weightMatrix" et "nw:thresholdVector" dans une boucle for, le problème c'est que j'utilisais le compteur pour afficher et celà marche. Mais il faut que je récupère les données en utilisant les attributs. C'est la que vient le problème, je n'y arrive pas. Pouvez-vous m'aidez ? :D
Voici mon code XML :
Code:
1 2 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
| <?xml version="1.0" encoding="UTF-8"?>
<nw:network
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:nw='https://www.timc.imag.fr/TBAN'
xsi:schemaLocation='https://www.timc.imag.fr/TBAN file:/home/dervieco/Documents/StageL3/NetBeans/TestStructXML/src/xml/Network.xsd'>
<nw:structure>
<nw:nbState>2</nw:nbState>
<nw:nodeNumber>3</nw:nodeNumber>
<nw:weightMatrix>
<nw:row rowNo="0">
<nw:cell cellNo="0">-1</nw:cell>
<nw:cell cellNo="1">2</nw:cell>
<nw:cell cellNo="2">-1</nw:cell>
</nw:row>
<nw:row rowNo="1">
<nw:cell cellNo="0">-1</nw:cell>
<nw:cell cellNo="1">1</nw:cell>
<nw:cell cellNo="2">1</nw:cell>
</nw:row>
<nw:row rowNo="2">
<nw:cell cellNo="0">2</nw:cell>
<nw:cell cellNo="1">1</nw:cell>
<nw:cell cellNo="2">-1</nw:cell>
</nw:row>
</nw:weightMatrix>
<nw:thresholdVector>
<nw:threshold nodeNo="0">0</nw:threshold>
<nw:threshold nodeNo="1">0</nw:threshold>
<nw:threshold nodeNo="2">0</nw:threshold>
</nw:thresholdVector>
</nw:structure>
<nw:properties>
<nw:complexity>2</nw:complexity>
<nw:complexityLevin>8</nw:complexityLevin>
</nw:properties>
</nw:network> |
Et voici mon fichier DOM JAVA :
Code:
1 2 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 90 91
| import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class ReadXMLFile_2 {
public static void main(String[] args) {
/**
* Récupération d'une instance de la classe "DocumentBuilderFactory"
*/
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
/**
* Création du Parseur
*/
try{
DocumentBuilder builder = factory.newDocumentBuilder();
/**
* Création d'un Document
*/
Document document = builder.parse(new File("src/network1.xml"));
// Afficher les Informations du Prologue
System.out.println("*************PROLOGUE************");
System.out.println("Version : " + document.getXmlVersion());
System.out.println("Encoding" + document.getXmlEncoding());
System.out.println("Standalone : " + document.getXmlStandalone());
/**
* Affichage Root
*/
Element root = document.getDocumentElement();
System.out.println("\n" + "*************RACINE************");
System.out.println(root.getNodeName());
/**
* Affichage Données NW:STRUCTURE
*/
System.out.println("\n" + "*************NW:STRUCTURE************");
// Afficher nw:nbState
NodeList nbState = document.getElementsByTagName("nw:nbState");
Node nbStateChild = nbState.item(0).getFirstChild();
System.out.println("Nb State : " + nbStateChild.getNodeValue());
// Afficher nw:nodeNumber
NodeList nodeNumber = document.getElementsByTagName("nw:nodeNumber");
Node nodeNumberChild = nodeNumber.item(0).getFirstChild();
System.out.println("Node Number : " + nodeNumberChild.getNodeValue());
// Afficher nw:weightMatrix
NodeList weightMatrix = document.getElementsByTagName("nw:row");
Node weightMatrixChild = weightMatrix.item(0).getFirstChild();
System.out.println("Weight Matrix : ");
/**
* Affichage Données NW:PROPERTIES
*/
System.out.println("\n" + "*************NW:PROPERTIES************");
// Affichage nw:complexity
NodeList complexity = document.getElementsByTagName("nw:complexity");
Node complexityChild = complexity.item(0).getFirstChild();
System.out.println("Complexity : " + complexityChild.getNodeValue());
// Affichage nw:complexityLevin
NodeList complexityLevin = document.getElementsByTagName("nw:complexityLevin");
Node complexityLevinChild = complexity.item(0).getFirstChild();
System.out.println("Complexity Levin : " + complexityLevinChild.getNodeValue());
}
catch(final ParserConfigurationException e){
e.printStackTrace();
}
catch(final SAXException e){
e.printStackTrace();
}
catch(final IOException e){
e.printStackTrace();
}
}
} |