Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Looks like there's not such API in Android. However, You still have the following options to fix the issue:</p> <ul> <li>Look for some open-source library which provides such ability;</li> <li><p>Do some manual string operations still using <code>XmlSerializer</code>, like provided below:</p> <pre><code>private void testXMLFiles() { //create a new file called "new.xml" in the SD card final File newXmlFile = new File(Environment.getExternalStorageDirectory() + "/download/teste/audit.xml"); RandomAccessFile randomAccessFile = null; final boolean fileExists = newXmlFile.exists(); String lastLine = null; if (fileExists) { try { randomAccessFile = new RandomAccessFile(newXmlFile, "rw"); randomAccessFile.seek(0); if (null != randomAccessFile) { final Scanner scanner = new Scanner(newXmlFile); int lastLineOffset = 0; int lastLineLength = 0; while (scanner.hasNextLine()) { // +1 is for end line symbol lastLine = scanner.nextLine(); lastLineLength = lastLine.length() + 2; lastLineOffset += lastLineLength; } // don't need last &lt;/root&gt; line offset lastLineOffset -= lastLineLength; // got to string before last randomAccessFile.seek(lastLineOffset); } } catch(FileNotFoundException e) { Log.e("FileNotFoundException", "can't create FileOutputStream"); } catch (IOException e) { Log.e("IOException", "Failed to find last line"); } } else { try { newXmlFile.createNewFile(); } catch(IOException e) { Log.e("IOException", "exception in createNewFile() method"); } try { randomAccessFile = new RandomAccessFile(newXmlFile, "rw"); } catch(FileNotFoundException e) { Log.e("FileNotFoundException", "can't create FileOutputStream"); } } //we create a XmlSerializer in order to write xml data XmlSerializer serializer = Xml.newSerializer(); if (randomAccessFile == null) { return; } try { final StringWriter writer = new StringWriter(); serializer.setOutput(writer); if (!fileExists) { serializer.startDocument(null, true); serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); serializer.startTag(null, "root"); } else { serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); } 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"); if (!fileExists) { serializer.endTag(null, "root"); } serializer.flush(); if (lastLine != null) { serializer.endDocument(); writer.append(lastLine); } // Add \n just for better output in console randomAccessFile.writeBytes(writer.toString() + "\n"); randomAccessFile.close(); Toast.makeText(getApplicationContext(), "Save!", Toast.LENGTH_SHORT).show(); } catch (Exception e) { Log.e("Exception","error occurred while creating xml file"); e.printStackTrace(); } } </code></pre></li> </ul> <p>Its output after second run is the following (quite similar to what You expect):</p> <pre><code>&lt;?xml version='1.0' 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> <ul> <li>Store all tags from initial xml (e.g. using <a href="http://developer.android.com/reference/javax/xml/parsers/SAXParser.html" rel="nofollow">SAXParser</a> you can read tags, write to new file the same time and apppend new ones at the end using <code>XMLSerializer</code>);</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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