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
|
package notams;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Notams {
private static final String ICAO = "LFFF LFBB LFMM LFRR LFEE LIRR LIBB LIMM LECB LECM LPPC LMMM LGGG LCCC LLLL OLBB ";
private static final String REPORTTYPE = "Raw";
private static final String ACTIONTYPE = "notamRetrievalByICAOs";
private static final String BUTTON = "Check All NOTAMs";
private static final String SUBMIT = "View NOTAMs";
private static final String POST_URL = "https://www.notams.faa.gov/dinsQueryWeb/queryRetrievalMapAction.do";
public static void main(String[] args) {
Map<String, Object> parameters = new LinkedHashMap<>();
parameters.put("retrieveLocId", ICAO);
parameters.put("reportType", REPORTTYPE);
parameters.put("actionType", ACTIONTYPE);
parameters.put("button", BUTTON);
parameters.put("submit", SUBMIT);
File file = new File(System.getProperty("user.home"), "Desktop/MonFichier.xml");
try {
getData(parameters, file);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void getData(Map<String, Object> params, File outputFile) throws IOException {
Connection jsoup = Jsoup.connect(POST_URL).timeout(10 * 1000).method(Method.POST);
params.entrySet().forEach(param -> jsoup.data(param.getKey(), String.valueOf(param.getValue())));
Response response = jsoup.execute();
// parse the document from response
Document document = response.parse();
XMLOutputFactory xmlfactory = XMLOutputFactory.newInstance();
try (OutputStream output = Files.newOutputStream(outputFile.toPath())) {
XMLStreamWriter xmlwriter = xmlfactory.createXMLStreamWriter(output);
xmlwriter.writeStartDocument();
try {
xmlwriter.writeStartElement("locations");
try {
// on parcourt les tables de données
List<Element> tables = document.select("form table table").stream().skip(1) // on ignore les headers
.collect(Collectors.toList());
// les tables sont par paires, une pour le nom, une pour les data
for (int i = 0; i < tables.size(); i += 2) {
Element locationTable = tables.get(i);
// pour obtenir le nom (titre)
Element name = locationTable.selectFirst("tr:nth-child(2) > td:nth-child(1) > a");
if ( name==null ) continue; // on ignore les tables sans nom
xmlwriter.writeStartElement("locations");
try {
// recherche du nom
xmlwriter.writeAttribute("name", name.text()); // on récupère le texte
exportData(xmlwriter,tables.get(i+1));
} finally {
xmlwriter.writeEndElement(); // fermer location
}
}
} finally {
xmlwriter.writeEndElement(); // fermer locations
}
} finally {
xmlwriter.writeEndDocument();
xmlwriter.close();
}
} catch (XMLStreamException e) {
throw new IOException(e);
}
}
private static void exportData(XMLStreamWriter xmlwriter, Element element) throws XMLStreamException {
xmlwriter.writeStartElement("data");
try {
int index=1;
for(Element PRE : element.select("PRE")) {
xmlwriter.writeStartElement("PRE");
try {
xmlwriter.writeCharacters(PRE.text());
}
finally {
xmlwriter.writeEndElement();
}
}
}
finally {
xmlwriter.writeEndElement();
}
}
} |
Partager