我正在尝试在春季使用Castor OXM解组将XML解组到对象。
XML:
<LevelA>
<LevelB>
<item name="itemA">value</item>
<item name="itemB">value</item>
<item name="itemC">value</item>
<item name="itemD">value</item>
<item name="itemE">value</item>
</LevelB>
</LevelA>
Object:
Class LevelA {
LevelB objLevelB;
//getter and setter
}
Class LevelB {
List<Items> item = new ArrayList<Items>();
//getter and setter
}
Class Items {
String Name;
String Value;
//getter and setter
}
Castor Mapping:
<mapping>
<class name="LevelA">
<map-to xml="LevelA" />
<field name="objLevelB" type="LevelB">
<bind-xml name="LevelB" />
<class name="LevelB">
<field name="item" type="Items" collection="arraylist">
<bind-xml name="item" node="element" />
<class name="Items">
<field name="Name" type="string">
<bind-xml name="name" node="attribute" />
</field>
<field name="Value" type="string">
<bind-xml node="text" />
</field>
</class>
</field>
</class>
</field>
</class>
</mapping>
我收到以下错误:
org.xml.sax.SAXException: unable to find FieldDescriptor for 'item' in ClassDescriptor of LevelB
我已经尝试了几种方法,并花费了足够的时间,我现在需要你们所有人的帮助。
任何帮助都会很有用。
自己想出了方法,这是对我有用的映射。我正在分享答案以帮助他人,在同样的情况下。
<?xml version="1.0" encoding="UTF-8"?>
<mapping>
<description> Provides Mapping to Convert Document to POJO</description>
<class name="LevelAClass">
<map-to xml="LevelA" />
<field name="objLevelB" type="LevelBClass">
<bind-xml name="LevelB" />
</field>
</class>
<class name="LevelBClass">
<field name="item" type="LevelCClass"
collection="arraylist">
<bind-xml name="item" />
</field>
</class>
<class name="LevelCClass">
<field name="Name">
<bind-xml name="name" node="attribute" />
</field>
<field name="Value">
<bind-xml name="item" node="text" />
</field>
</class>
</mapping>