JaxB的最后一个字段在解组时返回null

时间:2019-06-03 03:35:49

标签: java xml jaxb marshalling unmarshalling

我正在尝试使用JaxB解组xml文件。它获取除最后一部分(值)以外的所有值。该值可以是简单值,也可以是值列表。我可以通过排除translationObjectValue来获取值,但这仅适用于单个值,不适用于值列表。我想支持两种值类型。下面是我的xml文件的示例。

    <?xml version="1.0" encoding="UTF-8"?><translationObjectFile fileType="ASSETMETADATA">
  <translationObjectProperties>
    <property isMultiValue="false" nodePath="/content/dam/we-retail/fr/experiences/steelhead-and-spines-in-alaska/steelhead-and-spines-in-alaska-1.jpg/jcr:content/metadata" propertyName="dc:description">Test92 - Desc</property>
    <property isMultiValue="false" nodePath="/content/dam/we-retail/fr/experiences/steelhead-and-spines-in-alaska/steelhead-and-spines-in-alaska-1.jpg/jcr:content/metadata" propertyName="dc:rights">Sample Photography</property>
    <property isMultiValue="true" nodePath="/content/dam/we-retail/fr/experiences/steelhead-and-spines-in-alaska/steelhead-and-spines-in-alaska-1.jpg/jcr:content/metadata" propertyName="dc:subject">
      <value order="0">alaska</value>
      <value order="1">bolivia</value>
      <value order="2">camping</value>
      <value order="3">condoriri</value>
    </property>
    <property isMultiValue="false" nodePath="/content/dam/we-retail/fr/experiences/steelhead-and-spines-in-alaska/steelhead-and-spines-in-alaska-1.jpg/jcr:content/metadata" propertyName="dc:title">Test92 - Title</property>
  </translationObjectProperties>
</translationObjectFile>

下面是我的吸气器/吸气器。

TranslationObjectFile

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class TranslationObjectFile {
    @XmlAttribute
    private String fileType;
    @XmlAttribute
    private String sourcePath;
    @XmlElementWrapper
    @XmlElement(name = "property")
    private List<TranslationObjectProperty> translationObjectProperties;

TranslationObjectProperty

@XmlAccessorType(XmlAccessType.FIELD)
public class TranslationObjectProperty {
    @XmlAttribute
    private boolean isMultiValue;
    @XmlAttribute
    private String nodePath;
    @XmlAttribute(name = "propertyName")
    private String propertyNameValue;
    @XmlElementWrapper
    @XmlElement(name = "value")
    private List<TranslationObjectValue> subjectValue;

TranslationObjectValue

@XmlAccessorType(XmlAccessType.FIELD)
public class TranslationObjectValue {
    @XmlValue
    private String value;

以下是用于解组内容的代码:

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;

提前谢谢!

0 个答案:

没有答案