我给XML,看起来像(当然其它更多的属性):JAXB - 反编组态对象
<inventory>
<item kind="GRILL" tag=123 brand="WEBER"/>
<item kind="CAR" tag=124 make="FORD" model="EXPLORER" />
</inventory>
有大约十几个不同类型。我使用注释映射到看起来像Java类:
@XmlRootElement(name="inventory")
public class Inventory {
@XmlElement(name="item")
public List<Item> itemList = new LinkedList<Item>;
}
abstract public class Item {
@XmlAttribute public int tag;
}
public class Grill extends Item {
@XmlAttribute public string brand;
}
public class Car extends Item {
@XmlAttribute public string make;
@XmlAttribute public string model;
}
我怎样才能得到JAXB创建基于“种类”字段的子类项目的对象?
可能重复[Java/JAXB:Unmars大厅Xml基于特定的子类](http://stackoverflow.com/questions/2992234/java-jaxb-unmarshall-xml-to-specific-subclass-based-on-attribute) – 2011-03-30 23:21:06
你是对的...我在发布之前做了一次搜索,但不知何故错过了它。 – dave 2011-03-30 23:34:12