Reputation: 473
I have a string of XML that looks like this:
<e1 atr1="3" atr2="asdf">
<e1b atr3="3" atr4="asdf">
<e1c atr5="3" atr6="asdf"/>
TestValue1
</e1b>
<e1b atr3="3" atr4="asdf">
<e1c atr5="3" atr6="asdf"/>
TestValue2
</e1b>
</e1>
It is different than other XML I have parsed in the past because the e1b elements have values TestValue1
and TestValue2
as well as child elements (e1c
).
If an element has both attributes and a value, you have to create a custom converter for xstream to be able to parse it. My attempt at that is below, but because the e1b element has attributes, child elements, AND a value, I'm not sure how to handle it. In my converter, I have left off all references to the e1c
child element. What do I need to add to the converter to allow it to handle the e1c
element correctly? Right now, e1c
values are not getting populated when I do a xstream.fromXML()
.
public class e1Converter implements Converter {
@SuppressWarnings("rawtypes")
public boolean canConvert(Class clazz) {
return e1b.class == clazz;
}
public void marshal(Object object, HierarchicalStreamWriter hsw,
MarshallingContext mc) {
e1b e = (e1b) object;
hsw.addAttribute("atr3", e.getAtr3());
hsw.addAttribute("atr4", e.getAtr4());
hsw.setValue(e.getE1bValue());
}
public Object unmarshal(HierarchicalStreamReader hsr,
UnmarshallingContext uc) {
e1b e = new e1b();
e.setAtr3(hsr.getAttribute("atr3"));
e.setAtr4(hsr.getAttribute("atr4"));
e.setE1bValue(hsr.getValue());
return e;
}
}
Upvotes: 1
Views: 1498
Reputation: 473
According to Jörg on the xstream mailing list:
Actually you cannot. XStream cannot read mixed-mode XML i.e. XML where text and child elements are mixed at the same level. The readers will simply act in undefined behavior. This kind of XML does simply not fit into the hierarchical stream model of XStream. What's the value of parent here:
<parent> what <child/>is <child/> the <child/>value <child/>now? </parent
Sorry, Jörg
Upvotes: 1