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
| package extraction_xml;
import java.io.*;
import javax.xml.xpath.*;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import javax.xml.namespace.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Extraction_avec_filtre {
static File fichier;
// Constructeur
public Extraction_avec_filtre (String url){
fichier = new File(url);
}
// Méthodes
/**
* créer une instance de XPath et lui faire évaluer une XPathExpression
* @param file
* @param expression
* @param retour
* @return
*/
public String evaluerDOM(File file, String expression, QName retour){
String texteCopie=null;
try{
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder constructeur = docBuilderFactory.newDocumentBuilder();
Document document = constructeur.parse(file);
//création du XPath
XPathFactory fabrique = XPathFactory.newInstance();
XPath xpath = fabrique.newXPath();
//évaluation de l'expression XPath
XPathExpression exp = xpath.compile(expression);
NodeList resultat = (NodeList) exp.evaluate(document,XPathConstants.NODESET); // récupère la liste de Node correspondant au xpath (donc des pre)
StringBuilder sb=new StringBuilder(); // pour faire la concaténation
for(int i=0; i<resultat.getLength(); i++) { // parcours
if ( i!=0 ) { // pour avoir des séparations entre chaque node, sauf pour le premier
sb.append("\n--------------------------\n");//ajout d'une ligne de séparation par exemple
//sb.append("\n"); // ajoute une ligne vide
}
//sb.append("Ligne ").append(i+1).append("\n");//ajout d'un titre par exemple
sb.append("\n");//ajout d'une ligne de séparation par exemple
Node pre = resultat.item(i); // on récupère le node d'index i
sb.append(pre.getTextContent()); // concaténation du texte du node
}
texteCopie=sb.toString(); //
}catch(XPathExpressionException xpee){
xpee.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return texteCopie;
}
/**
* @param args
*/
public static void main(String[] args) throws IOException {
Extraction test = new Extraction("/Users/moi/Desktop/testNOTAM/MonFichier.xml");
String resultat = test.evaluerDOM(fichier, "//PRE", XPathConstants.STRING);
/* File Notams = new File("/Users/moi/Desktop/notams_test.txt") ;
try (PrintWriter out = new PrintWriter(new FileWriter(Notams))) {
out.write(resultat) ; //écris bonjour dans le fichier
out.println(); //fais un retour à la ligne dans le fichier
out.close() ; //Ferme le flux du fichier, sauvegardant ainsi les données.*/
System.out.println(resultat);
}
}
/*}*/ |
Partager