我希望從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];
}