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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| package aip;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.events.XMLEvent;
public class AIP {
public static void main(String[] args) throws IOException, XMLStreamException {
String fichier = "/Users/moi/Mes Documents/sources test/x.xml";
QName abd = QName.valueOf("Abd");
QName codeType = QName.valueOf("codeType");
QName codeId = QName.valueOf("codeId");
QName geoLat = QName.valueOf("geoLat");
QName geoLong = QName.valueOf("geoLong");
QName geoLatCen = QName.valueOf("geoLatCen");
QName geoLongCen = QName.valueOf("geoLongCen");
QName valRadius = QName.valueOf("valRadius");
QName uomRadius = QName.valueOf("uomRadius");
StringBuilder sb = new StringBuilder();
/*StringBuilder abdsb = new StringBuilder();*/
StringBuilder avxsb = new StringBuilder();
StringBuilder circlesb = new StringBuilder();
int zone=0;
/*int coord=0;*/
XMLInputFactory f = XMLInputFactory.newInstance();
try {
try(Reader reader = Files.newBufferedReader(Paths.get(fichier))) {
XMLStreamReader xmlreader = f.createXMLStreamReader( reader);
while(xmlreader.hasNext()) {
switch(xmlreader.next()) {
case XMLEvent.START_ELEMENT:
if ( abd.equals(xmlreader.getName()) ) {
avxsb.append("--------------------------\n"); // rajoute des pointillés pour delimiter chaque zones
avxsb.append("Zone ").append(zone++)/*.append("\n")*/;
boolean grabtext=false;
readavx: while(xmlreader.hasNext()) {
switch(xmlreader.next()) {
case XMLEvent.START_ELEMENT:
if ( codeType.equals(xmlreader.getName()) ) {
avxsb.append("\n").append("\n").append(" codeType: ");
grabtext = true;
}
else if ( codeId.equals(xmlreader.getName()) ) {
avxsb.append("\n").append("\n").append(" nom: ");
grabtext = true;
}
else if ( geoLat.equals(xmlreader.getName()) ) {
avxsb.append("\n").append(" Coord: ");
grabtext = true;
}
else if ( geoLong.equals(xmlreader.getName()) ) {
circlesb.append(" long: ");
grabtext = true;
}
else if ( geoLatCen.equals(xmlreader.getName()) ) {
avxsb.append("\n").append(" Circle coordinate center : ");
grabtext = true;
}
else if ( geoLongCen.equals(xmlreader.getName()) ) {
circlesb.append(" LongCen: ");
grabtext = true;
}
else if ( valRadius.equals(xmlreader.getName()) ) {
avxsb.append(" Radius : ");
grabtext = true;
}
else if ( uomRadius.equals(xmlreader.getName()) ) {
circlesb.append("\n").append(" unité : ");
grabtext = true;
}
else {
grabtext = false;
avxsb.append("\n");
}
break;
case XMLEvent.END_ELEMENT:
grabtext=false;
if ( abd.equals(xmlreader.getName())) {
if ( sb.length()>0 ) sb.append("\n");
sb.append("\n").append(avxsb);
avxsb.setLength(0);
break readavx;
}
break;
case XMLEvent.CHARACTERS:
if ( grabtext ) {
avxsb.append(xmlreader.getText());
/*circlesb.append(xmlreader.getText());*/
}
break;
}
}
}
}
}
}
System.out.println(sb);
// pour écrire dans un fichier :
//Files.writeString(Paths.get("/Users/moi/Desktop/xxxxxxx.txt"), sb.toString());
/*try(PrintWriter printWriter = new PrintWriter(Files.newBufferedWriter(Paths.get("/Users/moi/Desktop/xxxxxxx.txt")))) {
printWriter.println(sb);*/
} catch (XMLStreamException | IOException e) {
e.printStackTrace();
}
}
} |
Partager