2013-03-23 160 views
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> 

回答

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

是的,我已經更改了XML以查看它是否佈局錯誤。我現在會測試這個。 – 2013-03-23 01:18:00

+0

不錯。這工作完美。謝謝。 – 2013-03-23 01:21:16

+0

不客氣。 – 2013-03-23 01:56:35