Raised generosity as the only answer did not provide a good implementation for Android. Is there a faster implementation compatible with Android? Or is SimpleXML the best performance I get?
I am new to Java and Android, so I donβt know the correct procedure for deserializing the xml string for an object. I found a method that works in:
public static Object deserializeXMLToObject(String xmlFile,Object objClass) throws Exception { try { InputStream stream = new ByteArrayInputStream(xmlFile.getBytes("UTF-8")); Serializer serializer = new Persister(); objClass = serializer.read(objClass, stream); return objClass; } catch (Exception e) { return e; } }
Where xmlFile is the string (misnamed) xml and objClass is the empty class of the class I want to deserialize to. This is usually a list of other objects.
Class Example:
@Root(name="DepartmentList") public class DepartmentList { @ElementList(entry="Department", inline=true) public List<Department> DepartmentList =new ArrayList<Department>(); public boolean FinishedPopulating = false; }
Department Class:
public class Department { @Element(name="DeptID") private String _DeptID =""; public String DeptID() { return _DeptID; } public void DeptID(String Value) { _DeptID = Value; } @Element(name="DeptDescription") private String _DeptDescription =""; public String DeptDescription() { return _DeptDescription; } public void DeptDescription(String Value) { _DeptDescription = Value; } }
XML example:
<DepartmentList> <Department> <DeptID>525</DeptID> <DeptDescription>Dept 1</DeptDescription> </Department> <Department> <DeptID>382</DeptID> <DeptDescription>Dept 2</DeptDescription> </Department> </DepartmentList>
This works great throughout the application, but I have come to the conclusion that it needs to deserialize> 300 objects in the list. It takes about 5 seconds or close to a minute when debugging, but users are not happy with this performance and wasted time when debugging is undesirable. Is there any way to speed this up? Or should I do it differently? Preferably only by changing the deserializeXMLToObject method.
source share