import java.io.FileOutputStream; import org.jdom.*; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; public class Demo { static Element racine = new Element("Abou-Daoud"); static org.jdom.Document document = new Document(racine); public static BufferedReader in; public void doit() { String str; String chTitle; String cont; String nbr; int test = 0; String ch = ""; try { in = new BufferedReader(new InputStreamReader(new FileInputStream( "data.txt"), "UTF-8")); if (in.ready()) { str = in.readLine(); while (str != null) { // ******** Treatment of "Book" ******** while ((str != null) && (str.indexOf("/1 ") != -1)) { Element book = new Element("Book"); chTitle = str.substring(str.indexOf("/1"), str.lastIndexOf("/1") + 2); Attribute titleB = new Attribute("Title", chTitle); // System.out.println(chTitle); book.setAttribute(titleB); racine.addContent(book); str = in.readLine(); // ******** Treatment of "Chapter" ******** while ((str != null) && (str.indexOf("/30") != -1)) { //slashTrente = true; Element chapitre = new Element("Chapiter"); chTitle = str.substring(str.indexOf("/30"), str.lastIndexOf("/30") + 3); // System.out.println(chTitle); Attribute titleC = new Attribute("Title", chTitle); chapitre.setAttribute(titleC); book.addContent(chapitre); str = in.readLine(); // ****** Treatement de "Sous chapitre " ****** while ((str != null) && (str.indexOf("/7 ") != -1)) { Element souschapitre = new Element( "Sous-chapitre"); chTitle = str.substring(str.indexOf("/7"), str.lastIndexOf("/7") + 2); Attribute titleD = new Attribute("Title", chTitle); System.out.println(chTitle); souschapitre.setAttribute(titleD); chapitre.addContent(souschapitre); str = in.readLine(); // ******** Treatment of "number hadith" // ******** while ((str != null) && (str.indexOf("$") != -1)) { Element number = new Element( "number-hadith"); nbr = str.substring(str.indexOf("$"), str.indexOf(" ")); number.addContent(nbr); chapitre.addContent(number); // System.out.println(nbr); while ((str != null) && (str.indexOf("$") == -1) && (str.indexOf("/30 ") == -1) && (str.indexOf("/1 ") == -1) && (str.indexOf("/7 ") == -1)) { test = 1; cont = str.substring(0, str.length()); ch = ch + cont; str = in.readLine(); // System.out.println(cont); } if (test == 1) { test = 0; Element contained = new Element( "contained"); contained.addContent(ch); chapitre.addContent(contained); ch = ""; } } } } } // ******** Treatment of "Contained" ******** } } in.close(); //enregistre("out.xml"); // affiche(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { new Demo().doit(); } static void affiche() { try { XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat()); sortie.output(document, System.out); } catch (java.io.IOException e) { } } static void enregistre(String fichier) { try { XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat()); sortie.output(document, new FileOutputStream(fichier)); } catch (java.io.IOException e) { } } }