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

それはあなたを助けます。

ありがとう。

于 2013-02-16T07:15:35.263 に答える