解析肥皂响应时出现问题(iphone / touchxml)

发布于 2024-08-28 19:02:24 字数 2278 浏览 7 评论 0原文

我制作了一个网络服务并且它可以工作,问题在于网络服务响应。 我有这个 xml(我想用 touchxml 解析),问题是 touchxml 它找到了两个 xml,一个在另一个里面,它导致崩溃。 这是我的问题,我如何解析这个xml?我如何删除所有像soap-env、xmlns这样的东西...

<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body>
<ns1:getCategoriesResponse xmlns:ns1="http://192.168.1.30/wyws/server.php"><return xsi:type="xsd:string">

    <?xml version="1.0" encoding="UTF-8"?>
    <categories>
       <category id="1" desc="Encontrar Códigos " icon="wy">Código </category>
       <category id="3" desc="" icon="farm">Far</category>
       <category id="5" desc="" icon="gas">Gas</category>
       <category id="6" desc="" icon="hos">Hos</category>
       <category id="7" desc="" icon="pol">Pol</category>
    </categories>

</return></ns1:getCategoriesResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>

这就是我打电话的方式:

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue:@"8bit" forHTTPHeaderField:@"Content-Transfer-Encoding"];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"getCategories" forHTTPHeaderField:@"SOAPAction"];

[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];  

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

webData = [[NSMutableData data] retain];

然后

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSLog(@"DONE. Received Bytes: %d", [webData length]);

    NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSISOLatin1StringEncoding];


NSLog(theXml);
....
}

I make a webservice and it works, the problem is with the webservice response.
I have this xml (and i want to parse with touchxml), the problem is with touchxml that it founds two xml, one inside the other and it make crash.
Here is my question, how i can parse this xml? how i can remove all the stuff like soap-env, xmlns ....

<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body>
<ns1:getCategoriesResponse xmlns:ns1="http://192.168.1.30/wyws/server.php"><return xsi:type="xsd:string">

    <?xml version="1.0" encoding="UTF-8"?>
    <categories>
       <category id="1" desc="Encontrar Códigos " icon="wy">Código </category>
       <category id="3" desc="" icon="farm">Far</category>
       <category id="5" desc="" icon="gas">Gas</category>
       <category id="6" desc="" icon="hos">Hos</category>
       <category id="7" desc="" icon="pol">Pol</category>
    </categories>

</return></ns1:getCategoriesResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>

This is how i make the call:

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue:@"8bit" forHTTPHeaderField:@"Content-Transfer-Encoding"];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"getCategories" forHTTPHeaderField:@"SOAPAction"];

[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];  

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

webData = [[NSMutableData data] retain];

And then

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSLog(@"DONE. Received Bytes: %d", [webData length]);

    NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSISOLatin1StringEncoding];


NSLog(theXml);
....
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

虫児飞 2024-09-04 19:02:24

我写了这个,它不是很优雅,但它有效。

 NSRange in = [tmpTheXml rangeOfString:@"<return xsi:type=\"xsd:string\">"];
 NSRange fi = [tmpTheXml rangeOfString:@"</return>"];
 int inic = in.location + in.length;
 NSRange tmprange = {inic,fi.location - inic} ;
 tmpTheXml = [tmpTheXml substringWithRange:tmprange];

I wrote this, it´s not very elegant, but it works.

 NSRange in = [tmpTheXml rangeOfString:@"<return xsi:type=\"xsd:string\">"];
 NSRange fi = [tmpTheXml rangeOfString:@"</return>"];
 int inic = in.location + in.length;
 NSRange tmprange = {inic,fi.location - inic} ;
 tmpTheXml = [tmpTheXml substringWithRange:tmprange];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文