Details
-
Type:
New Feature
-
Status:
Closed
-
Priority:
Minor
-
Resolution: Fixed
-
Affects Version/s: 1.2.1
-
Fix Version/s: 1.4
-
Component/s: None
-
Labels:None
-
JDK version and platform:JDK 1.5_08 for Windows XP
Description
I found a thread about "convert element with string body and attributes" in user mail list(http://article.gmane.org/gmane.comp.java.xstream.user/2349), but it seems that no one fire an issue here.
I have a class like this:
class Dely {
public String code;
public String content;
}
I use it like this:
public static void main(String ...strings) {
XStream xstream = new XStream();
xstream.alias("dely", Dely.class);
xstream.useAttributeFor("code", String.class);
//xstream.setDefaultContent("content", Dely.class);
Dely d = new Dely();
d.code = "abc";
d.content = "Hello";
System.out.println(xstream.toXML(d));
}
I would like the output xml as following:
<dely code="abc">Hello</dely>
Issue Links
Activity
Jörg Schaible
made changes -
Jörg Schaible
made changes -
Jörg Schaible
made changes -
| Priority | Minor [ 4 ] | |
| Assignee | Joerg Schaible [ joehni ] | |
| Fix Version/s | 1.x Maintenance [ 12873 ] | |
| Affects Version/s | 1.2.1 [ 12872 ] | |
| Fix Version/s | 1.2.1 [ 12872 ] |
Jörg Schaible
made changes -
| Status | Open [ 1 ] | Resolved [ 5 ] |
| Resolution | Fixed [ 1 ] |
Jörg Schaible
made changes -
| Fix Version/s | 1.4 [ 17519 ] | |
| Fix Version/s | 1.x Maintenance [ 12873 ] |
Jörg Schaible
made changes -
| Status | Resolved [ 5 ] | Closed [ 6 ] |
You can always create your own custom converter:
public class DelyConverter implements Converter {
public void marshal(Object source, HierarchicalStreamWriter writer,
{ Dely d = (Dely) source; writer.addAttribute("code", d.code); writer.setValue(d.content); }MarshallingContext context)
public Object unmarshal(HierarchicalStreamReader reader,
{ Dely d = new Dely(); d.code = reader.getAttribute("code"); d.content = reader.getValue(); return d; }UnmarshallingContext context)
public boolean canConvert(Class type)
{ return type.equals(Dely.class); }}