0

我正在为 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>
4

1 回答 1

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];
于 2013-03-23T01:15:09.447 回答