我正在尝试使用JaxB解组xml文件。尽管我的字段与属性属性有所不同。下面是我的xml文件的示例:
<?xml version="1.0" encoding="UTF-8"?><translationObjectFile fileType="ASSETMETADATA">
<translationObjectProperties>
<property isMultiValue="false" nodePath="/content/dam/we-retail/fr/activities/running/running-woods-woman.jpg/jcr:content/metadata" propertyName="jcr:title">Running Woods Woman</property>
<property isMultiValue="false" nodePath="/content/dam/we-retail/fr/activities/running/running-woods-woman.jpg/jcr:content/metadata" propertyName="dc:description">Test101 Desc</property>
<property isMultiValue="true" nodePath="/content/dam/we-retail/fr/activities/running/running-woods-woman.jpg/jcr:content/metadata" propertyName="dc:subject">
<value order="0">Test101 - Key1</value>
<value order="1">Test101 - Key2</value>
<value order="2">Test101 - Key3</value>
</property>
<property isMultiValue="false" nodePath="/content/dam/we-retail/fr/activities/running/running-woods-woman.jpg/jcr:content/metadata" propertyName="dc:title">Running Woods Woman - Test101</property>
</translationObjectProperties>
</translationObjectFile>
如您在上面看到的,名为 isMultiValue 的属性描述了一个属性是单个值还是值列表。我可以通过执行以下操作获取值列表:
@XmlElement(name = "value")
private List<String> values;
尽管对于单个值,它返回null。也许是因为它在确切值之前没有值标签?然后,我尝试通过执行以下操作获取单个值:
@XmlValue
private String value;
我想添加一个条件来检查 isMultiValue 是否为true,因为它将使用List类型,否则将使用单个字符串实现。虽然我迷失了如何做。
以下是我的解组功能:
String inputStream = IOUtils.toString(xmlInputStream, "UTF-8");
final JAXBContext jc = JAXBContext.newInstance(TranslationObjectFile.class);
final Unmarshaller unmarshaller = jc.createUnmarshaller();
Object value = unmarshaller.unmarshal(new StringReader(inputStream));
return (T) value;
提前谢谢!