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("Sahih-Muslim"); static org.jdom.Document document = new Document(racine); public static BufferedReader in; public void doit() { String str; String chTitle; String cont; String nbr; 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)) { Element chapitre = new Element("Chapter"); 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(); while ((str != null) && (str.indexOf("$") == -1)) { Element description = new Element("description"); chTitle = str; // System.out.println(chTitle); description.addContent(chTitle); chapitre.addContent(description); 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(" ")); str = str.substring(str.indexOf(" "), str.length()); Attribute titleN = new Attribute("Title", nbr); number.setAttribute(titleN); chapitre.addContent(number); // System.out.println(nbr); Element contained = new Element("contained"); while ((str != null) && (str.indexOf("$") == -1) && (str.indexOf("/30 ") == -1) && (str.indexOf("/1 ") == -1)) { cont = str.substring(0, str.length()); contained.addContent(cont); number.addContent(contained); str = in.readLine(); // System.out.println(cont); } } } } // ******** 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) { } } }