您好,我是 iPhone 开发的新手。我创建了一个使用来自 Web 服务的数据的应用程序。它工作正常,但我在解析时遇到问题。
我从 Web 服务获得的 xml 数据是:
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>A PHP Error was encountered</h4>
<p>Severity: Warning</p>
<p>Message: session_start() [<a href='function.session-start'>function.session-tart</a>]:
Cannot send session cookie - headers already sent by (output started at D:\xampp\htdocs\system
\application\controllers\isignin.php:4)</p>
<p>Filename: libraries/session.php</p>
<p>Line Number: 146</p>
</div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>A PHP Error was encountered</h4>
<p>Severity: Warning</p>
<p>Message: session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent (output started at D:\xampp\htdocs\6d\system \application\controllers\isignin.php:4)</p>
<p>Filename: libraries/session.php</p>
<p>Line Number: 146</p>
</div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>A PHP Error was encountered</h4>
<p>Severity: Warning</p>
<p>Message: Cannot modify header information - headers already sent by (output started at D:\xampp\htdocs\6d\system\application\controllers\isignin.php:4)</p>
<p>Filename: codeigniter/Common.php</p>
<p>Line Number: 360</p>
</div><?xml version="1.0" encoding="utf-8"?>
<xml><item><?xml version="1.0" encoding="UTF-8"?><newblock><name>abcde
</name><password>12345678</password></newblock></item></xml>
这是我从 connectionDidFinishLoading 方法中调用的:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"DONE. Received Bytes: %d", [webData length]);
if (xmlParser)
{
[xmlParser release];
}
xmlParser = [[NSXMLParser alloc] initWithData: webData];
[xmlParser setDelegate: self];
[xmlParser setShouldResolveExternalEntities:YES];
[xmlParser parse];
[connection release];
[webData release];
}
以下是使用的解析方法:
-(void) parser:(NSXMLParser *) parser
didStartElement:(NSString *) elementName
namespaceURI:(NSString *) namespaceURI
qualifiedName:(NSString *) qName
attributes:(NSDictionary *) attributeDict {
if( [elementName isEqualToString:@"GetWeatherResult"])
{
if (!soapResults)
{
soapResults = [[NSMutableString alloc] init];
}
elementFound = YES;
}
}
-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string
{
if (elementFound)
{
[soapResults appendString: string];
}
}
-(void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"GetWeatherResult"])
{
//---displays the country---
NSLog(soapResults);
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Current Temperature!"
message:soapResults
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
[soapResults setString:@""];
elementFound = FALSE;
}
}
我的查询是:我想从收到的 XML 数据中提取值为“abcde”的“名称”标签。
但是当我在收到的上述xml数据中搜索值为“abcde”的“name”时,由于&qt、<等 原因,解析器找不到它。
1) 如何修复我的 NSMutableData 使其具有正常值(<、> 等)?NSMutableData 的任何内置函数可以 用<, >替换<, >吗?或者
2)在服务器端生成 xml 数据时是否有任何遗漏(或做错),或者在客户端(在 iPhone 上)解析时是否遗漏了任何东西,或者
3)解决上述问题的任何其他解决方案。
提前致谢。