Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I insert a new node to an existing XML
    text
    copied!<p>I would like to insert a new node into a existent xml file, but The code below inserts all the nodes again.</p> <p>I do a test if the file exists. If not, I create a new xml file and write the tags. If exists, it also creates the nodes, but the wrong way.</p> <pre><code>//create a new file called "new.xml" in the SD card File newxmlfile = new File(Environment.getExternalStorageDirectory() + "/download/teste/audit.xml"); if (newxmlfile.exists()){ try{ fileos = new FileOutputStream(newxmlfile, true); }catch(FileNotFoundException e){ Log.e("FileNotFoundException", "can't create FileOutputStream"); } } else { try{ newxmlfile.createNewFile(); }catch(IOException e){ Log.e("IOException", "exception in createNewFile() method"); } try{ fileos = new FileOutputStream(newxmlfile); }catch(FileNotFoundException e){ Log.e("FileNotFoundException", "can't create FileOutputStream"); } } //we create a XmlSerializer in order to write xml data 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, "root"); serializer.startTag(null, "child1"); serializer.endTag(null, "child1"); serializer.startTag(null, "child2"); serializer.attribute(null, "attribute", "value"); serializer.endTag(null, "child2"); serializer.startTag(null, "child3"); serializer.text("some text inside child3"); serializer.endTag(null, "child3"); serializer.endTag(null, "root"); serializer.endDocument(); serializer.flush(); fileos.close(); Context context = getApplicationContext(); CharSequence text = "Save!"; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show(); } catch (Exception e) { Log.e("Exception","error occurred while creating xml file"); } </code></pre> <p>The result is this:</p> <pre><code>&lt;?xml version='1.0' encoding='UTF-8' standalone='yes' ?&gt; &lt;root&gt; &lt;child1 /&gt; &lt;child2 attribute="value" /&gt; &lt;child3&gt;some text inside child3&lt;/child3&gt; &lt;/root&gt;&lt;?xml version='1.0' encoding='UTF-8' standalone='yes' ?&gt; &lt;root&gt; &lt;child1 /&gt; &lt;child2 attribute="value" /&gt; &lt;child3&gt;some text inside child3&lt;/child3&gt; &lt;/root&gt; </code></pre> <p>But I want the result like this:</p> <pre><code>&lt;?xml version='1.0' encoding='UTF-8' standalone='yes' ?&gt; &lt;root&gt; &lt;child1 /&gt; &lt;child2 attribute="value" /&gt; &lt;child3&gt;some text inside child3&lt;/child3&gt; &lt;child1 /&gt; &lt;child2 attribute="value" /&gt; &lt;child3&gt;some text inside child3&lt;/child3&gt; &lt;/root&gt; </code></pre> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload