package pack; import java.io.File; import java.io.FileOutputStream; import java.util.Iterator; import java.util.List; import org.jdom.Element; import org.jdom.input.SAXBuilder; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; public class Nar { /* narrateur System.out.println(ch.substring(0, ch.indexOf("/20"))); */ /* * / * contained * System.out.println(ch.substring(ch.indexOf("/20"),ch.length())); */ static org.jdom.Document document; static Element racine; public static void main(String[] args) { try { lireFichier("input.xml"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } changement(); try { enregistreFichier("out.xml"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } static void lireFichier(String fichier) throws Exception { /* * document = new BufferedReader(new InputStreamReader(new * FileInputStream("input.xml"),"UTF-8")); */ SAXBuilder sxb = new SAXBuilder(); document = sxb.build(new File(fichier)); racine = document.getRootElement(); } static void enregistreFichier(String fichier) throws Exception { XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat()); sortie.output(document, new FileOutputStream(fichier)); } public static void changement() { List listBooks = racine.getChildren("Book"); Iterator i = listBooks.iterator(); while (i.hasNext()) { Element courantB = (Element) i.next(); List listChapters = courantB.getChildren("Chapter"); Iterator j = listChapters.iterator(); while (j.hasNext()) { Element courantC = (Element) j.next(); String cont = courantC.getChild("contained").getText(); //courantC.removeChild("contained"); Element narrateur = new Element("narrateur"); Element contained = new Element("contained"); narrateur.addContent(cont.substring(0, cont.indexOf("/20"))); contained.addContent(cont.substring(cont.indexOf("/20"), cont.length())); courantC.addContent(narrateur); courantC.addContent(contained); } } } }