简体   繁体   English

如何在JAXB中向元素添加属性

[英]How to add Attribute to Element in JAXB

I am new to JAXB library and not able to get the solution for adding @XmlAttribute to the existing Code. 我是JAXB库的新手,无法获得将@XmlAttribute添加到现有代码的解决方案。

I have an XML which have two element (Name and Value) as show below : 我有一个XML,其中包含两个元素(名称和值),如下所示:

<ns4:Envelope xmlns:ns2="xyz/123" xmlns:ns3="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns4="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns6="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns5="http://www.w3.org/2001/XMLSchema" xmlns:abc="abc">
    <ns4:Header>
        <ns2:ID ns4:mustUnderstand="1">testId</ns2:ID>
    </ns4:Header>
    <ns4:Body>
        <ns2:Set>
            <List ns3:arrayType="abc:hash[1]">
                <Struct>
                    <Name>Interval</Name>
                    <Value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:int">10</Value>
                </Struct>
            </List>
        </ns2:Set>
    </ns4:Body>
</ns4:Envelope>

In the above XML, the xsi:type is been getting generated Automatically by JAXB. 在上述XML中,JAXB已自动生成xsi:type。 The Struct Class having the below Code 具有以下代码的Struct类

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Struct", propOrder = {
    "name",
    "value"
})
public class Struct {

    @XmlElement(name = "Name", required = true)
    protected String name;
    @XmlElement(name = "Value", required = true)
    protected Object value;

    public String getName() {
        return name;
    }
    public void setName(String value) {
        this.name = value;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

As you can see the type is not been set in the above class. 如您所见,没有在上面的类中设置类型。 I want to have some provision to add type as per user defined, rather than getting generated as per the data type. 我希望有一些规定可以根据用户定义添加类型,而不是根据数据类型生成。

I am not sure how can I add Attribute to this Struct Class, which can add the type to "Value" Element (As we can see the Value Element is Simple String Object) 我不确定如何将属性添加到该Struct类,该类可以将类型添加到“ Value”元素(如我们所见,Value元素是简单字符串对象)

If you just want a type attribute, try something like this: 如果只需要type属性,请尝试以下操作:

public class TypedValue {

    @XmlAttribute
    protected String type;
    @XmlValue
    protected String value;
}

instead of Object in Struct.value . 而不是Struct.valueObject

The xsi:type is a very special thing for inheritance. xsi:type对于继承是非常特殊的事情。

If you want to customize how JAXB represents something you can always create an XmlAdapter for it. 如果要自定义JAXB表示的XmlAdapter ,则可以始终为其创建XmlAdapter

Adaping a Field/Property 修改字段/属性

The @XmlJavaTypeAdatper annotation is used to reference the XmlAdapter . @XmlJavaTypeAdatper批注用于引用XmlAdapter

@XmlElement(name = "Value", required = true)
@XmlJavaTypeAdapter(ValueAdapter.class)
protected Object value;

XmlAdapter XmlAdapter

Is responsible for converting between the real object in your domain model, and the one you want to marshal to get the desired XML. 负责在您的域模型中的实际对象和您想要封送以获得所需XML的对象之间进行转换。

public class ValueAdapter extends XmlAdapter<MyAdaptedObject, Object> {
    ...
}

MyAdaptedObject MyAdaptedObject

Is a POJO that will marshal to your desired XML. 是一种POJO,将封送您所需的XML。

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

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