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?
既然我追求的数据都是文本的呢?
2 个解决方案
#1
5
You can use @XmlList to make it a space separated value.
您可以使用@XmlList使其成为空格分隔值。
For a comma separated list you will need to use an XmlAdapter. For more information on XmlAdapter see:
对于以逗号分隔的列表,您需要使用XmlAdapter。有关XmlAdapter的更多信息,请参阅:
#2
2
Here's an XmlAdapter
to handle comma separated lists:
这是一个处理逗号分隔列表的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();
}
}
You would use it like this:
你会像这样使用它:
@XmlElementWrapper(name="Mylist")
@XmlJavaTypeAdapter(CommaSeparatedListAdapter.class)
List<Items> myItems = new ArrayList<Items>()
#1
5
You can use @XmlList to make it a space separated value.
您可以使用@XmlList使其成为空格分隔值。
For a comma separated list you will need to use an XmlAdapter. For more information on XmlAdapter see:
对于以逗号分隔的列表,您需要使用XmlAdapter。有关XmlAdapter的更多信息,请参阅:
#2
2
Here's an XmlAdapter
to handle comma separated lists:
这是一个处理逗号分隔列表的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();
}
}
You would use it like this:
你会像这样使用它:
@XmlElementWrapper(name="Mylist")
@XmlJavaTypeAdapter(CommaSeparatedListAdapter.class)
List<Items> myItems = new ArrayList<Items>()