2 developerwab developerWab 于 2013.02.17 14:46 提问

从XMLParser中获取国家名字

我希望从xml.中获取国家名字:

xml结果:

<Table diffgr:id="Table1" msdata:rowOrder="0"><id>1</id><country>Afghanistan</country><isd_code>93</isd_code><timezone>+04:30</timezone><visible>false</visible></Table><Table diffgr:id="Table2" msdata:rowOrder="1"><id>2</id><country>Albania</country><isd_code>355</isd_code><timezone>+01:00</timezone><visible>false</visible></Table>

在解析器结束方法:

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{

    if ( [elementName isEqualToString:@"Table"] ) {
       NSLog(@"attribute is %@",attributeDict);
    }
}

输出:

attribute is {
    "diffgr:id" = Table1;
    "msdata:rowOrder" = 0;
}

怎么获取?谢谢

1个回答

Mr_me
Mr_me   2013.02.17 15:54
已采纳

把下面这段代码放在viewDidLoad:

- countryarr=[[NSMutableArray alloc]init];
nodeContent=[[NSMutableString alloc]init];


 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[nodeContent appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];

} 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName

{
     if ([elementName isEqualToString:@"country"])
{
    [countryarr addObject:nodeContent];
}
 nodeContent=[[NSMutableString alloc]init];

}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!