XStream parse attributes and values at the same time
Asked Answered
C

4

8

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.

Corvine answered 28/3, 2010 at 17:29 Comment(0)
C
9

I finally found the solution, a Converter solves this, here is the code

public class CityConverter implements Converter {

    public void marshal(Object value, HierarchicalStreamWriter writer, 
                                                               MarshallingContext context) {
        City city = (City) value;
        writer.addAttribute("id", city.getId());
        writer.addAttribute("type", city.getType().toString());
        writer.setValue(city.getName());
    }

    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
        City city = new City();
        city.setName(reader.getValue());
        city.setId(reader.getAttribute("id"));
        city.setType(reader.getAttribute("type"));
        return city;
    }

    public boolean canConvert(Class clazz) {
        return clazz.equals(City.class);
    }

}

And in the part of setting the aliases I also set up the CityConverter

xStream.registerConverter(new CityConverter());
xStream.alias("search", List.class);
xStream.alias("loc", City.class);

And all works fine :)

Corvine answered 28/3, 2010 at 18:25 Comment(0)
N
7

I am posting this in hopes that it might help others because it took me quite some time to find it... http://fahdshariff.blogspot.com/2011/12/using-xstream-to-map-single-element.html

The answer is to use @XStreamConverter - ToAttributedValueConverter

@XStreamAlias("error")
@XStreamConverter(value=ToAttributedValueConverter.class, strings={"message"})
public class Error {

  String message;

  ...

There are lots of interesting converters that provide all sorts of useful functionalities... http://x-stream.github.io/converters.html

Nichellenichol answered 9/3, 2012 at 14:24 Comment(0)
T
0

XStream seems kind of complicated, you could do the following in JAXB:

public class City { 

    @XmlAttribute private String  id; 
    @XmlAttribute private Integer type; 
    @XmlValue private String  name; 

    // getters & setters... 
}
Tymothy answered 21/7, 2010 at 18:24 Comment(1)
Check out my blog post comparing JAXB & XStream: bdoughan.blogspot.com/2010/10/…Tymothy
O
0
@XStreamConverter(value= ToAttributedValueConverter.class, strings={"name"})
public class City { 

    @XStreamAsAttribute @XStreamAlias("id") private String  id; 
    @XStreamAsAttribute @XStreamAlias("type") private Integer type; 
    private String  name; 

    // getters & setters... 
}
Oblation answered 8/3, 2021 at 10:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.