import javax.xml.transform.dom.*; import javax.xml.transform.*; import org.w3c.dom.*; import org.xml.sax.*; import java.util.regex.*; import java.io.*; import javax.xml.parsers.*; import javax.xml.transform.stream.*; public class Rapport { public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException { Rap_analyser rap=new Rap_analyser(); } } class Rap_analyser{ public Rap_analyser() throws ParserConfigurationException, SAXException, IOException{ File f = new File("C:\\Documents and Settings\\Propriétaire\\Bureau\\rapport définitif.fo"); try { fin = new FileInputStream(f); } catch (FileNotFoundException e) { e.printStackTrace(); } factory = DocumentBuilderFactory.newInstance(); builder = factory.newDocumentBuilder(); doc = builder.parse(fin); Element root = doc.getDocumentElement(); try { analyse(root); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } System.out.print("rapport xml créé !!"); try { write(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } } public void analyse(Node el) throws FileNotFoundException, TransformerException{ NodeList children = el.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if(child instanceof Element){analyse(child);} else if((child instanceof Text )&& (((Text) child).getData().trim().length() != 0)){ String txt = ((Text) child).getData(); pattern=Pattern.compile("Quest\\s*\\W\\s*(\\d+)\\s*:\\s*(\\d+)\\s*\\W"); matcher=pattern.matcher(txt); if(matcher.matches()){ el.removeChild(child); System.out.println("trouvé"); for(int q=0;q<=matcher.groupCount();q++){ System.out.println(q+"==>"+matcher.group(q)); } } System.out.println(txt); } } } public void write() throws TransformerFactoryConfigurationError, FileNotFoundException, TransformerException{ Transformer t = TransformerFactory.newInstance().newTransformer(); t.transform(new DOMSource(doc), new StreamResult(new FileOutputStream(new File("C:\\Documents and Settings\\Propriétaire\\Bureau\\new rapport définitif.fo")))); } DocumentBuilderFactory factory; DocumentBuilder builder; FileInputStream fin; Document doc; Pattern pattern; Matcher matcher; }