Spring Castor Unmarshalling with Mapping

时间:2016-02-28 06:43:00

标签: spring unmarshalling castor oxm

我正在尝试使用Spring中的Castor OXM Unmarshalling将XML解组为Object。

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

我已经尝试了几种方法,花了足够的时间,现在我需要你们的帮助。

任何帮助都会有用。

1 个答案:

答案 0 :(得分:1)

想出自己的方式,这里是适合我的映射。在同样的情况下,我正在分享帮助他人的答案。

<?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>