XSD anytype和JAXB [英] XSD anytype and JAXB

查看:114
本文介绍了XSD anytype和JAXB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xsd定义(来自www.tmforum.org ossj common api v1.5)

I have an xsd definition (from www.tmforum.org ossj common api v1.5)

<element name="primaryKey" nillable="false">
   <complexType mixed="false">                   
      <complexContent mixed="false">
         <extension base="anyType"/>                   
      </complexContent>
   </complexType>
</element>

并希望生成如下xml

and would like to generate an xml as follows

<ossj-co-v1-5:primaryKey>mykey</ossj-co-v1-5:primaryKey>

使用xjc从xsd生成的PrimaryKey类需要将DOM元素存储在列表中(参见底部生成的PrimaryKey类。myKey这里是一个TextNode,因为它不是DOM元素,所以不能将它添加到xjc生成的PrimaryKey类中。我应该如何继续获取所需的输出?

The PrimaryKey class generated from the xsd using xjc requires a DOM Element to be stored in a list (see the generated PrimaryKey class at the bottom". "myKey" here is a TextNode and since its not an DOM Element, it cannot be added to xjc generated PrimaryKey class. How should I proceed to get the required output?

这是从xsd生成的PrimaryKey类

Here is the PrimaryKey class generated from the xsd

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
    "any"
    })
public static class PrimaryKey {

    @XmlAnyElement
    protected List<Element> any;
    @XmlAnyAttribute
    private Map<QName, String> otherAttributes = new HashMap<QName, String>();

    public List<Element> getAny() {
        if (any == null) {
            any = new ArrayList<Element>();
        }
        return this.any;
    }


    public Map<QName, String> getOtherAttributes() {
        return otherAttributes;
    }

}


推荐答案

以下对象模型适用于您的方案。我将尝试挖掘适当的架构自定义以生成这些对象模型。

The following object models would work for your scenario. I'll try to dig up the approprate schema customizations to produce these object models.

选项#1

您的代码可能如下所示。这意味着元素primaryKey将导致对象PrimaryKey被实例化,并在any属性上设置相应的文本内容。

You could have your code look like the following. This would mean that the element "primaryKey" would cause the object PrimaryKey to be instantiated with the corresponding text content being set on the any property.

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = {"any" }) 
public static class PrimaryKey { 

    @XmlValue
    protected String any; 

    @XmlAnyAttribute 
    private Map<QName, String> otherAttributes = new HashMap<QName, String>(); 

    public List<Element> getAny() { 
        if (any == null) { 
            any = new ArrayList<Element>(); 
        } 
        return this.any; 
    } 


    public Map<QName, String> getOtherAttributes() { 
        return otherAttributes; 
    } 

} 

选项#2

如果您希望外部对象具有与primaryKey对应的String属性,则可以执行以下操作:

If you want an outer object to have a String property corresponding to the primaryKey you could do the following:

@XmlAccessorType(XmlAccessType.FIELD) 
public class Root {

    // @XmlElement is implied
    private String primaryKey;

}

这篇关于XSD anytype和JAXB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆