在 iOS SDK (iPad) 中使用 TouchXML 解析 SOAP 结果

发布于 2024-10-14 07:23:06 字数 2408 浏览 1 评论 0原文

我正在开发一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无所的.畏惧 2024-10-21 07:23:06

尝试使用 NSXMLDocument,它应该和 CXMLDocument 一样工作。以下是一些文档:
链接

Try using the NSXMLDocument, it should work as well as CXMLDocument. Here are some docs:
link

七婞 2024-10-21 07:23:06

我建议使用 XPath 解析器。您没有做任何太重的事情,因此速度的提高是非常值得的。我个人的选择是TFHpple。有一些更快、更精确的解决方案,但我发现 TFHpple 的简单性很难被超越。

示例:

NSData *data = [[NSData alloc] initWithContentsOfFile:@"example.html"]; 
// Create parser xpathParser = [[TFHpple alloc] initWithXMLData:data];
NSArray *elements = [xpathParser search:@"//LogOnResult"];
TFHppleElement *element = [elements objectAtIndex:0];
// Get the text within the cell tag NSString *content = [element content];
[xpathParser release]; [data release];

显然您必须编写一些代码来保存您想要的部分。查看这些数据,我可能会创建一个可以通过属性访问的自定义数据持有者类。您可以将 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:

NSData *data = [[NSData alloc] initWithContentsOfFile:@"example.html"]; 
// Create parser xpathParser = [[TFHpple alloc] initWithXMLData:data];
NSArray *elements = [xpathParser search:@"//LogOnResult"];
TFHppleElement *element = [elements objectAtIndex:0];
// Get the text within the cell tag NSString *content = [element content];
[xpathParser release]; [data release];

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文