如何在JAXB编组期间向类添加包装器元素 [英] How to add wrapper element to class during the JAXB marshalling

查看:80
本文介绍了如何在JAXB编组期间向类添加包装器元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Moxy Marshalling 方法创建 XML .除了一件小事情,一切似乎都工作正常.基本上,我想知道如何在编组期间向类本身添加包装器元素.

I am trying to create XML using the Moxy Marshalling approach. Everything seems to be working fine except for one small thing. Basically, I would like to know how to add wrapper element to the class itself during the marshalling.

我们知道,我们可以添加 @XmlPath("test/name/text()"),以将包装器添加到任何 String 元素中.同样为了收集,我们可以使用 @XmlElementWrapper(name ="languages") @XmlElement(name ="language").但是这些是针对 class 中的字段的.如何将包装器元素添加到已编组的类本身中?

As we are aware we can add @XmlPath("test/name/text()") to add a wrapper to any of the String elements. Also for collection we can make use of @XmlElementWrapper(name="languages"), @XmlElement(name="language"). But these are for the fields within the class. How can I add the wrapper element to the class itself which is being marshalled?

我有以下课程:

@XmlRootElement(name = "customer")
@XmlType(name="customer",propOrder={"name","languages"})
@XmlAccessorType(XmlAccessType.FIELD)   
public class Customer{
    
    @XmlPath("extension/name/text()")
    private String name;
    
    @XmlElementWrapper(name = "languages")
    @XmlElement(name = "language")
    private List<String> languages;
    
    //Getter, Setter and other constuctors
}

这将产生如下所示的输出 XML :

This will produce the output XML something like this:

<customer>
    <extension>
        <name>Hello</name>
    </extension>
    <languages>
        <language>English</language>
        <language>UK English</language>
    </languages>
</customer>

但是,我想将 wrapper 元素添加到整个类的事件中,以便输出看起来像这样:(注意整个 customer classWrapper )

However, I would like to add the wrapper element to event the whole class so that output would look something like this: (Notice the whole customer is wrapper within classWrapper)

<classWrapper>
    <customer>
        <extension>
            <name>Hello</name>
        </extension>
        <languages>
            <language>English</language>
            <language>UK English</language>
        </languages>
    </customer>
</classWrapper>

我尝试将 @XmlPath @XmlElementWrapper 添加到 Customer 类,但是由于它只能添加到班级中的所有字段,但不是整个班级的字段.

I tried adding the @XmlPath and @XmlElementWrapper to the Customer class but it throws an error as it can be added only to the fields within the class but not to the whole class.

以下是我的 Main 类,该类将用于编组:

Following is my Main class which would be used for marshalling:

public class HelloWorld{

     public static void main(String []args){
        Customer customer = new Customer();
        
        List<String> languages = new ArrayList<String>();
        languages.add("English");
        languages.add("UK English");
        
        customer.setName("Hello");
        customer.setLanguages(languages);
        
        JAXBContext context = JAXBContext.newInstance(Customer.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(customer,System.out);
     }
}

我知道我可以编写另一个 wrapper 类向其中添加自定义字段,然后使用该 wrapper 类进行 marshalling.实际上,我现在所做的与现在相同.但是,尝试查找是否有某种 Moxy 方法来执行此操作,类似于我们使用 @XmlPath @XmlElementWrapper

I am aware that I can write another wrapper class add the custom field to it then use that wrapper class for the marshalling. Actually, I am doing the same as of now. However, trying to find if there is some Moxy approach of doing this similar to how we have @XmlPath and @XmlElementWrapper

有人可以建议某种形式的 Moxy Jaxb 相关方法来实现整个类的wrapper元素吗?

Can someone please suggest some form of Moxy Jaxb related approach for achieving the wrapper element to the whole class?

推荐答案

这是一个临时解决方法,可以满足我的预期.在此处发布答案,以便将来对某人有所帮助.

This is a temporary workaround that works as expected for me. Posting the answer here so it can be helpful to someone in the future.

  1. @XmlRootElement(客户")更改为 @XmlRootElement("classWrapper").因此,您将获得 classWrapper 作为外部元素.

  1. Change the @XmlRootElement("customer") to @XmlRootElement("classWrapper"). So you will get the classWrapper as the outer element.

然后使用 @XmlPath 更改 Customer 类中的所有元素,以便所有元素都位于 Customer 标记下.所以整体 customer.class 看起来像这样:

Then change all the element within the Customer class using the @XmlPath so that all element go under the Customer tag. So overall customer.class would look something like this:

    @XmlRootElement(name = "classWrapper")
    @XmlType(name="customer",propOrder={"name","languages"})
    @XmlAccessorType(XmlAccessType.FIELD)
    @Getter
    @Setter
    public class Customer{
    
      @XmlPath("customer/extension/name/text()")
      private String name;
    
      @XmlPath("customer/languages/language/text()")
      private List<String> languages;
    
      //Getter, Setter and other constuctors
    }

使用 @XmlPath 时只是一个提示:如果text() 不是简单类型(如String、Date 等类型),请不要使用.

Just a tip while using the @XmlPath: do not use text() if its not simple type such as String,Date, etc type.

例如,如果元素是带有 List Custom 类型的元素,则不要使用/text()

For example if the element is of Custom type with List then do not use /text()

@XmlPath("extension/elements/element")
List<myType> elements;

这将添加扩展名->元素->元素,然后是内容.

This will add extension -> elements -> element then content.

<extension>
<elements>
<element></element>
<element></element>
......
</elements>
</extension>

如果元素为 String 类型,则必须使用 text()

If the elements are of String type then you have to use text()

@XmlPath("extension/elements/text()")
List<String> elements;

这篇关于如何在JAXB编组期间向类添加包装器元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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