我的xml是这样的:
<Schedule id="1296" date="2010-05-20">
−
<Sports>
<Sport id="1" name="Football" abbr="FB"/>
<Sport id="2" name="Basketball" abbr="BK"/>
<Sport id="3" name="Baseball" abbr="BB"/>
<Sport id="4" name="Hockey" abbr="HK"/>
<Sport id="5" name="Other" abbr="OT"/>
</Sports>
−
<Leagues>
<League id="1" league_id="1" sport_id="1" periods="4" half_time_minutes="12" name="NFL" abbr="NFL" covers_league="NFL" covers_sport="football"/>
<League id="2" league_id="2" sport_id="1" periods="4" half_time_minutes="18" name="College Football" abbr="CFB" covers_league="NCAAF" covers_sport="football"/>
<League id="3" league_id="3" sport_id="2" periods="4" half_time_minutes="15" name="NBA" abbr="NBA" covers_league="NBA" covers_sport="basketball"/>
<League id="4" league_id="4" sport_id="2" periods="2" half_time_minutes="15" name="College Basketball" abbr="CBK" covers_league="NCAAB" covers_sport="basketball"/>
<League id="5" league_id="5" sport_id="3" periods="9" half_time_minutes="0" name="MLB" abbr="MLB" covers_league="MLB" covers_sport="baseball" ml="true"/>
<League id="6" league_id="6" sport_id="3" periods="9" half_time_minutes="0" name="College Baseball" abbr="CBB" ml="true"/>
<League id="7" league_id="7" sport_id="4" periods="3" half_time_minutes="0" name="NHL" abbr="NHL" covers_league="NHL" covers_sport="hockey" ml="true"/>
<League id="8" league_id="8" sport_id="2" periods="4" half_time_minutes="15" name="WNBA" abbr="WNBA" covers_league="WNBA" covers_sport="basketball"/>
</Leagues>
如何解析此xml中的值?
答案 0 :(得分:3)
要获取属性,首先必须告诉编译器要从哪个元素获取属性。所以我会使用if语句。让我们说我们想要获得“名称”属性值。它在Sport元素中。所以我们会写。
if([elementname isEqualToString:@"Sport"]){
nameString = [attributeDict objectForKey:@"name"];
}
它会查找Sport元素,然后将名为name的elements元素的值存储到我们名为nameString的NSString中。
答案 1 :(得分:1)
您必须使用NSXMLParser。
答案 2 :(得分:0)
使用NSXMLParser
。请尝试按照本教程进行操作: