History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: XSTR-344
Type: New Feature New Feature
Status: Open Open
Assignee: Unassigned
Reporter: jian liao
Votes: 1
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
XStream

Allow convert element with string body and attributes

Created: 18/Sep/06 12:47 AM   Updated: 07/Jul/08 01:44 PM
Component/s: None
Affects Version/s: None
Fix Version/s: 1.2.1

File Attachments: 1. Java Source File DefaultContentTest.java (0.6 kb)

Issue Links:
Duplicate
 
Related
 

JDK version and platform: JDK 1.5_08 for Windows XP


 Description  « Hide
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>



 All   Comments   Change History      Sort Order: Ascending order - Click to sort in descending order
Guilherme Silveira - 24/Oct/06 07:52 AM
You can always create your own custom converter:

public class DelyConverter implements Converter {

public void marshal(Object source, HierarchicalStreamWriter writer,
MarshallingContext context) { Dely d = (Dely) source; writer.addAttribute("code", d.code); writer.setValue(d.content); }

public Object unmarshal(HierarchicalStreamReader reader,
UnmarshallingContext context) { Dely d = new Dely(); d.code = reader.getAttribute("code"); d.content = reader.getValue(); return d; }

public boolean canConvert(Class type) { return type.equals(Dely.class); }

}