0

我有具有以下结构的大型 xml 树:

<?xml version="1.0"?>
<SampleElem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <Children>
      <SampleElem>
         <Children/>
         <Id>1</Id>
         <Value>Test2</Value>
      </SampleElem>
   </Children>
   <Id>-1</Id>
   <Value>Test1</Value>
</SampleElem>

我正在尝试使用simpleframework反序列化它:

Serializer serializer = new Persister();
SampleElem root = serializer.read(SampleElem.class, someStream, false);

一段时间后,应用程序因错误而停止。从设备记录:

E/AndroidRuntime(13591): FATAL EXCEPTION: AsyncTask #1
E/AndroidRuntime(13591): java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime(13591):    at android.os.AsyncTask$3.done(AsyncTask.java:200)
E/AndroidRuntime(13591):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
E/AndroidRuntime(13591):    at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
E/AndroidRuntime(13591):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
E/AndroidRuntime(13591):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
E/AndroidRuntime(13591):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
E/AndroidRuntime(13591):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
E/AndroidRuntime(13591):    at java.lang.Thread.run(Thread.java:1019)
E/AndroidRuntime(13591): Caused by: java.lang.StackOverflowError
E/AndroidRuntime(13591):    at java.lang.StringBuilder.append(StringBuilder.java:217)
E/AndroidRuntime(13591):    at java.lang.reflect.Modifier.toString(Modifier.java:285)
E/AndroidRuntime(13591):    at java.lang.reflect.Method.toString(Method.java:538)
E/AndroidRuntime(13591):    at java.lang.reflect.Method.equals(Method.java:361)
E/AndroidRuntime(13591):    at org.apache.harmony.lang.annotation.AnnotationFactory.invoke(AnnotationFactory.java:307)
E/AndroidRuntime(13591):    at $Proxy1.inline(Native Method)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.ElementListLabel.getConverter(ElementListLabel.java:160)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.CacheLabel.getConverter(CacheLabel.java:280)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite.readVariable(Composite.java:604)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite.readInstance(Composite.java:573)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite.readUnion(Composite.java:549)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite.readElement(Composite.java:532)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite.readElements(Composite.java:445)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite.access$400(Composite.java:59)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite$Builder.read(Composite.java:1383)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite.read(Composite.java:201)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite.read(Composite.java:148)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Traverser.read(Traverser.java:92)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.CompositeList.populate(CompositeList.java:175)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.CompositeList.read(CompositeList.java:120)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite.readVariable(Composite.java:623)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite.readInstance(Composite.java:573)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite.readUnion(Composite.java:549)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite.readElement(Composite.java:532)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite.readElements(Composite.java:445)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite.access$400(Composite.java:59)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite$Builder.read(Composite.java:1383)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite.read(Composite.java:201)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite.read(Composite.java:148)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Traverser.read(Traverser.java:92)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.CompositeList.populate(CompositeList.java:175)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.CompositeList.read(CompositeList.java:120)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite.readVariable(Composite.java:623)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite.readInstance(Composite.java:573)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite.readUnion(Composite.java:549)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite.readElement(Composite.java:532)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite.readElements(Composite.java:445)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite.access$400(Composite.java:59)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite$Builder.read(Composite.java:1383)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite.read(Composite.java:201)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite.read(Composite.java:148)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Traverser.read(Traverser.java:92)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.CompositeList.populate(CompositeList.java:175)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.CompositeList.read(CompositeList.java:120)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite.readVariable(Composite.java:623)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Composite.readInstance(Composite.java:573)
E/AndroidRuntime(13591):    at org.simpleframework.xml.core.Compos

数据完全正确,输入的xml完全有效。你有什么想法?

谢谢!

更新:

树策略呢?它应该如何使用?你有什么例子吗?

4

3 回答 3

1

您无法在 Android 上更改默认堆栈大小或现有线程的堆栈大小。但是,您可以在创建线程时设置堆栈大小。使用完整的构造函数:

public Thread(ThreadGroup group, Runnable runnable, String threadName, long stackSize)

您可以将堆栈大小设置为最大 256KB。

因此,您的策略是创建一个具有深堆栈的新线程,并在该线程上运行此解析代码。

于 2013-05-08T00:56:21.640 回答
0

通过传递以下 JVM 参数来更改堆栈大小:

-Xss128m

因为,您的 XML 文件很大,并且解析器似乎正在执行递归(可能是因为<SampleElem>可以<SampleElem>作为一个孩子)堆栈大小需要增加。默认大小仅为 512 KB。

注意:如果解析仍然失败,128m;继续加倍尺寸(比如 256m,然后是 512m 等)

于 2013-05-07T22:09:46.120 回答
0

在我的案例中,最令人满意的解决方案是将我的 xml 模式重写为线性结构。它减少了反序列化树的总内存需求。

谢谢大家的意见。

于 2013-06-18T18:21:05.227 回答