嗨,我在过去的两天到处都看过&找不到对我有用的东西。我已经设法从网址中提取数据,并在我的日志中显示每个元素和值。
我无法弄清楚的步骤是如何将它添加到我的“events”数组中,而不必手动搜索每个字符串,因为它会自动为我执行(如我的日志中所示)。
这是我的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
现在我已经浏览了所有元素,属性等,如何将其链接到我的表视图和行计数?我想放弃,请帮忙!非常感谢你的时间非常感谢你!
答案 0 :(得分:0)
Sudo rm -rf得到了最好的回应
那你为什么不试试呢?我用过TBXML已经有一段时间了。从那以后,我开始直接使用libxml2(通过Cocoa包装器),所以我可以使用X-path直接选择我想要的东西而不需要循环。作为替代方案,您应该尝试使用此解析库;我听说它的简洁性很棒。 github.com/ZaBlanc/RaptureXML对不起,我现在没有时间为此编写测试用例。