我正在为Android开发一款应用。我的应用程序解析XML远程文件,并将第一个标记的数据存储在地图的ArraList中,将第二个标记的数据存储在另一个MapsList中。 我将在这里发布我的XML:
<?xml version="1.0" encoding="UTF-8"?>
<Programs>
<Program programNumber="1" imgURL="http://www.photovideolife.com/userfiles/Placeholder%2001.jpg" description="Lorem ipsum dolor sit er elit">
<Episode pN="1" episodeNumber="1" transmissionName="Titolo" date="29 Giu 2013" time1="14:30" time2="" channel="IRIS" channelLogo="http://indiscrezioni.files.wordpress.com/2010/06/logo_iris.jpg">
</Episode>
<Episode pN="1" episodeNumber="1" transmissionName="Titolo" date="29 Giu 2013" time1="" time2="16:30" channel="La7" channelLogo="http://www.tabaccheriavenza.it/media/logo/la7.jpg">
</Episode>
<Episode pN="1" episodeNumber="2" transmissionName="Titolo" date="01 Lug 2013" time1="14:30" time2="" channel="IRIS" channelLogo="http://indiscrezioni.files.wordpress.com/2010/06/logo_iris.jpg">
</Episode>
<Episode pN="1" episodeNumber="2" transmissionName="Titolo" date="01 Lug 2013" time1="" time2="16:30" channel="la7" channelLogo="http://www.tabaccheriavenza.it/media/logo/la7.jpg">
</Episode>
</Program>
<Program programNumber="2" imgURL="http://mesa.umich.edu/files/mesa/field/image/placeholder2.png" description="Lorem ipsum dolor sit er elit">
<Episode pN="2" episodeNumber="1" transmissionName="Titolo 1" date="30 Giu 2013" time1="13:30" time2="" channel="Rai 1" channelLogo="http://i822.photobucket.com/albums/zz145/Mattelufregn/Loghi%20canali%20tv/70px-Logo_Rai_1_2010svg50x50.png">
</Episode>
<Episode pN="2" episodeNumber="1" transmissionName="Titolo 1" date="30 Giu 2013" time1="" time2="18:30" channel="Rai 5" channelLogo="http://www.tuttotv.info/wp-content/uploads/2011/04/logo_rai5_50.jpg">
</Episode>
<Episode pN="2" episodeNumber="2" transmissionName="Titolo 1" date="01 Lug 2013" time1="13:30" time2="" channel="Rai 1" channelLogo="http://i822.photobucket.com/albums/zz145/Mattelufregn/Loghi%20canali%20tv/70px-Logo_Rai_1_2010svg50x50.png">
</Episode>
<Episode pN="2" episodeNumber="2" transmissionName="Titolo 1" date="01 Lug 2013" time1="" time2="18:30" channel="Rai 5" channelLogo="http://www.tuttotv.info/wp-content/uploads/2011/04/logo_rai5_50.jpg">
</Episode>
</Program>
<Program programNumber="3" imgURL="http://wp.contempographicdesign.com/wp_paramount/wp-content/themes/paramount/images/image_placeholder_lrg.jpg" description="Lorem ipsum dolor sit er elit">
<Episode pN="3" episodeNumber="1" transmissionName="Titolo 2" date="30 Giu 2013" time1="10:30" time2="" channel="Canale 5" channelLogo="http://www.mozaic.qa/logo/canale5.jpg">
</Episode>
<Episode pN="3" episodeNumber="1" transmissionName="Titolo 2" date="30 Giu 2013" time1="" time2="17:30" channel="Italia 1" channelLogo="http://upload.wikimedia.org/wikipedia/it/thumb/3/30/Logo_Italia_1.svg/50px-Logo_Italia_1.svg.png">
</Episode>
<Episode pN="3" episodeNumber="2" transmissionName="Titolo 2" date="01 Lug 2013" time1="10:30" time2="" channel="Canale 5" channelLogo="http://www.mozaic.qa/logo/canale5.jpg">
</Episode>
<Episode pN="3" episodeNumber="2" transmissionName="Titolo 2" date="01 Lug 2013" time1="" time2="17:30" channel="Italia 1" channelLogo="http://upload.wikimedia.org/wikipedia/it/thumb/3/30/Logo_Italia_1.svg/50px-Logo_Italia_1.svg.png">
</Episode>
</Program>
</Programs>
在解析结束时,我有一个用于程序的ArrayList和一个用于Episode的ArrayList。您可以看到程序和剧集有一个公共字段(programNumber和pN),现在我需要将存储在Episode的ArrayList中的数据用于正确的程序。 我怎么能这样做? 我在网上找到了这个解决方案:
for (HashMap<String, String> programsMap : programs) {
for (Map.Entry<String, String> entry : programsMap.entrySet()) {
// I added some code here
}
}
我如何解决这个问题? 谢谢你的建议。
答案 0 :(得分:2)
String programNumber = "3";
// create the result list (empty initially)
List<Map<String, String>> episodesForProgramNumber = new ArrayList<>();
// loop through all the episodes
for (Map<String, String> episode : allEpisodes) {
// if the current episode has the given program number, add it to the result
if (programNumber.equals(episode.get("pN")) {
episodesForProgramNumber.add(episode);
}
}
但Java是一种OO语言。您不应使用地图来存储数据。您应该使用Program
和Episode
类型的对象。 Program
包含List<Episode>
。
答案 1 :(得分:1)
你在想关系。在SQL数据库中,您可以执行类似于您的操作。通过密钥将剧集映射到节目。
我会更多地面向对象。创建一个Program
和一个Episode
类,其属性与XML结构中的元素相同。除此之外,您的Program
课程应该有一个剧集列表(例如:List<Episode> episodes
),您可以在其中添加属于该计划的所有剧集。
答案 2 :(得分:0)
你需要使用对象,但我想JB Nizet的解决方案是可行的。下次使用对象。