title: Why can't I parse an image from an XML file using this code from my iPhone application?
body: <p>I'm having trouble importing image data which is encoded in an XML file.  Following is the code of my class where I read the XML file and try to get the photo from the encoded data.</p>
  
  <p><strong>edit 1</strong></p>
  
  <p><em>I haven't any errors but I always get an empty value on "artXML.photo"....</em></p>
  
  <p>Can anyone point out what might be going wrong?</p>
  
  <pre><code>-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
  
      valoreLetto = [valoreLetto stringByAppendingString:string];
  }
  
  -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
    namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
  {
  
      if([elementName isEqualToString:@"photo"]) {
  
          NSData *aData;
          aData = [NSData dataWithBase64EncodedString:valoreLetto];
  
          //valoreLetto contains a value like X'FFD8FFE000....', from the blob representation of an image
  
          artXML.photo = [UIImage imageWithData:aData];
      }
  
  }
  
  @end
  
  @interface NSData (ParserXmlArticoli)
  
  -(id)dataWithBase64EncodedString:(NSString *)string;   
  -(NSString *)base64Encoding;
  @end
  
  
  static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  
  
  @implementation NSData (ParserXmlArticoli)
  
  -(id)dataWithBase64EncodedString:(NSString *)string;
  {
  
      if (string == nil)
          [NSException raise:NSInvalidArgumentException format:nil];
      if ([string length] == 0)
          return [NSData data];
  
      static char *decodingTable = NULL;
      if (decodingTable == NULL)
      {
  
          decodingTable = malloc(256);
          if (decodingTable == NULL)
              return nil;
          memset(decodingTable, CHAR_MAX, 256);
          NSUInteger i;
          for (i = 0; i &lt; 64; i++)
              decodingTable[(short)encodingTable[i]] = i;
      }
  
      const char *characters = [string cStringUsingEncoding:NSASCIIStringEncoding];
      if (characters == NULL)  
          return nil;
      char *bytes = malloc((([string length] + 3) / 4) * 3);
      if (bytes == NULL)
          return nil;
      NSUInteger length = 0;
  
      NSUInteger i = 0;
      while (YES)
  
      {
  
          char buffer[4];
          short bufferLength;
          for (bufferLength = 0; bufferLength &lt; 4; i++)
          {
  
              if (characters[i] == '\0')
                  break;
              if (isspace(characters[i]) || characters[i] == '=')
                  continue;
              buffer[bufferLength] = decodingTable[(short)characters[i]];
              if (buffer[bufferLength++] == CHAR_MAX)      
              {
  
                  free(bytes);
                  return nil;
  
              }
  
          }
  
          if (bufferLength == 0)
              break;
          if (bufferLength == 1)
  
          {
  
              free(bytes);
              return nil;
  
          }
  
          bytes[length++] = (buffer[0] &lt;&lt; 2) | (buffer[1] &gt;&gt; 4);
          if (bufferLength &gt; 2)
              bytes[length++] = (buffer[1] &lt;&lt; 4) | (buffer[2] &gt;&gt; 2);
          if (bufferLength &gt; 3)
              bytes[length++] = (buffer[2] &lt;&lt; 6) | buffer[3];
  
      }
  
      realloc(bytes, length);
      return [NSData dataWithBytesNoCopy:bytes length:length];
  
  }
  
  -(NSString *)base64Encoding;
  
  {
  
  if ([self length] == 0)
          return @"";
  
      char *characters = malloc((([self length] + 2) / 3) * 4);
      if (characters == NULL)
          return nil;
      NSUInteger length = 0;
  
      NSUInteger i = 0;
      while (i &lt; [self length])
  
      {
  
                  char buffer[3] = {0,0,0};
          short bufferLength = 0;
          while (bufferLength &lt; 3 &amp;&amp; i &lt; [self length])
              buffer[bufferLength++] = ((char *)[self bytes])[i++];
  
          characters[length++] = encodingTable[(buffer[0] &amp; 0xFC) &gt;&gt; 2];
          characters[length++] = encodingTable[((buffer[0] &amp; 0x03) &lt;&lt; 4) | ((buffer[1] &amp; 0xF0) &gt;&gt; 4)];
          if (bufferLength &gt; 1)
              characters[length++] = encodingTable[((buffer[1] &amp; 0x0F) &lt;&lt; 2) | ((buffer[2] &amp; 0xC0) &gt;&gt; 6)];
          else characters[length++] = '=';
          if (bufferLength &gt; 2)
              characters[length++] = encodingTable[buffer[2] &amp; 0x3F];
          else characters[length++] = '=';
  
      }
  
      return [[[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES] autorelease];
  
  }
  
  @end
  </code></pre>