iphoneiosobjective-cxmltouchxml

Assertion failure error


I am making a dynamic library for iPhone (for Cydia) and am trying to use TouchXML to parse XML. When I call this method

+(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;
} 

from my app, I get this error from the debugger

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

I am calling the method using this

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

and the XML is laid out like this

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

Solution

  • I was playing with the documentation example and with your XML and I see you changed the XML. The following code worked for me with the xml you posted in the question:

    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];