问题描述
- 从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; }
怎么获取?谢谢
解决方案
把下面这段代码放在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];
}
时间: 2024-11-13 03:36:09