0

嗨,过去两天我到处找,找不到任何适合我的东西。我设法从 url 中提取数据,并在我的日志中显示每个元素和值。

我无法弄清楚的步骤是如何将它添加到我的“事件”数组中,而无需手动搜索每个字符串,因为它会自动为我执行此操作(如我的日志中所示)。

这是我的 loadUrl 方法:

- (void)loadURL {

// Create a success block to be called when the asyn request completes
TBXMLSuccessBlock successBlock = ^(TBXML *tbxmlDocument) {
    NSLog(@"PROCESSING ASYNC CALLBACK");

    // If TBXML found a root node, process element and iterate all children
    if (tbxmlDocument.rootXMLElement)
        [self traverseElement:tbxmlDocument.rootXMLElement];

};

// 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:@"http://somexml.com/xml"] 
                           success:successBlock 
                           failure:failureBlock]; 

    events = [NSMutableArray array]; }

这是我的 traverseElement 方法:

- (void) traverseElement:(TBXMLElement *)element {

do {
    // Display the name of the element
    NSLog(@"%@",[TBXML elementName:element]);


    // iterate all child elements of tbxml.rootXMLElement that are named "author"
    [TBXML iterateAttributesOfElement:element withBlock:^(TBXMLAttribute *attribute, NSString *name, NSString *value) {

        // Display name and value of attribute to the log window
        NSLog(@"%@->%@ = %@",[TBXML elementName:element], name, value);

    }];

    // if the element has child elements, process them
    if (element->firstChild) [self traverseElement:element->firstChild];

    // Obtain next sibling element
} while ((element = element->nextSibling));  


}

这是我要引入的 xml:

<resultsPage totalEntries="6" perPage="50" page="1" status="ok">
<results>
<event uri="http://somexml.com/xml" popularity="0.863682" displayName="Radio 1's Hackney 
Weekend 2012" id="9234656" type="Festival" status="ok">
<location city="London, UK" lng="-0.128" lat="51.5078"/>
<series displayName="Radio 1's Hackney Weekend"/>
<end time="" date="2012-06-24" datetime=""/>
<start time="" date="2012-06-23" datetime=""/>
<performance displayName="Lucy Labeaux" billingIndex="5" id="23336188" billing="headline">
<artist uri="http://www.somexml.com/artistxml" displayName="Lucy Labeaux" id="1168415">
<identifier href="http://somexml.com.xml" mbid="4593d49a-7f67-46ba-9ec0-126bd676286f"/>
</artist>
</performance>

我的日志如下所示:

PROCESSING ASYNC CALLBACK
 resultsPage
 resultsPage->totalEntries = 6
 resultsPage->perPage = 50
 resultsPage->page = 1
 resultsPage->status = ok
 results
 event
 event->uri = http://example.com
 event->popularity = 0.863682
 event->displayName = Cooled Up Week 2012
 event->id = 9234656
 event->type = Festival
 event->status = ok
 location
 location->city = London, UK
 location->lng = -0.128
 location->lat = 51.5078
 series
 series->displayName = Cooled Up Week 2012
 end
 end->time = 
 end->date = 2012-06-24
 end->datetime = 
 start
 start->time = 
 start->date = 2012-06-23
 start->datetime = 
 performance
 performance->displayName = Lucy Labeaux
 performance->billingIndex = 1

所以现在我已经浏览了所有元素、属性等,如何将它链接到我的表格视图和行数?我想放弃,请帮助!我真的很感谢你的时间非常感谢你!

4

1 回答 1

0

Sudo rm -rf 反应最好

那你为什么不试试呢?自从我使用 TBXML 以来已经有一段时间了。从那时起,我开始直接使用 libxml2(通过 Cocoa 包装器),因此我可以使用 X-path 直接选择我想要的内容,而无需循环。作为替代方案,您应该尝试这个解析库;我听说过它的简单性。github.com/ZaBlanc/RaptureXML 很抱歉,我目前没有时间为此编写测试用例。

于 2012-05-16T10:47:54.437 回答