我有以下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解析到一個城市對象,我得到了場「名」與空值。
任何人都知道如何設置正確的別名來做到這一點?提前致謝。
看看我的博客文章比較JAXB和XStream:http://bdoughan.blogspot.com/2010/10/how-does-jaxb-compare-to-xstream.html – 2010-10-07 18:48:35