断言失败错误

时间:2013-03-23 00:24:33

标签: iphone ios objective-c xml touchxml

我正在为iPhone制作一个动态库(用于Cydia),我正在尝试使用TouchXML来解析XML。当我打电话给这个方法时

+(NSArray *)initWithXMLfromData:(NSData *)data withRootElement:(NSString *)rootElement{
NSError *err;
CXMLDocument *parser = [[CXMLDocument alloc] initWithData:data options:0 error:&err];
NSArray *xml = [parser nodesForXPath:[NSString stringWithFormat:@"//%@", rootElement] error:&err];
if (err) {
    NSLog(@"%@", err);
}
return xml;
} 

从我的应用程序中,我从调试器中得到此错误

Assertion failure in -[CXMLElement description], /Users/macuser/Desktop/c/parser/TouchXML-master/Source/CXMLElement.m:287

我正在使用此

调用方法
NSArray *xml = [XMLParse initWithXMLfromData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://a-cstudios.com/xml.xml"]] withRootElement:@"root"];
NSLog(@"%@", [xml objectAtIndex:0]);

并且XML就像这样布局

<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
    <val>34</val>
</root>

1 个答案:

答案 0 :(得分:1)

我正在使用文档示例和XML,我看到您更改了XML。以下代码适用于您在问题中发布的xml:

NSMutableArray *res = [[NSMutableArray alloc] init];

CXMLDocument *doc = [[[CXMLDocument alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://a-cstudios.com/xml.xml"]] options:0 error:nil] autorelease];
NSArray *nodes = NULL;

//  searching for val nodes
nodes = [doc nodesForXPath:@"//val" error:nil];

for (CXMLElement *node in nodes) {
    NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
    int counter;
    for(counter = 0; counter < [node childCount]; counter++) {
        //  common procedure: dictionary with keys/values from XML node
        [item setObject:[[node childAtIndex:counter] stringValue] forKey:[[node childAtIndex:counter] name]];
    }

    [res addObject:item];
    [item release];
}

//  print  results
NSLog(@"%@", res);
[res release];