2013-03-23 2 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을 사용하여 메소드를 호출하고이 오류를 얻을 수 like

<?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을 잘못 배치했는지 확인하기 위해 변경했습니다. 나는 이것을 지금 시험 할 것이다. –

+0

니스. 이것은 완벽하게 작동했습니다. 감사. –

+0

당신을 진심으로 환영합니다. –