Bonjour ,

Je rencontre actuellement un soucis , je vous présente mon projet , je doit réaliser un formulaire qui permet d'enregistrer les données dans un fichier sous forme XML . Aprés plusieurs recherche sur internet , je parvient bien à créer mon fichier XML et à insérer des données dedans . Mon seul problème est que je ne parvient pas à ajouter d'autre données à la suite du fichier XML sans détruire les données précédentes , quelqu'un pourrais me donner un coup de main ou bien m'aiguillé ? Je vous met mon code ci dessous .

En vous remerciant , Surinox


Code Java : 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
167
168
169
170
171
172
 
package com.example.enguerran.popopi;
 
import android.Manifest;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.util.Log;
import android.util.Xml;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
 
 
import org.xmlpull.v1.XmlSerializer;
 
public class MainActivity extends AppCompatActivity {
 
    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
 
    protected LocationManager locationManager;
    protected ImageButton retrieveLocationButton;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();
 
        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        retrieveLocationButton = (ImageButton) findViewById(R.id.Imagebutton);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
 
        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER,
                MINIMUM_TIME_BETWEEN_UPDATES,
                MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
                new MyLocationListener()
        );
        showCurrentLocation();
        retrieveLocationButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCurrentLocation();
            }
        });
 
        final Button button = (Button) findViewById(R.id.button2);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
 
                File newxmlfile = new File(Environment.getExternalStorageDirectory()+"/fichier.xml");
                try{
                    newxmlfile.createNewFile();
                }catch(IOException e)
                {
                    Log.e("IOException", "Exception in create new File(");
                }
 
 
                FileOutputStream fileos = null;
                try{
                    fileos = new FileOutputStream(newxmlfile);
 
                }catch(FileNotFoundException e)
                {
                    Log.e("FileNotFoundException",e.toString());
                }
 
 
 
 
 
                XmlSerializer serializer = Xml.newSerializer();
                try{
                    serializer.setOutput(fileos, "UTF-8");
                    serializer.startDocument(null, Boolean.valueOf(true));
                    serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
                    serializer.startTag(null, "Arbre");
 
                        serializer.startTag(null, "Arbre2");
                            serializer.attribute(null, "nummartelage", "12351");
                            serializer.attribute(null, "latitude", "50.655");
                            serializer.attribute(null, "longitude", "50.655");
                            serializer.attribute(null, "EssenceArbre", "Test");
                            serializer.attribute(null, "Diametre", "155cm");
                            serializer.attribute(null, "Hauteur", "24m");
                            serializer.attribute(null, "Volume", "24m²");
                            serializer.attribute(null, "Qualité", "Bonne");
                        serializer.endTag(null, "Arbre2");
 
                    serializer.endTag(null,"Arbre");
 
 
                    serializer.endDocument();
                    serializer.flush();
                    fileos.close();
 
 
                }catch(Exception e)
                {
                    Log.e("Exception","Exception occured in wroting");
                }
 
 
 
            }
        });
 
    }
 
    protected void showCurrentLocation() {
 
        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
 
        if (location != null) {
            String message = String.format(
                    " Long: %1$s \n Lat: %2$s",
                    location.getLongitude(), location.getLatitude()
            );
 
            final EditText editText = (EditText) findViewById(R.id.editText2);
            Toast.makeText(MainActivity.this, message,
                    Toast.LENGTH_LONG).show();
            editText.setText(message);
        }
 
    }
 
    private class MyLocationListener implements LocationListener {
 
        public void onLocationChanged(Location location) {
            String message = String.format(
                    "Lon: %1$s  Lat: %2$s",
                    location.getLongitude(), location.getLatitude()
            );
            Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
        }
 
        public void onStatusChanged(String s, int i, Bundle b) {
            Toast.makeText(MainActivity.this, "Provider status changed",
                    Toast.LENGTH_LONG).show();
        }
 
        public void onProviderDisabled(String s) {
            Toast.makeText(MainActivity.this,
                    "Provider disabled by the user. GPS turned off",
                    Toast.LENGTH_LONG).show();
        }
 
        public void onProviderEnabled(String s) {
            Toast.makeText(MainActivity.this,
                    "Provider enabled by the user. GPS turned on",
                    Toast.LENGTH_LONG).show();
        }
 
    }
 
}


Code XML : 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"> <!--IMPORTANT otherwise backgrnd img. will not fill the whole screen -->
 
 
 
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >
 
 
 
 
    <ImageView
        android:layout_width="280dp"
        android:layout_height="120dp"
        android:background="@drawable/logo"
        android:layout_centerHorizontal="true"
        android:id="@+id/imageView" />
 
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Enregistrement d'un arbre"
        android:id="@+id/textView"
        android:layout_alignParentStart="true"
        android:textAlignment="center"
        android:layout_marginTop="150dp"
        android:layout_alignParentEnd="true"
        android:textColor="#000000"
        android:layout_marginBottom="0dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
 
        android:text="Numéro de martelage :"
        android:id="@+id/textView2"
        android:textAlignment="center"
        android:textColor="#000000"
 
        android:layout_toStartOf="@+id/editText"
        android:layout_alignParentEnd="false"
        android:layout_alignParentStart="true"
        android:layout_marginTop="220dp" />
 
    <EditText
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignEnd="@+id/imageView" />
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Coordonnées GPS : "
        android:id="@+id/textView3"
        android:textAlignment="center"
        android:layout_centerVertical="true"
        android:layout_toStartOf="@+id/editText"
        android:layout_below="@id/textView2"
        android:layout_marginTop="25dp"
        android:textColor="#000000"
        android:layout_alignParentStart="true" />
 
    <EditText
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:id="@+id/editText2"
        android:textSize="10dp"
        android:layout_marginTop="12dp"
        android:layout_below="@+id/textView2"
        android:layout_toEndOf="@+id/textView3" />
 
 
 
    <ImageButton
        android:id="@+id/Imagebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@+id/editText2"
        android:layout_below="@+id/textView2"
        android:src="@drawable/navigation"
        android:background="@color/white"
        android:layout_marginTop="25dp"
      />
 
 
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Essence de l&apos;arbre :"
        android:id="@+id/textView4"
        android:textAlignment="center"
        android:layout_centerVertical="true"
        android:layout_toStartOf="@+id/editText"
        android:layout_below="@id/textView2"
        android:layout_marginTop="69dp"
        android:textColor="#000000"
        android:layout_alignParentStart="true" />
 
    <EditText
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:id="@+id/editText3"
        android:layout_marginTop="12dp"
        android:layout_below="@+id/textView3"
        android:layout_toEndOf="@+id/textView4" />
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Diamètre(m) :"
        android:id="@+id/textView5"
        android:textAlignment="center"
        android:layout_centerVertical="true"
        android:layout_toStartOf="@+id/editText"
        android:layout_below="@id/textView3"
        android:layout_marginTop="69dp"
        android:textColor="#000000"
        android:layout_alignParentStart="true" />
 
    <EditText
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:id="@+id/editText5"
        android:layout_marginTop="12dp"
        android:layout_below="@+id/textView4"
        android:layout_toEndOf="@+id/textView5" />
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hauteur(m) :"
        android:id="@+id/textView6"
        android:textAlignment="center"
        android:layout_centerVertical="true"
        android:layout_toStartOf="@+id/editText"
        android:layout_below="@id/textView4"
        android:layout_marginTop="69dp"
        android:textColor="#000000"
        android:layout_alignParentStart="true" />
 
    <EditText
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:id="@+id/editText6"
        android:layout_marginTop="12dp"
        android:layout_below="@+id/textView5"
        android:layout_toEndOf="@+id/textView6" />
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Volume(m3) :"
        android:id="@+id/textView7"
        android:textAlignment="center"
        android:layout_centerVertical="true"
        android:layout_toStartOf="@+id/editText"
        android:layout_below="@id/textView5"
        android:layout_marginTop="69dp"
        android:textColor="#000000"
        android:layout_alignParentStart="true" />
 
    <EditText
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:id="@+id/editText7"
        android:layout_marginTop="12dp"
        android:layout_below="@+id/textView6"
        android:layout_toEndOf="@+id/textView7" />
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Qualité :"
        android:id="@+id/textView8"
        android:textAlignment="center"
        android:layout_centerVertical="true"
        android:layout_toStartOf="@+id/editText"
        android:layout_below="@id/textView6"
        android:layout_marginTop="69dp"
        android:textColor="#000000"
        android:layout_alignParentStart="true" />
 
    <EditText
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:id="@+id/editText8"
        android:layout_marginTop="12dp"
        android:layout_below="@+id/textView7"
        android:layout_toEndOf="@+id/textView8" />
 
 
 
 
    <DatePicker
        android:id="@+id/dpResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"/>
 
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        android:layout_row="1"
        android:layout_column="0"
        android:text="Enregistrer un arbre"
        android:layout_below="@+id/editText8"
        android:layout_marginTop="0dp"
        style="@style/button_text"
        android:background="@drawable/green_button"/>
 
 
</RelativeLayout>
 
 
</ScrollView>


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
<?xml version="1.0" encoding="utf-8"?>
 
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 
    package="com.example.enguerran.popopi">
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
 
 
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.google.android.gms.location.sample.basiclocationsample" />
</manifest>