0

#import "GDataXMLNode.h"最初に.hファイルをインポートしました。これが私のパーサーXMLを使用して解析する必要があるものです。GDataXML

<SiteStats>
   <visitor>1</visitor>
   <uniqueVisitor>1</uniqueVisitor>
   <orderCount>0</orderCount>
   <revenue>null</revenue>
   <conversionRate>0</conversionRate>
   <newProduct>3</newProduct>
   <outOfStockProduct>0</outOfStockProduct>
</SiteStats>

ここで注意すべきことの1つは、このxmlがWebからのものであるということです。そのためNSUrlConnection、デリゲートを使用してxmlデータを取得しました。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    responseString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
    NSLog(@"Response: %@",responseString);
    // other stuff here...
}

ここでを取得responseStringし、次のコードを使用して解析します。しかし、私はそれを解析することができません。

 xmlDocument = [[GDataXMLDocument alloc] initWithXMLString:responseString options:0 error:&errorOnStore];
if (nil == xmlDocument)    {
    NSLog(@"could not load xml file");
}
else    {
    NSLog(@"Loading desire xml");
    NSLog(@"%@", xmlDocument.rootElement);
    NSArray *getData = [[xmlDocument rootElement] elementsForName:@"SiteStats"];
    NSLog(@"%@",getData);
    records = [[NSMutableArray alloc] init];
    //[records retain];
    if(getData.count !=0 ){
        NSLog(@"data has");
    }
    //storing the car model in the mutable array
    for(GDataXMLElement *e in getData){
        NSLog(@"Enering in the xml file");
        [records addObject:e];

        NSString *Visitor = [[[e elementsForName:@"visitor"] objectAtIndex:0] stringValue];
        NSLog(@"Visitor : %@",Visitor);

        NSString *UVisitor = [[[e elementsForName:@"uniqueVisitor"] objectAtIndex:0] stringValue];
        NSLog(@"Unique Visitor : %@",UVisitor);
    }
}

私は私がするときにこの値を得ることができますNSLog(@"%@", xmlDocument.rootElement);

 GDataXMLElement 0x1a4290: {type:1 name:SiteStats xml:"<SiteStats><visitor>4</visitor><uniqueVisitor>3</uniqueVisitor><orderCount>0</orderCount><revenue>0</revenue><conversionRate>0</conversionRate><newProduct>3</newProduct><outOfStockProduct>0</outOfStockProduct></SiteStats>"}

しかし、私は配列NSLog(@"%@",getData);にデータを取得していません。getData

誰かが問題がどこにあるか教えてもらえますか?前もって感謝します。

4

2 に答える 2

1
xmlDocument = [[GDataXMLDocument alloc] initWithXMLString:responseString options:0 error:&errorOnStore];
if (nil == xmlDocument)    
{
    NSLog(@"could not load xml file");
}
else    
{
    NSLog(@"Loading desire xml");
    NSLog(@"%@", xmlDocument.rootElement);

    // skip this bits, you're trying to retrieve wrong element
    //NSArray *getData = [[xmlDocument rootElement] elementsForName:@"SiteStats"];
    //NSLog(@"%@",getData);
    records = [[NSMutableArray alloc] init];
    //[records retain];
    //if(getData.count !=0 )
    //{
        //NSLog(@"data has");
    //}
    //storing the car model in the mutable array
    //for(GDataXMLElement *e in getData)
    //{
        //NSLog(@"Enering in the xml file");
        //[records addObject:e];

        NSString *Visitor = [[[xmlDocument.rootElement elementsForName:@"visitor"] objectAtIndex:0] stringValue];
        NSLog(@"Visitor : %@",Visitor);

        NSString *UVisitor = [[[xmlDocument.rootElement elementsForName:@"uniqueVisitor"] objectAtIndex:0] stringValue];
        NSLog(@"Unique Visitor : %@",UVisitor);
    //}
}

編集: NSArray *getData = [[xmlDocument rootElement] elementsForName:@"SiteStats"];この行では、間違った要素を取得しようとしています。要素 "SiteStats" は既にルート要素であるため、そのコードを実行すると、"サイドスタッツ」

<SiteStats>
   <SiteStats>
       <visitor>1</visitor>
       <uniqueVisitor>1</uniqueVisitor>
       <orderCount>0</orderCount>
       <revenue>null</revenue>
       <conversionRate>0</conversionRate>
       <newProduct>3</newProduct>
       <outOfStockProduct>0</outOfStockProduct>
   </SiteStats>
</SiteStats>

XML が上記のような場合、元のコードは機能する可能性があります。

于 2012-01-25T03:59:31.243 に答える
0

解析を許可するには、タグの属性「 xmlns 」も変更する必要がある場合があります

<html> by noNSxml . これを行わないと、正しく解析されません。

于 2013-01-18T09:21:04.910 に答える