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 92 93 94 95
| package mairies;
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 RechParInsee {
public String RechInsee (String Mairies, String insee) throws SAXException, IOException {
File Organisme = new File( "C:/Users/Céline/Documents/Cours/Fac/L2/Java/Projava" );
File[]list=Organisme.listFiles();
for (int j=0;j<list.length; j++){
String t[]=(list[j].getName().split("-"));
if ((t[0].compareToIgnoreCase("C:/Users/Céline/Documents/Cours/Fac/L2/Java/Projava/Mairies"))==0){
DocumentBuilder dBuilder = null;
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
dBuilder = dbFactory.newDocumentBuilder();
} catch (ParserConfigurationException e ) {
System.err.println("impossible");
System.exit(0);
}
Document doc=dBuilder.parse(new File (list[j].getPath()));
doc.getDocumentElement().normalize();
Element e =doc.getDocumentElement();
if ((e.getAttribute("insee")).compareTo("insee")==0){
NodeList nodes = doc.getElementsByTagName("Organisme");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
System.out.println(getValue("Nom", element) +":"+ "\n");
System.out.println("INSEE : " + element.getAttribute("codeInsee"));
System.out.println("Code Postal : " + getValue("CodePostal", element));
System.out.println("Ville :" + getValue("NomCommune", element));
System.out.println("Numéro de Téléphone : " + getValue("Téléphone", element));
System.out.println("Adresse Mail : " + getValue("Email", element));
System.out.println("Site Web : " + getValue("Url", element)+"\n");
}
}
}
}
}
return "La mairie recherchée a été trouvée";
}
private static String getValue(String Organisme, Element element) {
NodeList nodes = element.getElementsByTagName(Organisme).item(0).getChildNodes();
Node node = (Node) nodes.item(0);
return node.getNodeValue();
}
public static void main(String[]args) throws SAXException, IOException{
System.out.println(".................");
RechParInsee fbgseth =new RechParInsee();
System.out.println(".................");
fbgseth.RechInsee("C:/Users/Céline/Documents/Cours/Fac/L2/Java/Projava","95002");
System.out.println(".................");
}
} |
Partager