JAXB 可以将 ArrayList 输出为逗号分隔值吗?

发布于 2024-09-27 04:43:31 字数 492 浏览 6 评论 0原文

我有类似的东西

@XmlElementWrapper(name="Mylist")
List<Items> myItems = new ArrayList<Items>()

,结果是这样的,

<Mylist>
   <myItems>item 1</myItems>
   <myItems>item 2</myItems>
   <myItems>item 3</myItems>
</Mylist>

是否有可能使它看起来更像是

<Mylist>
   <myItems>item 1, item 2, item 3</myItems>
</Mylist>

因为我所追求的数据无论如何都只是文本?

I have something like

@XmlElementWrapper(name="Mylist")
List<Items> myItems = new ArrayList<Items>()

and that comes out like

<Mylist>
   <myItems>item 1</myItems>
   <myItems>item 2</myItems>
   <myItems>item 3</myItems>
</Mylist>

Is it possible to make this come out more like

<Mylist>
   <myItems>item 1, item 2, item 3</myItems>
</Mylist>

Since the data I am after is all just textual anyway?

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

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

发布评论

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

评论(3

感情洁癖 2024-10-04 04:43:31

您可以使用 @XmlList使其成为空格分隔的值。

对于逗号分隔的列表,您需要使用 XmlAdapter。有关 XmlAdapter 请参阅:

You can use @XmlList to make it a space separated value.

For a comma separated list you will need to use an XmlAdapter. For more information on XmlAdapter see:

唯憾梦倾城 2024-10-04 04:43:31

这是一个用于处理逗号分隔列表的 XmlAdapter

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class CommaSeparatedListAdapter extends XmlAdapter<String, List<String>> {

    @Override
    public List<String> unmarshal(final String string) {
        final List<String> strings = new ArrayList<String>();

        for (final String s : string.split(",")) {
            final String trimmed = s.trim();

            if (trimmed.length() > 0) {
                strings.add(trimmed);
            }
        }

        return strings;
    }

    @Override
    public String marshal(final List<String> strings) {
        final StringBuilder sb = new StringBuilder();

        for (final String string : strings) {
            if (sb.length() > 0) {
                sb.append(", ");
            }

            sb.append(string);
        }

        return sb.toString();
    }
}

您可以像这样使用它:

@XmlElementWrapper(name="Mylist")
@XmlJavaTypeAdapter(CommaSeparatedListAdapter.class)
List<Items> myItems = new ArrayList<Items>()

Here's an XmlAdapter to handle comma separated lists:

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class CommaSeparatedListAdapter extends XmlAdapter<String, List<String>> {

    @Override
    public List<String> unmarshal(final String string) {
        final List<String> strings = new ArrayList<String>();

        for (final String s : string.split(",")) {
            final String trimmed = s.trim();

            if (trimmed.length() > 0) {
                strings.add(trimmed);
            }
        }

        return strings;
    }

    @Override
    public String marshal(final List<String> strings) {
        final StringBuilder sb = new StringBuilder();

        for (final String string : strings) {
            if (sb.length() > 0) {
                sb.append(", ");
            }

            sb.append(string);
        }

        return sb.toString();
    }
}

You would use it like this:

@XmlElementWrapper(name="Mylist")
@XmlJavaTypeAdapter(CommaSeparatedListAdapter.class)
List<Items> myItems = new ArrayList<Items>()
岁吢 2024-10-04 04:43:31

您可以通过使字段为瞬态 @XmlTransient 并创建一个用于计算以逗号分隔的字符串的方法来实现此目的。

@XmlTransient
List<Items> myItems = new ArrayList<Items>()

@XmlElement(name = "myItems")
public String getItemsAsCommasSeparated() {
    return String.join(", ", myItems);
}

关于包装 如果确实有必要,您必须将列表包装到另一个对象中。

you can accomplish that by making the field transient @XmlTransient and creating a method for calculating a String with a comma-separated.

@XmlTransient
List<Items> myItems = new ArrayList<Items>()

@XmlElement(name = "myItems")
public String getItemsAsCommasSeparated() {
    return String.join(", ", myItems);
}

About the wrapping <Mylist> if it is really necessary you will have to wrap the list into a another object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文