简体   繁体   English

如何使用 JAXB 解组包含混合标记(具有属性,并且具有嵌套标记的内容值)的 XML 文件?

[英]How to unmarshal XML file that contains mixed tag (has attributes, and has content value with nested tag) with JAXB?

I need to convert xml file to java objects.我需要将 xml 文件转换为 java 个对象。

<PRODUCT id="10" name="Notebook">
    <VALUE id="30" type="Formatted">This is mixed <TUNIT style="style-12">formatted</TUNIT> text value.</VALUE>
</PRODUCT>

Here is Product class:这是产品 class:

@Getter
@Setter
@XmlRootElement(name = "PRODUCT")
@XmlAccessorType(XmlAccessType.FIELD)
public class Product {

    @XmlAttribute(name = "id")
    private String id;

    @XmlAttribute(name = "name")
    private String name;

    @XmlElementRef(name = "VALUE")
    private Value value;
}

Here is Value class:这是值 class:

@Getter
@Setter
@XmlRootElement(name = "VALUE")
@XmlAccessorType(XmlAccessType.FIELD)
public class Value {

    @XmlAttribute(name = "id")
    private String id;

    @XmlAttribute(name = "type")
    private String type;

    @XmlValue
    private String content;

    @XmlElementRef(name = "TUNIT")
    private Tunit tunit;
}

Here is Tunit class:这是 Tunit class:

@Getter
@Setter
@XmlRootElement(name = "TUNIT")
@XmlAccessorType(XmlAccessType.FIELD)
public class Tunit {

    @XmlAttribute(name = "style")
    private String style;

    @XmlValue
    private String content;
}

When I set @XmlAttribute for <VALUE> attribute id, @XmlValue for <VALUE> content, and @XmlElementRef for <TUNIT> - I'm getting an error:当我为<VALUE>属性 ID 设置 @XmlAttribute、为<VALUE>内容设置 @XmlValue 以及为<TUNIT>设置 @XmlElementRef 时 - 我收到一个错误:

If a class has @XmlElement property, it cannot have @XmlValue property.

Is it possible to unmarshal this xml with JAXB?是否可以用 JAXB 解组此 xml?

Within your <VALUE...>...</VALUE> element you have mixed content: plain text and a <TUNIT> element.在您的<VALUE...>...</VALUE>元素中,您有混合内容:纯文本和<TUNIT>元素。

Therefore, in your Value class you need to define a List<Object> property to receive this mixed content (in your case strings and objects of type Tunit . For this to happen you need to annotate it with @XmlMixed and also with @XmlElementRef (to define the mapping between XML <TUNIT> and Java Tunit ). See also the example in the API documentation of @XmlMixed .因此,在您的Value class 中,您需要定义一个List<Object>属性来接收此混合内容(在您的情况下是字符串和Tunit类型的对象。为此,您需要使用@XmlMixed@XmlElementRef (定义 XML <TUNIT>和 Java Tunit之间的映射。另请参阅 @XmlMixed 的API 文档中的@XmlMixed

For your XML example with the XML fragment对于带有 XML 片段的 XML 示例
This is mixed <TUNIT style="style-12">formatted</TUNIT> text value.
the mixed content list in the Value object will receive these items: Value object 中的混合内容列表将收到以下项目:

  • a string "This is mixed "一个字符串"This is mixed "
  • a Tunit object Tunit
  • a string " text value."一个字符串" text value."

So finally the Value class will look like this所以最终Value class 看起来像这样

@XmlRootElement(name = "VALUE")
@XmlAccessorType(XmlAccessType.FIELD)
public class Value {

    @XmlAttribute(name = "id")
    private String id;

    @XmlAttribute(name = "type")
    private String type;

    @XmlMixed
    @XmlElementRef(name = "TUNIT", type = Tunit.class)
    private List<Object> content;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM