如何从xmlParser检索国家/地区名称

时间:2013-02-16 07:00:05

标签: iphone ios objective-c nsxmlparser

我正在尝试从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);
    }
}

OutPut

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

如何检索国家/地区名称。 提前谢谢。

3 个答案:

答案 0 :(得分: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];

}

答案 1 :(得分:0)

试一试

NSString *elementname;

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

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{

   if([elementname isEqualToString:@"country"])
   {
       NSLog(@"country name is : %@",string);
   }
}

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

答案 2 :(得分:-1)

直接在-(void)connectionDidFinishLoading:(NSURLConnection *)connection方法

中尝试此操作

代码::

NSString *theXML = [[[NSString alloc] initWithBytes: [responseData mutableBytes] length:[responseData length] encoding:NSUTF8StringEncoding] autorelease];

theXML = [theXML stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
theXML = [theXML stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
theXML = [theXML stringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"];

NSLog(@" String :: %@", theXML);

[arrCountry removeAllObjects];

NSArray *arr = [theXML componentsSeparatedByString:@"<Your Main Parent>"];

NSLog(@"Count :: %d", [arr count]);

for(int i = 1; i < [arr count]; i++)
{
   NSString *str = [arr objectAtIndex:i];

   NSArray *arr1 = [str componentsSeparatedByString:@"<Table>"];
   NSString *data = [arr1 objectAtIndex:1];
   NSRange ranfrom = [data rangeOfString:@"</Table>"];

   for(int i = 1; i < [arr1 count]; i++)
   {
      NSString *str1 = [arr1 objectAtIndex:i];

      NSArray *arr12 = [str1 componentsSeparatedByString:@"<country>"];
      NSString *data1 = [arr12 objectAtIndex:1];
      NSRange ranfrom1 = [data1 rangeOfString:@"</country>"];

      [arrCountry addObject:[data1 substringToIndex:ranfrom1.location]];
   }
}

[connection release];

它会帮助你。

感谢。