Ok
Merci
Mon problème est résolu
Merci a tous pour votre aide
A la prochaine ;)
Jasmin rose
Version imprimable
Ok
Merci
Mon problème est résolu
Merci a tous pour votre aide
A la prochaine ;)
Jasmin rose
J'ai eu besoin de faire ce que je préconisais, donc voila une classe qui parse un fichier text et en fait un fichier xml.
Code:
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 import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; public class SLParser { InputStream is; String codePage; static String NS = "http://apache.org/cocoon/slop/parser/1.0"; private Document xmlDoc; public SLParser(InputStream is, String codePage) { this.is = is; this.codePage = codePage; xmlDoc = null; } public void parse() throws IOException, ParserConfigurationException, TransformerException { if(is.markSupported()) is.reset(); InputStreamReader isr = new InputStreamReader(is, codePage); BufferedReader input = new BufferedReader(isr,100); Document doc= prepareDocument(); Element root = doc.getDocumentElement(); String line = null; int lineNumber =0; while(( line = input.readLine()) != null) { lineNumber++; Element lineElt; if(line.length()>0) lineElt = doc.createElement("sl:line"); else lineElt = doc.createElement( "sl:empty-line"); lineElt.setTextContent(line); lineElt.setAttribute( "line-number", String.valueOf(lineNumber)); root.appendChild(lineElt); } isr.close(); input.close(); } public Document getDocument() throws Exception { if(xmlDoc==null) throw new Exception("Document null: you need to parse() first"); return xmlDoc; } private Document prepareDocument() throws ParserConfigurationException { // Création d'un nouveau DOM DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance(); DocumentBuilder constructeur = fabrique.newDocumentBuilder(); xmlDoc = constructeur.newDocument(); // Propriétés du DOM xmlDoc.setXmlVersion("1.0"); xmlDoc.setXmlStandalone(true); // Création de l'arborescence du DOM Element racine = xmlDoc.createElementNS(NS, "sl:parsed-text"); xmlDoc.appendChild(racine); return xmlDoc; } public void writeToOutputStream(OutputStream out) throws Exception { if(xmlDoc==null) throw new Exception("Document null: you need to parse() first"); DOMSource domSource = new DOMSource(xmlDoc); StreamResult result = new StreamResult(out); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, codePage); transformer.transform(domSource, result); } public static void main(String []args) { try { SLParser bp = new SLParser(new FileInputStream("C:\\2006_AOUT_00014439.lst"), "ISO-8859-1"); bp.parse(); bp.writeToOutputStream(new FileOutputStream("C:\\2006_AOUT_00014439.xml")); } catch (FileNotFoundException e){ e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }
merci pour ton code que je trouve interessant
mais pour mieux comprendre mon problème regare
java xml
disscussion problème java xml