0

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

我怎样才能检索国家名称。提前致谢。

4

3 回答 3

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];

}
于 2013-02-16T07:09:57.163 回答
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;
}
于 2013-02-16T07:19:39.023 回答
-1

直接在-(void)connectionDidFinishLoading:(NSURLConnection *)connectionMethod中试试

代码 ::

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];

它会帮助你。

谢谢。

于 2013-02-16T07:15:35.263 回答