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();
}
}
} |
Partager