如何更改中的默认 Hashmap 标签名称使用 JAXB 编组 XML 时是否会发生其他事情?

发布于 2025-01-16 01:26:27 字数 1862 浏览 2 评论 0原文

我正在尝试使用 JAXB 将编组 XML 中的标记值更改为 .调用我的应用程序的客户正在期待,以前我使用 XSLT 来执行此转换,但我正在切换到 JAXB 方法来创建 XML 响应。

现在,这是我正在创建的错误 XML:

<error>
    <details>
        <entry>
            <key>code</key>
            <value>1234</value>
        </entry>
        <entry>
            <key>customKey</key>
            <value>customerValue</value>
        </entry>
    </details>
</error>

我需要它看起来像这样:

<error>
    <details>
        <detail>
            <key>code</key>
            <value>1234</value>
        </detail>
        <detail>
            <key>customKey</key>
            <value>customerValue</value>
        </detail>
    </details>
</error>

我用来编组的 Java 对象如下:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Map;

@XmlRootElement(
        name = "error"
)
@XmlAccessorType(XmlAccessType.FIELD)
public class Error {
    
    private Map<String, String> details;

    public Error() {

    }

    public Error(Map<String, String> details) {
        this.details = details;
    }
    
    public Map<String, String> getDetails() {
        return details;
    }

    public void setDetails(Map<String, String> details) {
        this.details = details;
    }
}

在将 Map 编组到 XML 时,是否有一种方法可以更改默认命名约定使用 JAXB?我是否可以使用嵌套对象而不是哈希映射以另一种方式来实现此目的?

我正在尝试创建一个

列表,但我的 JAXB 在编组列表时遇到了麻烦。

谢谢

I am attempting to change the value of the tag in the marshalled XML using JAXB to . My clients that are calling my application are expecting , previously I was using XSLT to do this transformation but I am switching to a JAXB approach to create the XML response.

Right now this is the error XML that I am creating:

<error>
    <details>
        <entry>
            <key>code</key>
            <value>1234</value>
        </entry>
        <entry>
            <key>customKey</key>
            <value>customerValue</value>
        </entry>
    </details>
</error>

And I need it to look like this:

<error>
    <details>
        <detail>
            <key>code</key>
            <value>1234</value>
        </detail>
        <detail>
            <key>customKey</key>
            <value>customerValue</value>
        </detail>
    </details>
</error>

The Java object that I am using to marshall is as follows:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Map;

@XmlRootElement(
        name = "error"
)
@XmlAccessorType(XmlAccessType.FIELD)
public class Error {
    
    private Map<String, String> details;

    public Error() {

    }

    public Error(Map<String, String> details) {
        this.details = details;
    }
    
    public Map<String, String> getDetails() {
        return details;
    }

    public void setDetails(Map<String, String> details) {
        this.details = details;
    }
}

Is there a way to change the default naming convention when marshalling a Map to XML using JAXB? Is there possibly I can do this another way with nested objects instead of a hash map?

I was messing around attempting to do a List of <details> but my JAXB was having troubles marshalling a list.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

献世佛 2025-01-23 01:26:27

有没有办法改变默认的命名约定
使用 JAXB 将 Map 编组为 XML 时?

我认为没有办法改变这个命名,因为元素名称
在 JAXB 实现中进行硬编码。
在类 com.sun.xml.bind.v2.runtime.property.SingleMapNodeProperty 中
(它负责序列化Map条目)实际上有这样的代码:

this.entryTag = context.nameBuilder.createElementName("","entry");
this.keyTag = context.nameBuilder.createElementName("","key");
this.valueTag = context.nameBuilder.createElementName("","value");

我是否可以使用嵌套对象而不是哈希映射以另一种方式来实现此目的?

我正在尝试做一个

列表
但我的 JAXB 在整理列表时遇到了麻烦。

不知道你遇到了哪些困扰。
但是您只需将 Error 类从 更改为 即可实现您的目标

private Map<String, String> details;

@XmlElementWrapper(name = "details")
@XmlElement(name = "detail")
private List<Detail> details;

当然还要相应地调整构造函数/getter/setter)。
您还需要一个 Detail 类来表示内容
... 内。

@XmlAccessorType(XmlAccessType.FIELD)
public class Detail {

    @XmlElement
    private String key;

    @XmlElement
    private String value;
    
    // constructors, getters, setters (omitted here for brevity)
}

Is there a way to change the default naming convention
when marshalling a Map to XML using JAXB?

I think there is no way to change this naming, because the element names of
<entry>, <key> and <value> are hard-coded in the JAXB implementation.
In class com.sun.xml.bind.v2.runtime.property.SingleMapNodeProperty
(which is responsible for serializing a Map entry) there is actually this code:

this.entryTag = context.nameBuilder.createElementName("","entry");
this.keyTag = context.nameBuilder.createElementName("","key");
this.valueTag = context.nameBuilder.createElementName("","value");

Is there possibly I can do this another way with nested objects instead of a hash map?

I was messing around attempting to do a List of <details>
but my JAXB was having troubles marshalling a list.

I don't know which troubles you encountered.
But you can achieve your goal just by changing in your Error class from

private Map<String, String> details;

to

@XmlElementWrapper(name = "details")
@XmlElement(name = "detail")
private List<Detail> details;

(and of course also adapting the constructor/getter/setter accordingly).
You will also need a Detail class for representing the content
within <detail>...</detail>.

@XmlAccessorType(XmlAccessType.FIELD)
public class Detail {

    @XmlElement
    private String key;

    @XmlElement
    private String value;
    
    // constructors, getters, setters (omitted here for brevity)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文