TBXML 根元素始终为 null
我对此完全感到困惑,因为我已经使用 TBXML 编写了其他几种方法来正确解析 XML 文件。下面是有问题的方法。无论我尝试什么,根元素都保持为空,因此该方法的其余部分失败。正如您所看到的,XML 文件位于通过 PHP 输出的 Web 上。我在另一个应用程序中使用了完全相同的方法,没有任何问题。我将 XML 输出与之前使用的输出进行了比较,发现除了元素之外没有任何差异。这个方法的代码几乎是相同的,但我一定遗漏了一些东西。任何帮助将不胜感激。
- (void)loadFromZenPhoto{
tbxml = [[TBXML alloc] initWithURL:[NSURL URLWithString:@"http://www.flpublicarchaeology.org/civilwar/generatexml.php"]];
// Obtain root element
TBXMLElement *root = tbxml.rootXMLElement;
// if root element is valid
if (root) {
TBXMLElement *site = [TBXML childElementNamed:@"site" parentElement:root];
// if a site element was found, create site object
while (site != nil) {
Site *aSite = [[Site alloc] init];
TBXMLElement *siteid = [TBXML childElementNamed:@"id" parentElement:site];
if (siteid != nil)
aSite.siteid = [[TBXML textForElement:siteid] intValue];
TBXMLElement *parentid = [TBXML childElementNamed:@"parentid" parentElement:site];
if (parentid != nil)
aSite.parentid = [[TBXML textForElement:parentid] intValue];
TBXMLElement *folder = [TBXML childElementNamed:@"folder" parentElement:site];
if (folder != nil)
aSite.folder = [TBXML textForElement:folder];
TBXMLElement *title = [TBXML childElementNamed:@"title" parentElement:site];
if (title != nil)
aSite.title = [TBXML textForElement:title];
// add site object to the sites array
[sites addObject:aSite];
//advance to next sibling
site = [TBXML nextSiblingNamed:@"site" searchFromElement:site];
}
}
}
I am completely baffled by this as I have coded several other methods using TBXML to properly parse an XML file. Below is the method in question. The root element remains null no matter what I try therefore the rest of the method fails. As you can see, the XML file is located on the web being output via PHP. I've used this exact same method in another application with no trouble whatsoever. I've compared the XML output to what I previously used and see no differences other than elements. The code for this method is almost identical but I must be missing something. Any help would be greatly appreciated.
- (void)loadFromZenPhoto{
tbxml = [[TBXML alloc] initWithURL:[NSURL URLWithString:@"http://www.flpublicarchaeology.org/civilwar/generatexml.php"]];
// Obtain root element
TBXMLElement *root = tbxml.rootXMLElement;
// if root element is valid
if (root) {
TBXMLElement *site = [TBXML childElementNamed:@"site" parentElement:root];
// if a site element was found, create site object
while (site != nil) {
Site *aSite = [[Site alloc] init];
TBXMLElement *siteid = [TBXML childElementNamed:@"id" parentElement:site];
if (siteid != nil)
aSite.siteid = [[TBXML textForElement:siteid] intValue];
TBXMLElement *parentid = [TBXML childElementNamed:@"parentid" parentElement:site];
if (parentid != nil)
aSite.parentid = [[TBXML textForElement:parentid] intValue];
TBXMLElement *folder = [TBXML childElementNamed:@"folder" parentElement:site];
if (folder != nil)
aSite.folder = [TBXML textForElement:folder];
TBXMLElement *title = [TBXML childElementNamed:@"title" parentElement:site];
if (title != nil)
aSite.title = [TBXML textForElement:title];
// add site object to the sites array
[sites addObject:aSite];
//advance to next sibling
site = [TBXML nextSiblingNamed:@"site" searchFromElement:site];
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我想你面临着和我之前遇到的同样的问题。我想问题是你通过 XCode 建议修复了 TBXML.m 中的语义问题。 请回答此问题以获取更多信息。
Ok, I think you are facing the same problem that I had before. I guess the problem is you fixed the semantic issues in TBXML.m by XCode suggestions. Please this answer for more info.
嗯,这是一个棘手的问题。您的 Web 服务似乎正在返回 ASCII 编码的 html。而 TBXML 需要 UTF8 编码。
尝试使用此检查
它会以
stringLoad: (null)
的形式记录到控制台,但如果您使用NSLog(@"stringLoad: %@", [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http ://www.flpublicarchaeology.org/civilwar/generatexml.php"] 编码:NSASCIIEncoding 错误:nil]);
控制台记录正确的 html 响应。
希望这有帮助。
Well this was a tricky one. It looks like your web service is returning ASCII encoded html. Whereas TBXML expects a UTF8 encoding.
Try checking with this
It logs to console as
stringLoad: (null)
, but if you useNSLog(@"stringLoad: %@", [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.flpublicarchaeology.org/civilwar/generatexml.php"] encoding:NSASCIIEncoding error:nil]);
the console logs the correct html response.
Hope this helps.