2013-02-16 43 views
0

我想從xml檢索國名。這是xml結果。如何從xmlParser檢索國名

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

我怎樣才能檢索到的國家名稱。 在此先感謝。

+0

http://stackoverflow.com/questions/1316413/how-do-i-parse-a-basic-xml-file-with-nsxmlparser描述得到的方式來自標記的值。 – 2013-02-16 07:13:10

回答

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

試試這個直接-(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]; 

它會幫你的。

謝謝。

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