在 iOS SDK (iPad) 中使用 TouchXML 解析 SOAP 结果
我正在开发一个使用 WebService 中的函数的 iPad 项目。 处理网络服务连接、数据等工作查找。但我无法使用 TouchXML 解析结果 SOAP。从 xml 中获取节点始终返回 0 长度。
输出 xml:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<LogOnResponse xmlns="http://coreservices.org/v1">
<LogOnResult>
<Id>1c0e9ad0-a3be-4cd0-8f0d-0616a63a4e28</Id>
<userId>2</userId>
<user>
<ID>2
<Name>Beheerder
<emailAddress />
<cultureName>nl-NL</cultureName>
</user>
</LogOnResult>
</LogOnResponse>
</soap:Body>
</soap:Envelope>
解析器代码:
NSData *aData = [[NSData alloc] initWithBytes:[webData mutableBytes]
length:[webData length]];
NSString *xmlData = [[NSString alloc] initWithData:aData
encoding:NSASCIIStringEncoding];
NSLog(@"%@", xmlData);
[xmlData release];
CXMLDocument *domUserIdentity = [[[CXMLDocument alloc] initWithData:aData
options:0
error:nil]
autorelease];
[aData release];
NSArray *nodesList = [domUserIdentity nodesForXPath:@"//LogOnResult" error:nil]; // 0 length
for (CXMLElement *resultElement in nodesList) {
for (int counter = 0; counter
NSString *elemName = [[resultElement childAtIndex:counter] name];
NSString * elemValue = [[[resultElement childAtIndex:counter]
stringValue]
stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[elemName release];
[elemValue release];
}
}
[nodesList release];
知道我做错了什么吗?
提前非常感谢。
伊诺尔
I'm working on an iPad project that is using functions in a WebService.
Handling webservice connection, data etc works find. But i'm not able to parse the result SOAP using TouchXML. Getting the nodes out of the xml always returns 0 length.
The output xml:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<LogOnResponse xmlns="http://coreservices.org/v1">
<LogOnResult>
<Id>1c0e9ad0-a3be-4cd0-8f0d-0616a63a4e28</Id>
<userId>2</userId>
<user>
<ID>2
<Name>Beheerder
<emailAddress />
<cultureName>nl-NL</cultureName>
</user>
</LogOnResult>
</LogOnResponse>
</soap:Body>
</soap:Envelope>
parser code:
NSData *aData = [[NSData alloc] initWithBytes:[webData mutableBytes]
length:[webData length]];
NSString *xmlData = [[NSString alloc] initWithData:aData
encoding:NSASCIIStringEncoding];
NSLog(@"%@", xmlData);
[xmlData release];
CXMLDocument *domUserIdentity = [[[CXMLDocument alloc] initWithData:aData
options:0
error:nil]
autorelease];
[aData release];
NSArray *nodesList = [domUserIdentity nodesForXPath:@"//LogOnResult" error:nil]; // 0 length
for (CXMLElement *resultElement in nodesList) {
for (int counter = 0; counter
NSString *elemName = [[resultElement childAtIndex:counter] name];
NSString * elemValue = [[[resultElement childAtIndex:counter]
stringValue]
stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[elemName release];
[elemValue release];
}
}
[nodesList release];
Any idea what did i do wrong?
Thank's alot in advance.
Inoel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用 NSXMLDocument,它应该和 CXMLDocument 一样工作。以下是一些文档:
链接
Try using the NSXMLDocument, it should work as well as CXMLDocument. Here are some docs:
link
我建议使用 XPath 解析器。您没有做任何太重的事情,因此速度的提高是非常值得的。我个人的选择是TFHpple。有一些更快、更精确的解决方案,但我发现 TFHpple 的简单性很难被超越。
示例:
显然您必须编写一些代码来保存您想要的部分。查看这些数据,我可能会创建一个可以通过属性访问的自定义数据持有者类。您可以将 XML 解析为该内容,然后将其保存为将访问它的视图控制器中的属性。
快乐编码!
I recommend using an XPath Parser. You aren't doing anything too heavy, so the speed hit is well worth it. My personal choice is TFHpple. There are slightly faster and more precise solutions out there, but I find TFHpple's simplicity hard to beat.
Example:
Obviously you'd have to write some code to save the parts you want. Looking at this data, I'd probably make a custom data holder class that you can access with properties. You can parse your XML into that and then save it as a property in the view controller that will be accessing it.
Happy coding!