看到上一篇文章后,很多人跟我反应说解析部分看不懂(哈哈,博主自淫),故这篇文章将贴出更多示例,以便来巩固SimpleXml的相关注解用法。
废话不多说,上马。
http://www.digi24.ro/rss/Stiri/Digi24/
<rss version="2.0">
<channel>
<item>
<title>Atenționare cod galben de furtuni pentru 16 județe</title>
<link>http://www.digi24.ro/Stiri/Digi24/Actualitate/Stiinta+si+Mediu/Vreme+rece+in+toata+tara</link>
<guid>http://www.digi24.ro/Stiri/Digi24/Actualitate/Stiinta+si+Mediu/Vreme+rece+in+toata+tara</guid>
<description> <![CDATA[După zile cu temperaturi de 30 de grade Celsius]]> </description>
<category>Stiinta si Mediu</category>
<pubDate>Wed, 20 Apr 2016 08:12:37 +0300</pubDate>
<enclosure url="http://www.digi24.ro/onedb/picture(width=200)/57170fbf682ccf45917e8587" length="1" type="image/jpeg"/>
</item>
……(略去一大片item)
</item>
</channel>
</rss>
步骤一
@Root(name = "rss", strict = false)
public class Digi24RSS {
@Element(name = "channel")
public Digi24Channel digi24Channel;
}
步骤二
@Root(name = "channel", strict = false)
public class Digi24Channel {
@ElementList(name = "item", inline = true)
public List<Digi24Items> digi24Items;
}
步骤三
@Root(name = "item", strict = false)
public class Digi24Items {
@Element(name = "title")
public String title;
@Element(name = "enclosure", required = true)
public Digi24Enclosure imageUrl;
@Element(name = "pubDate")
public String publicationDate;
}
步骤四
@Root(name = "enclosure", strict = false)
public class Digi24Enclosure {
@Attribute(name = "url")
public String imageUrl;
}
打完收工。
http://webservices.nextbus.com/service/publicXMLFeed?command=routeList&a=sf-muni
<?xml version="1.0" encoding="utf-8"?>
<body copyright="All data copyright San Francisco Muni 2016.">
<route tag="E" title="E-Embarcadero"/>
<route tag="F" title="F-MarketWharves"/>
<route tag="J" title="J-Church"/>
<route tag="KT" title="KT-Ingleside/Third Street"/>
<route tag="L" title="L-Taraval"/>
<route tag="M" title="M-Ocean View"/>
……
<route tag="61" title="California Cable Car"/>
</body>
步骤一
@Root(name = "body", strict=false)
public class RouteList {
@ElementList(required=true, inline=true, entry="route")
public List<RouteWrapper> routes;
}
步骤二
@Root(name="route", strict=false)
public class RouteWrapper {
@Attribute(name="tag", required=true)
public String id;
@Attribute(name="title", required=true)
public String title;
public String toString() {
return this.id + " - " + this.title;
}
}
收工!
http://webservices.nextbus.com/service/publicXMLFeed?command=routeConfig&a=sf-muni
<?xml version="1.0" encoding="utf-8"?>
<body copyright="All data copyright San Francisco Muni 2016.">
<route tag="E" title="E-Embarcadero" color="667744" oppositeColor="ffffff" latMin="37.7528099" latMax="37.8085899" lonMin="-122.41737" lonMax="-122.38682">
<stop tag="5184" title="Jones St Beach St" lat="37.8072499" lon="-122.41737" stopId="15184"/>
<stop tag="3092" title="Beach S Mason St" lat="37.80741" lon="-122.4141199" stopId="13092"/>
<stop tag="3095" title="Beach St Stockton St" lat="37.8078399" lon="-122.41081" stopId="13095"/>
<stop tag="37440" title="Metro East" lat="37.7528099" lon="-122.38682" stopId="137440"/>
<direction tag="E____O_F00" title="Outbound to King 4th" name="Outbound" useForUI="true">
<stop tag="5184"/>
<stop tag="3092"/>
<stop tag="3095"/>
<stop tag="4502"/>
<stop tag="4529"/>
<stop tag="4516"/>
<stop tag="4518"/>
<stop tag="4504"/>
<stop tag="4534"/>
<stop tag="7283"/>
<stop tag="7794"/>
<stop tag="4506"/>
<stop tag="5234"/>
<stop tag="5239"/>
</direction>
<direction tag="E____I_F00" title="Inbound to Fisherman's Wharf" name="Inbound" useForUI="true">
<stop tag="5240"/>
<stop tag="5237"/>
<stop tag="7145"/>
<stop tag="7795"/>
<stop tag="4513"/>
<stop tag="4532"/>
<stop tag="4503"/>
<stop tag="4517"/>
<stop tag="4515"/>
<stop tag="7281"/>
<stop tag="4501"/>
<stop tag="4530"/>
<stop tag="5174"/>
<stop tag="5175"/>
<stop tag="35184"/>
</direction>
</route>
</body>
步骤一
@Root(name="body", strict=false)
public class RouteDetails {
@Attribute(name="tag")
@Path("route")
public String id;
@Attribute(name="title")
@Path("route")
public String title;
@Attribute(name="color")
@Path("route")
public String color;
@Attribute(name="oppositeColor")
@Path("route")
public String oppositeColor;
@ElementList(required=true, inline=true, entry="stop")
@Path("route")
public List<RouteStop> routeStops;
@ElementList(required=true, inline=true, entry="direction")
@Path("route")
public List<RouteDirection> routeDirections;
}
步骤二
@Root(name="stop", strict=false)
public class RouteStop {
@Attribute(name="tag")
public String id;
@Attribute(name="title")
public String title;
@Attribute(name="lat")
public String lat;
@Attribute(name="lon")
public String lon;
}
步骤三
@Root(name="direction", strict=false)
public class RouteDirection {
@Attribute(name="tag")
public String directionTag;
@Attribute(name="title")
public String title;
@ElementList(required=true, inline=true, entry="stop")
public List<StopListItem> stops;
@Attribute(name="name")
public String directionBound;
public ArrayList<String> getStopList() {
ArrayList<String> stopList = new ArrayList<>();
for(StopListItem stop : stops) {
stopList.add(stop.id);
}
return stopList;
}
}
散花完结!