Bonjour,

Quand je crée un fichier XML celui-ci est souvent tronqué :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.datacet.android.util;
 
 
import java.io.File;
 
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
 
import java.io.OutputStreamWriter;
 
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
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.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
 
 
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
 
public class TraceFileXML {
	private static final String TAG = "android.util.TraceFileXML";
 
	public static final int SERVER = 0;
	public static final int SMS = 1;
 
 
	private static Document doc;
	private static Element rootElement;
	private static Element event;
 
	public static void writeNodeEvent(int fileType) {
		loadingDocument(fileType);
 
		if(doc==null)
			initDocument();
		if(doc!=null) {
			event = doc.createElement("event");
			rootElement.appendChild(event);
		}
	}
 
	public static void write(String element, String text, int fileType) {
		Element myElement = null;
		if(doc!=null) {
			myElement = doc.createElement(element);
 
			if(text.contains(">")) {
				CDATASection cdata = doc.createCDATASection(text);
				myElement.appendChild(cdata);
			} else {
				myElement.appendChild(doc.createTextNode(text));			
			}	
		}
		event.appendChild(myElement);
	}
 
	private static void loadingDocument(int fileType) {
		File file = loadingFile(fileType);
		if(file!=null) {
			DocumentBuilderFactory factory;
			DocumentBuilder builder;
 
 
 
				try {
					factory = DocumentBuilderFactory.newInstance();
					builder = factory.newDocumentBuilder();
					doc = builder.parse(file);
					rootElement = doc.getDocumentElement();
				} catch (FileNotFoundException e) {
					Log.d(TAG, e.getMessage());
				} catch (ParserConfigurationException e) {
					Log.d(TAG, e.getMessage());
				} catch (SAXException e) {
					Log.d(TAG, e.getMessage());
				} catch (IOException e) {
					Log.d(TAG, e.getMessage());
				}
 
		}
 
	}
 
	private static File loadingFile(int fileType) {
		File file;
		switch(fileType) {
		case SERVER:
			file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "server.xml");
			break;
		case SMS:
			file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "sms.xml");
			break;
		default:
			file = null;	
		}
		return file;
	}
 
	private static void initDocument() {
		try {
			DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
 
			doc = docBuilder.newDocument();
			rootElement = doc.createElement("events");
			doc.appendChild(rootElement);
		} catch (ParserConfigurationException e) {
			doc = null;
			Log.d(TAG, e.getMessage());
		}
	}
 
	public static void close(int fileName) {
		File file = loadingFile(fileName);
 
		FileOutputStream outputStream = null;
		if(file!=null) {
			try {
				TransformerFactory transformerFactory = TransformerFactory.newInstance();
				Transformer transformer = transformerFactory.newTransformer();
				DOMSource source = new DOMSource(doc);
				outputStream = new FileOutputStream(file);
				StreamResult result = new StreamResult(new OutputStreamWriter(outputStream));
				transformer.transform(source, result);
			} catch (TransformerConfigurationException e) {
				Log.d(TAG, e.getMessage());
			} catch (TransformerException e) {
				Log.d(TAG, e.getMessage());
			} catch (FileNotFoundException e) {
				Log.d(TAG, e.getMessage());
			} finally {
				if(outputStream!=null) {
					try {
						outputStream.flush();
					} catch (IOException e) {
						Log.d(TAG, e.getMessage());
					}
					try {
						outputStream.close();
					} catch (IOException e) {
						Log.d(TAG, e.getMessage());
					}
				}
			}
			// Rafraichissement du fichier crée pour affichage quand le smartphone est monté en USB sur un hôte.
			Context context = MyApplication.getAppContext();
			Uri uri =  Uri.parse("file://" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());
			context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, uri));
		}
	}
}