1

对于这个元素,我总是得到 null:<address>Rua Santo Ubaldo, 28 - São Paulo</address>

xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<music>
    <principal>
        <id>1</id>    
        <title>Woods Bar</title>
        <text>Sofisticação, essa é a palavra que resume o Wood´s São Paulo. Um ambiente para o paulistano exigente, que busca por diversão, gente bonita e requinte. O sertanejo para o público seleto, com excelência em serviços e uma arquitetura diferente de tudo o que você já viu. Venha se surpreender!</text>
        <thumb_url>http://www.semhora.com/xmlparser/principal/thumb.png</thumb_url>
        <photo1>http://www.semhora.com/xmlparser/principal/foto1.jpg</photo1>
        <photo2>http://www.semhora.com/xmlparser/principal/foto2.jpg</photo2>
        <photo3>http://www.semhora.com/xmlparser/principal/foto3.jpg</photo3>
        <photo4>http://www.semhora.com/xmlparser/principal/foto4.jpg</photo4>
        <photo5>http://www.semhora.com/xmlparser/principal/foto5.jpg</photo5>
        <flyer1>http://www.semhora.com/xmlparser/principal/flyer1.jpg</flyer1>
        <flyer2>http://www.semhora.com/xmlparser/principal/flyer2.jpg</flyer2>
        <address>Rua Santo Ubaldo, 28 - São Paulo</address>
        <latitude>-23.596089,-46.682393</latitude>
    </principal>
</music>

块:

- (void)loadURL:(NSString *)newURL{

    // Create a success block to be called when the asyn request completes
    TBXMLSuccessBlock successBlock = ^(TBXML *tbxmlDocument) {


        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        // If TBXML found a root node, process element and iterate all children
        if (tbxmlDocument.rootXMLElement)
        {

            // Obtain root element
            TBXMLElement * root = tbxml.rootXMLElement;
            if (root)
            {
                _storeArray = [NSMutableArray new];
                [_storeArray removeAllObjects];
                TBXMLElement * elem_PLANT = [TBXML childElementNamed:@"principal" parentElement:root];
                while (elem_PLANT !=nil)
                {
                    TBXMLElement * elem_title = [TBXML childElementNamed:@"title" parentElement:elem_PLANT];
                    NSString *titleName = [TBXML textForElement:elem_title];

//                    TBXMLElement * elem_artist = [TBXML childElementNamed:@"text" parentElement:elem_PLANT];
//                    NSString *artistName = [TBXML textForElement:elem_artist];

                    TBXMLElement * elem_thumb = [TBXML childElementNamed:@"thumb_url" parentElement:elem_PLANT];
                    NSString *thumbName = [TBXML textForElement:elem_thumb];

                    TBXMLElement * elem_photo1 = [TBXML childElementNamed:@"photo1" parentElement:elem_PLANT];
                    NSString *photo1Name = [TBXML textForElement:elem_photo1];

                    TBXMLElement * elem_photo2 = [TBXML childElementNamed:@"photo2" parentElement:elem_PLANT];
                    NSString *photo2Name = [TBXML textForElement:elem_photo2];

                    TBXMLElement * elem_photo3 = [TBXML childElementNamed:@"photo3" parentElement:elem_PLANT];
                    NSString *photo3Name = [TBXML textForElement:elem_photo3];

                    TBXMLElement * elem_photo4 = [TBXML childElementNamed:@"photo4" parentElement:elem_PLANT];
                    NSString *photo4Name = [TBXML textForElement:elem_photo4];

                    TBXMLElement * elem_photo5 = [TBXML childElementNamed:@"photo5" parentElement:elem_PLANT];
                    NSString *photo5Name = [TBXML textForElement:elem_photo5];

                    TBXMLElement * elem_flyer1 = [TBXML childElementNamed:@"flyer1" parentElement:elem_PLANT];
                    NSString *flyer1Name = [TBXML textForElement:elem_flyer1];

                    TBXMLElement * elem_flyer2 = [TBXML childElementNamed:@"flyer2" parentElement:elem_PLANT];
                    NSString *flyer2Name = [TBXML textForElement:elem_flyer2];

                    NSString *addressName = nil;

                    TBXMLElement * elem_address = [TBXML childElementNamed:@"address" parentElement:elem_PLANT];
                    if (elem_address) {
                    addressName = [TBXML textForElement:elem_address];
                    }
                    TBXMLElement * elem_latitude = [TBXML childElementNamed:@"latitude" parentElement:elem_PLANT];
                    NSString *latitudeName = [TBXML textForElement:elem_latitude];

                    NSDictionary *dictionary = [[NSDictionary alloc]initWithObjects:@[titleName, thumbName, photo1Name, photo2Name, photo3Name, photo4Name, photo5Name, flyer1Name, flyer2Name, addressName, latitudeName] forKeys:@[@"title", @"thumb_url", @"photo1", @"photo2", @"photo3", @"photo4", @"photo5", @"flyer1", @"flyer2", @"address", @"latitude"]];
                    elem_PLANT = [TBXML nextSiblingNamed:@"principal" searchFromElement:elem_PLANT];
                    [_storeArray addObject:dictionary];
                   [self startLoading:dictionary];
                     [[NSOperationQueue mainQueue] addOperationWithBlock:^{ [_carousel reloadData]; }];
                }


            }

        }

    };

    // Create a failure block that gets called if something goes wrong
    TBXMLFailureBlock failureBlock = ^(TBXML *tbxmlDocument, NSError * error) {
        NSLog(@"Error! %@ %@", [error localizedDescription], [error userInfo]);
    };

    // Initialize TBXML with the URL of an XML doc. TBXML asynchronously loads and parses the file.
    tbxml = [[TBXML alloc] initWithURL:[NSURL URLWithString:newURL]
                               success:successBlock
                               failure:failureBlock];

}
4

1 回答 1

1

这不是对您的“问题是什么”问题的真正答案,而是反馈问题可能不在您的 xml 处理代码中。

如您所见,您对 xml 有一个异步查询,该查询在成功分支上具有此解析器处理程序。为了简化测试,我在AppDelegate中使用了这个代码片段(test.xml 是您在上面指定的):

TBXML *tbxml = [[TBXML alloc] initWithXMLFile:@"test.xml" error:nil];
TBXMLElement *root = tbxml.rootXMLElement;
if (root)
{
    NSLog(@"root found" );
    TBXMLElement * elem_PLANT = [TBXML childElementNamed:@"principal" parentElement:root];
    while (elem_PLANT !=nil)
    {
        NSLog(@"iter");
        NSString *addressName = nil;
        TBXMLElement * elem_address = [TBXML childElementNamed:@"address" parentElement:elem_PLANT];
        if (elem_address) {
            addressName = [TBXML textForElement:elem_address];
            NSLog(@"address=%@", addressName);
        }
        elem_PLANT = [TBXML nextSiblingNamed:@"principal" searchFromElement:elem_PLANT];
    }

}

因此,执行此操作会在控制台中显示以下内容:

2013-04-09 13:26:49.258 stack2[96042:c07] root found
2013-04-09 13:26:49.265 stack2[96042:c07] iter
2013-04-09 13:26:49.275 stack2[96042:c07] address=Rua Santo Ubaldo, 28 - São Paulo

要检查的区域可能是 1) 测试具有更多“主要”元素的真实 xml - 真实世界的 xml 文件可能会发生奇怪的事情 2) 围绕异步调用的一些事情,也许可以尝试使用调度组

评论:使用 TBXML v1.5,xcode 4.6.1

于 2013-04-09T11:46:10.330 回答