2010-03-28 49 views
6

我有以下XMLXStream的同時解析的屬性和值

<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> 

而一個城市類

public class City { 

    private String id; 
    private Integer type; 
    private String name; 

    // getters & setters... 
} 

我嘗試了以下的別名來解析XML

xStream.alias("search", List.class); 
xStream.alias("loc", City.class); 
xStream.useAttributeFor("id", String.class); 
xStream.useAttributeFor("type", Integer.class); 

但我不知道如何設置「loc」標記的值,如果我嘗試在XML中轉換City對象,我得到

<search> 
    <loc id="ARBA0001" type="1"> 
     <name>Buenos Aires</name> 
    </loc> 
</search> 

當我真的需要得到這個

<search> 
    <loc id="ARBA0001" type="1">Buenos Aires</loc> 
</search> 

然後,如果我嘗試將XML解析到一個城市對象,我得到了場「名」與空值。

任何人都知道如何設置正確的別名來做到這一點?提前致謝。

回答

8

我終於找到了解決辦法,一個轉換器解決了這一點,這裏是代碼

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); 
    } 

} 

而且在設置我還設置了CityConverter

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

別名的一部分,所有工作正常:)

1

XStream似乎有點複雜,你可以在JAXB中做以下事情:

public class City { 

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

    // getters & setters... 
} 
+2

看看我的博客文章比較JAXB和XStream:http://bdoughan.blogspot.com/2010/10/how-does-jaxb-compare-to-xstream.html – 2010-10-07 18:48:35

4

我希望在此公佈,這可能幫助別人,因爲我花了相當長的一段時間來找到它...... http://fahdshariff.blogspot.com/2011/12/using-xstream-to-map-single-element.html

的答案是使用@XStreamConverter - ToAttributedValueConverter

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

    String message; 

    ... 

有很多有趣的轉換器,提供各種有用的功能... http://x-stream.github.io/converters.html

+0

更簡單的解決方案,它應該是最有利的 – maxivis 2014-06-27 13:20:34