我正在使用TouchXML使用Web服务,我无法按照教程读取节点。我确实收到了CXMLDocument元素,但是,我的nodesForXPath调用总是返回NULL。可能有什么不对?
XML解析代码
NSMutableArray *res = [[NSMutableArray alloc] init];
NSData* valueData=[value dataUsingEncoding:NSUTF8StringEncoding];
// Do something with the CXMLDocument* result
CXMLDocument *result = [[[CXMLDocument alloc] initWithData:valueData options:0 error:nil] autorelease];
NSArray *nodes = NULL;
nodes = [result nodesForXPath:@"//Outputs" error:nil];
NSLog (@"Nodes is %@\n", nodes);
for (CXMLElement *node in nodes) {
NSLog (@"Test");
NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
int counter;
NSLog (@"Child Count is %@\n",[node childCount]);
NSLog (@"Child node is%@\n",[[node childAtIndex:0] stringValue]);
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]];
}
//[item setObject:[[node attributeForName:@"id"] stringValue] forKey:@"id"];
[res addObject:item];
[item release];
}
// and we print our results
NSLog(@"%@", res);
[res release];
}
XML:
<Nodes>
<Inputs>
<name>TestProgram</name>
</Inputs>
<Outputs>
<ReturnCode>0</ReturnCode>
<ReturnMessage>Success</ReturnMessage>
<NameDetails attr1="value1">Test Program</NameDetails>
<NameInstance attr1="value1">
<NameCharacteristics>
<Dob>04-25-2011</Dob>
</NameCharacteristics>
</NameInstance>
</Outputs>
</Nodes>
nodesForXPatch for //输出始终返回NULL。我想获取节点值。我尝试用Dob替换输出,但仍然得到一个空集。