简体   繁体   English

iOS-无法使SMXMLDocument工作

[英]iOS - Cannot get SMXMLDocument to work

Im using a xmlparser called SMXMLDocument . 我正在使用一个名为SMXMLDocument的xmlparser。 https://github.com/nfarina/xmldocument https://github.com/nfarina/xmldocument

I use NSURLConnection and NSURL to fetch the XML down and that works fine. 我使用NSURLConnectionNSURL向下提取XML ,并且工作正常。 I can log it in the console. 我可以在控制台中登录。

Now it is time to get the elements inside a string and i use this code. 现在是时候在字符串中获取元素了,我使用此代码。

- (void)connectionDidFinishLoading:(NSURLConnection *)conn 
{
    NSString *fetchedXML = [[[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding] autorelease];
    NSLog(@"%@", fetchedXML);

    [xmlData release];
    xmlData = nil;

    [connection release];
    connection = nil;

    [self performSelector:@selector(parseXML)];

}

-(void)parseXML
{

    SMXMLDocument *document = [SMXMLDocument documentWithData:xmlData error:nil];

    SMXMLElement *content = [document.root childNamed:@"content"];


    for (SMXMLElement *name in [content childrenNamed:@"name"])
    {
        NSString *name = [name valueWithPath:@"name"];
        NSLog(@"name:%@", name);
    }
}

This is apparently not working and i don't get a log with the name element. 这显然是行不通的,我没有获得带有name元素的日志。 My xml code is very simple and looks like this: 我的xml代码非常简单,看起来像这样:

<?xml version="1.0" encoding="UTF-8" ?>
<content>
<name>John Doe</name>
</content>

Can anyone help me with why this is not working. 谁能帮我解决为什么这不起作用。 Thanks in advance! 提前致谢! :D :D

Instead of 代替

 SMXMLElement *content = [document.root childNamed:@"content"];

try 尝试

 SMXMLElement *content = document.root;

Because "content" element is the root of your document :) 因为“ content”元素是文档的根:)

You need 你需要

SMXMLDocument *document = [SMXMLDocument documentWithData:xmlData error:nil];

NSString *content = [[[document.root childNamed:@"content"] childNamed:@"name"] value];

NSLog(@"name:%@", content);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM