XStream parse attributes and values at the same time
Posted
by gurbieta
on Stack Overflow
See other posts from Stack Overflow
or by gurbieta
Published on 2010-03-28T17:29:37Z
Indexed on
2010/03/28
17:33 UTC
Read the original article
Hit count: 584
Hi, I have the following XML
<search ver="3.0">
<loc id="ARBA0009" type="1">Buenos Aires, Argentina</loc>
<loc id="BRXX1283" type="1">Buenos Aires, Brazil</loc>
<loc id="ARDF0127" type="1">Aeroparque Buenos Aires, Argentina</loc>
<loc id="MXJO0669" type="1">Concepcion De Buenos Aires, Mexico</loc>
<loc id="MXPA1785" type="1">San Nicolas De Buenos Aires, Mexico</loc>
<loc id="ARBA0005" type="1">Balcarce, Argentina</loc>
<loc id="ARBA0008" type="1">Bragado, Argentina</loc>
<loc id="ARBA0010" type="1">Campana, Argentina</loc>
<loc id="ARBA0016" type="1">Chascomus, Argentina</loc>
<loc id="ARBA0019" type="1">Chivilcoy, Argentina</loc>
</search>
And a City class
public class City {
private String id;
private Integer type;
private String name;
// getters & setters...
}
I tried the following aliases to parse the XML
xStream.alias("search", List.class);
xStream.alias("loc", City.class);
xStream.useAttributeFor("id", String.class);
xStream.useAttributeFor("type", Integer.class);
But I can't figure out how to set the value of the "loc" tag, if I try to transform the City object in XML I get
<search>
<loc id="ARBA0001" type="1">
<name>Buenos Aires</name>
</loc>
</search>
When I really need to get this
<search>
<loc id="ARBA0001" type="1">Buenos Aires</loc>
</search>
Then, if I try to parse the XML to a City object I get the field "name" with a null value.
Anybody knows how to set te correct aliases to do this? Thanks in advance.
© Stack Overflow or respective owner