开发者

XML parsing - iPhone

开发者 https://www.devze.com 2023-01-27 19:34 出处:网络
Hello I am new to iPhone development. I have created one application that consume data from web service. And it works fine but I have a problem with parsing.

Hello I am new to iPhone development. I have created one application that consume data from web service. And it works fine but I have a problem with parsing.

The xml data that I get from the web service is:

<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>&lt;?xml version="1.0" encoding="UTF-8"?&g开发者_运维问答t;&lt;newblock&gt;&lt;name&gt;abcde
&lt;/name&gt;&lt;password&gt;12345678&lt;/password&gt;&lt;/newblock&gt;</item></xml>

This is what I call from connectionDidFinishLoading method:

-(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];
}

The following are the parsing methods used:

-(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; 
}
}

My query is : I want to extract the "name" tag with value "abcde" from the received XML data.

But when I search for "name" with value "abcde" in the above xml data received, parser can't find it because of &qt, &lt etc.

1) How to fix my NSMutableData to have normal values (<, > etc.)? Any inbuilt functions of NSMutableData to replace &lt, &gt with <, >? Or

2) Is there any thing missing (or doing wrong) while generating the xml data at the server side or Am I missing anything while parsing at the client side (on iPhone) Or

3) Any other solution to fix the above problem.

Thanks in advance.


Try :-

 [string stringByReplacingOccurrencesOfString:@"&lt" withString:@"<"];


I would recommend you use TBXML library, It's a very small and has very good performance. It also quite easy to grasp , at least easier than NSXMLParser, IMHO.

  1. There is no build in function to replace &lt, &gt in Objective C. So you might want to wrap that string in on the server.
0

精彩评论

暂无评论...
验证码 换一张
取 消