尝试更改反序列化空字符串的方式时的ConvertException:字段所需的元素注释

时间:2012-02-04 04:13:42

标签: java xml-serialization simple-framework

我有一个字典式的应用程序,我正在使用Java,我正在使用Simple框架进行序列化。它包含一个字符串的ArrayList,我遇到的唯一问题是Simple将序列化/反序列化这些字符串并将任何空值更改为null。我从this question了解到你需要使用转换器。我按照该页面上描述的内容添加了Sand在他的答案中写的类(需要更改“新的AnnotationStrategy()”),现在这就是我遇到问题的主要类看起来像(带有删除了众多的get / set方法):

    public class Item  {

    @ElementList(name = "itemContents")
    @Convert(SimpleXMLStringConverter.class)
    private ArrayList<String> values;

    public Item()
    {
    }
}

每当我尝试将类写入XML文件时,都会收到此错误:

org.simpleframework.xml.convert.ConvertException: Element annotation required for field 'values' private java.util.ArrayList main.LangComp.Item.values
    at org.simpleframework.xml.convert.ConverterScanner.getConvert(ConverterScanner.java:147)
    at org.simpleframework.xml.convert.ConverterScanner.getConvert(ConverterScanner.java:121)
    at org.simpleframework.xml.convert.ConverterScanner.getConverter(ConverterScanner.java:100)
    at org.simpleframework.xml.convert.AnnotationStrategy.write(AnnotationStrategy.java:175)
    at org.simpleframework.xml.convert.AnnotationStrategy.write(AnnotationStrategy.java:155)
    at org.simpleframework.xml.core.Source.setOverride(Source.java:394)
    at org.simpleframework.xml.core.Factory.setOverride(Factory.java:170)
    at org.simpleframework.xml.core.Composite.isOverridden(Composite.java:1387)
    at org.simpleframework.xml.core.Composite.writeElement(Composite.java:1309)
    at org.simpleframework.xml.core.Composite.writeUnion(Composite.java:1194)
    at org.simpleframework.xml.core.Composite.writeElements(Composite.java:1165)
    at org.simpleframework.xml.core.Composite.writeSection(Composite.java:1071)
    at org.simpleframework.xml.core.Composite.write(Composite.java:1042)
    at org.simpleframework.xml.core.Composite.write(Composite.java:1019)
    at org.simpleframework.xml.core.Traverser.write(Traverser.java:236)
    at org.simpleframework.xml.core.CompositeList.write(CompositeList.java:248)
    at org.simpleframework.xml.core.Composite.writeElement(Composite.java:1331)
    at org.simpleframework.xml.core.Composite.writeElement(Composite.java:1314)
    at org.simpleframework.xml.core.Composite.writeUnion(Composite.java:1194)
    at org.simpleframework.xml.core.Composite.writeElements(Composite.java:1165)
    at org.simpleframework.xml.core.Composite.writeSection(Composite.java:1071)
    at org.simpleframework.xml.core.Composite.write(Composite.java:1042)
    at org.simpleframework.xml.core.Composite.write(Composite.java:1019)
    at org.simpleframework.xml.core.Traverser.write(Traverser.java:236)
    at org.simpleframework.xml.core.Traverser.write(Traverser.java:208)
    at org.simpleframework.xml.core.Traverser.write(Traverser.java:186)
    at org.simpleframework.xml.core.Persister.write(Persister.java:1187)
    at org.simpleframework.xml.core.Persister.write(Persister.java:1169)
    at org.simpleframework.xml.core.Persister.write(Persister.java:1147)
    at org.simpleframework.xml.core.Persister.write(Persister.java:1266)
    at org.simpleframework.xml.core.Persister.write(Persister.java:1248)
    at org.simpleframework.xml.core.Persister.write(Persister.java:1229)
    at org.simpleframework.xml.core.Persister.write(Persister.java:1208)
    at main.IO.FileHandler.WriteXML(FileHandler.java:32)
    at main.UI.MainUI.formWindowClosing(MainUI.java:108)
    at main.UI.MainUI.access$000(MainUI.java:16)
    at main.UI.MainUI$1.windowClosing(MainUI.java:76)
    at java.awt.Window.processWindowEvent(Window.java:2045)
    at javax.swing.JFrame.processWindowEvent(JFrame.java:296)
    at java.awt.Window.processEvent(Window.java:2003)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Window.dispatchEventImpl(Window.java:2713)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:680)
    at java.awt.EventQueue$4.run(EventQueue.java:678)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

之前我能够让这个程序工作,唯一的问题是它会将空字符串变为null,这是我需要避免的。我无法弄清楚导致问题的原因或解决方法。是因为我从我链接的问题得到的转换器类是用于字符串,而不是ArrayLists,还是无关紧要因为XML节点是字符串而不是列表?当它说“字段所需的元素注释”时,我无法理解它想要的注释。

1 个答案:

答案 0 :(得分:2)

由于我使用的是@ElementList,因此抛出了错误消息,此过程需要@Element。我发现了一种方法可以使它与ArrayLists一起工作,因为我放弃了尝试让它在一个实例中工作,只是希望转换器适用于一切。我不知道这是一个好方法,但它的确有效。我只是按照here描述的步骤来创建一个RegistryStrategy,而不是将转换器绑定到我的类(它不接受),我将它绑定到String.class;换句话说,序列化过程并不关心它是否存储在复杂的类ArrayList中,如果它是一个String,那么如果它被读入为null,它将被设置为“”。