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 131 132
| package projet;
import java.io.*;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.StringReader;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.*;
import org.jdom.output.XMLOutputter;
import org.xml.sax.InputSource;
public class Parsing {
public static void main (String args[]){
org.jdom.Document document = null;
//Racine du document
org.jdom.Document test=null;
//On crée une instance de SAXBuilder
SAXBuilder sxb = new SAXBuilder();
try{
//On crée un nouveau document JDOM avec en argument le fichier XML
//Le parsing est terminé ;)
document = sxb.build(new InputSource (new FileReader("index.xml")));
System.out.println("Document " + document);
List childrens = document.getRootElement().getChildren();
Iterator i = childrens.iterator();
Element courant = null;
//Ma treemap d'objet
TreeMap tri = new TreeMap();
//Pour chaque element fils du document root ici <index>
String valeur = null;
while(i.hasNext()){
courant = (Element) i.next();
//Chaque <terme>
//On récupere la valeur de la balise <chaine>
valeur = getValueOf("chaine", courant);
System.out.println(valeur);
tri.put(valeur, courant);
}
System.out.println(tri);
//Le tri est terminé.
//On effectue le traitement souhaité
Iterator it = tri.keySet().iterator();
org.jdom.Element racine = new Element("index");
test.setRootElement(racine);
while(it.hasNext()){
Element temp = (Element)tri.get(it.next());
racine.addContent(temp);
}
}catch(Exception e){
e.printStackTrace();
}
enregistrer(test, "indextrie.xml");
}
public static String getValueOf(String balise, Element root){
String retour = "";
Element elt = goToElement(root, balise);
retour = getContenu(elt);
return retour;
}
public static Element goToElement(Element root, String child){
Element retour = null;
if(getNom(root).equals(child)){
retour = root;
}else{
List childrens = root.getChildren();
Iterator i = childrens.iterator();
Element courant = null;
while(i.hasNext()){
courant = (Element) i.next();
if(retour == null){
if(getNom(courant).equals(child)){
retour = courant;
}else{
retour = goToElement(courant, child);
}
}
}
}
return retour;
}
public static String getContenu(Element e){
return e.getText();
}
public static String getNom(Element e){
return e.getName();
}
public static void enregistrer(Document doc, String fichier)
{
try
{
//On utilise ici un affichage classic avec getPrettyFormat()
XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
//Remarquez qu'il suffit simplement de créer une instance de FileOutputStream
//avec en argument le nom du fichier pour effectuer la sérialisation.
sortie.output(doc, new FileOutputStream(fichier));
}
catch (java.io.IOException e){}
}
} |
Partager