使用 JAXB XMLAnyElement 类型的样式返回动态元素名称 [英] Use JAXB XMLAnyElement type of style to return dynamic element names

查看:65
本文介绍了使用 JAXB XMLAnyElement 类型的样式返回动态元素名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这些论坛以及其他博客文章中阅读了很多答案,但我似乎无法将这些部分联系在一起.

I have read a lot of answers in these forums, as well as other blog posts, but I can't quite seem to connect the pieces together.

所以,我们从一个包含 Map 属性的基本 POJO 开始.已经很好地确定了如何包装它,但这会返回一些值.我要做的是取然后名称(又名标签)并使其成为有效的 XML属性".所以我们会得到一些价值.

So, we start with a basic POJO containing a Map properties. It's well established how to wrap this, but that returns some value. What I'm looking to do is take then name (a.k.a. label) and make it an valid XML 'attribute'. So we would get some value.

我找到了一个例子(如果我能再次找到它会链接)如下:

I found one example (will link if I can find it again) as follows:

@XmlAnyElement
public List<JAXBElement<String>> getXmlProperties() {
   List<JAXBElement<String>> elements = new ArrayList<JAXBElement<String>>();
   for (Map.Entry<String, String> property: properties.entrySet()) 
      elements.add(new JAXBElement<String>(new QName(property.getKey()), 
      String.class, property.getValue()));
      return elements;
}

这工作得很好,但我在我的 Bean/Pojo 类中有这个,它与 GWT 前端共享,因此不能包含对 JAXBElement 和 QName 的引用(需要源代码).

This worked perfectly, but I had this in my Bean/Pojo class, which is shared with a GWT front-end, thus cannot contain references to JAXBElement and QName (source code required).

那么,有没有办法使用 XmlAdapter 和 JAXBElement/QName/XmlAnyElement 梦之队之类的东西来获得类似的结果?顺便说一句,如果考虑到这些因素,我正在使用 RESTEasy.

So, is there a way to get a similar result using something like the XmlAdapter, and the JAXBElement/QName/XmlAnyElement dream team? By the way, I'm using RESTEasy if that factors in at all.

这是带有@XmlAnyElement+JAXBElement 的论坛帖子:JAXB 的动态标记名称

Here is the forum post with the @XmlAnyElement+JAXBElement: Dynamic tag names with JAXB

推荐答案

我离答案不远了 - 经过更多的尝试,我找到了正确的组合.

I wasn't so far from the answer - after a bit more of experimenting I found the right combo.

为不可映射的返回类型创建一个包装类.包装器应包含/返回 List 使用 @XmlAnyElement 注释包装器返回类型.

Create a wrapper class for the un-mapable return type. Wrapper should contain/return List<JAXBElement<String> Annotate wrapper return type with @XmlAnyElement.

public class MapWrapper {
   @XmlAnyElement
    public List<JAXBElement<String>> properties = new ArrayList<JAXBElement<String>>();
}

创建一个编组到 MapWrapper 的 XmlAdapter

Create an XmlAdapter that marshals to the MapWrapper

public class MapAdapter extends XmlAdapter<MapWrapper, Map<String,String>> {
    @Override
    public MapWrapper marshal(Map<String,String> m) throws Exception {
    MapWrapper wrapper = new MapWrapper();
    List<JAXBElement<String>> elements = new ArrayList<JAXBElement<String>>();
       for (Map.Entry<String, String> property: m.entrySet()) {
          elements.add(new JAXBElement<String>(
                    new QName(getCleanLabel(property.getKey())), 
          String.class, property.getValue()));
       }
       wrapper.elements=elements;
    return wrapper;
}

@Override
public Map<String,String> unmarshal(MapWrapper v) throws Exception {
            // TODO
    throw new OperationNotSupportedException();
}

// Return a lower-camel XML-safe attribute
private String getCleanLabel(String attributeLabel) {
    attributeLabel = attributeLabel.replaceAll("[()]", "")
            .replaceAll("[^\\w\\s]", "_").replaceAll(" ", "_")
            .toUpperCase();
    attributeLabel = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL,
            attributeLabel);
    return attributeLabel;
}
}

使用 XmlAdapter 注释不可映射的类型

Annotate your unmappable type with the XmlAdapter

@XmlRootElement
public class SomeBean {

    @XmlJavaTypeAdapter(MapAdapter.class)
    public LinkedHashMap<String, String> getProperties() {
        return properties;
    }
}

地图如下:

My Property 1    My Value 1 
My Property 2    My Value 2

应该是:

<someBean>
   <properties>
     <myProperty1>My Value 1</myProperty1>
     <myProperty2>My Value 1</myProperty2>
   </properties>
</someBean>

希望这对其他人有帮助!

Hope this helps someone else!

这篇关于使用 JAXB XMLAnyElement 类型的样式返回动态元素名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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