不知道如何将TBXML网址数据添加到我的表格视图
问题描述:
嗨我在过去两天到处寻找&找不到任何适合我的东西。我设法从网址中提取数据,并在日志中显示每一个元素和值。不知道如何将TBXML网址数据添加到我的表格视图
我无法弄清楚的一步是如何将它添加到我的“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
所以,现在我已经经历了所有元素,属性等,我如何将它链接到我的表视图和行数?我想放弃,请帮助!我非常感谢你的时间,非常感谢你!
答
须藤室射频有最好的响应
那么你为什么不试试呢?自从我使用TBXML以来已经有一段时间了。从那以后,我开始直接使用libxml2(通过Cocoa包装器),这样我就可以使用X-path直接选择我想要的内容而无需循环。作为替代,你应该尝试这个解析库;我听说过它简单的优点。 github.com/ZaBlanc/RaptureXML对不起,我现在没有时间写这个测试用例。
你试图把什么特定的数据从你的表格视图放入? – 2012-04-28 14:23:08
从事件中,我试图拉出uri和displayName。从城市的位置,lng,&lat。从开始,时间和日期。我也试图将我的行数分配给totalEntries的数量,但似乎无法弄清楚这一点。 – Year3000 2012-04-28 19:47:06
我在想这些问题,但我不确定:'TBXMLElement * event = [TBXML childElementNamed:@“event”parentElement:element]; NSString * uri = [TBXML valueOfAttributeNamed:@“uri”forElement:event]; NSString * venue = [TBXML valueOfAttributeNamed:@“displayName”forElement:event]; [events addObject:[NSArray arrayWithObjects: [TBXML textForElement:event], uri,venue,nil]]; – Year3000 2012-04-28 20:15:57