Naing Win Htun Naing Win Htun - 2 days ago 4
Android Question

Android XMLSerializer cannot write all data into XML file

Now, I am trying to write XML file in running uiautomation test by uiautomator.
I used XMLSerializer for write XML in android. It is OK. It write XML file while running test. One thing that it cannot write all data as shown below.

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<tests>
<test>
<no>1</no>
<desc>Test One</desc>
</test>
<test>
<no>2</no>
<desc>Test Two</desc>
</test>
<tes


See, it cannot write all data. Here my code.

// Export XML file content.
XmlSerializer serializer = Xml.newSerializer();

StringWriter stringWriter = new StringWriter();

serializer.setOutput(stringWriter);
serializer.startDocument("UTF-8", true);
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);

serializer.startTag(null, "tests");

for(Test test : testList){
serializer.startTag(null, "test");

serializer.startTag(null, "no");
serializer.text(test.getNo());
serializer.endTag(null, "no");

serializer.startTag(null, "desc");
serializer.text(test.getDesc());
serializer.endTag(null, "desc");

serializer.endTag(null, "test");
}

serializer.endTag(null, "tests");

// Write file.
FileWriter fileWriter = new FileWriter("test.xml");

fileWriter.write(stringWriter.toString());

fileWriter.close();


My testList has at least 15 test. So, it should write all tests. But it writes only 2. Why! I don't know.

Help me. Thanks.

Answer

I would prefer to use Editable in this case.

Example:

 Editable editable;
....
....
....
 editable.append("<Type> " + "Name"+ " </Type>" + "<Class>" +"Class Name" + "</Class>");

It's easier and does the same thing. Hope it helps. :)

Comments