views:

1414

answers:

5

I am trying to parse a URL with an & in the URL:

ViewArticle.dbml?DB_OEM_ID=1800&ATCLID=3664162

..but using NSXMLParser, all i get is 1800ATCL. It completely ignores the &.

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if (qName) {
        elementName = qName;
    }


    if ([elementName isEqualToString:@"title"]) {
        self.contentOfCurrentNewsProperty = [NSMutableString string];
    }else if ([elementName isEqualToString:@"link"]){
        self.contentOfCurrentNewsProperty = [NSMutableString string];
    }else {
        self.contentOfCurrentNewsProperty = nil;
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{     
    if (qName) {
        elementName = qName;
    }

    _currentElement = elementName;

    if ([elementName isEqualToString:@"title"]) {
        self.currentNewsObject.title = self.contentOfCurrentNewsProperty;
    } else if ([elementName isEqualToString:@"link"]){
        self.currentNewsObject.link = self.contentOfCurrentNewsProperty;
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if (self.contentOfCurrentNewsProperty) {
        [self.contentOfCurrentNewsProperty appendString:string];
    }
}

Any ideas? Thanks

Edit: I did a little more testing and the line <?xml version="1.0" encoding="windows-1252"?> is what is messing it up, but this is embedded in the xml file, any way to get around it?**

A: 

Perhaps you could override the encoding using setCharacterEncoding

setCharacterEncoding:

Sets the character encoding of the receiver to encoding,

- (void)setCharacterEncoding:(NSString *)encoding

Parameters:

  • encoding

    A string that specifies an encoding; it must match the name of an IANA character set. See http://www.iana.org/assignments/character-sets for a list of valid encoding specifiers.

    Typically the encoding is specified in the XML declaration of a document that is processed, but it can be set at any time. If the specified encoding does not match the actual encoding, parsing of the document might fail.

dbr
+1  A: 

(dbr: the original poster is using NSXMLParser, not NSXMLDocument, so there is no setCharacterEncoding method for him to override.)

To the OP: To test your situation, I wrote a simple command-line utility that uses NSXMLParser to parse an XML document with the <?xml version="1.0" encoding="windows-1252"?> processing instruction. The parsing always fails, and the error code is 31, which corresponds to the enumeration NSXMLParserUnknownEncodingError.

To solve this problem, you may have to pre-process your input data. One way of doing this might be to use [NSString stringWithCString:yourCstring encoding:NSWindowsCP1252StringEncoding], where yourCstring is a standard C char* containing your XML. You can then remove the <?xml version="1.0" encoding="windows-1252"?> processing instruction, convert the NSString to an NSData, and pass it into the NSXMLParser.

erikprice
+1  A: 

thanks with the y'all's help i was able to get it going using

    NSData *myData = [NSData dataWithContentsOfURL:URL];
    NSString *myStr = [[NSString alloc] initWithData:myData encoding:NSWindowsCP1252StringEncoding];
    myStr = [myStr stringByReplacingOccurrencesOfString:@"encoding=\"windows-1252\"" withString:@""];
NSLog(@"my str is %@", myStr);

NSData* aData = [myStr dataUsingEncoding:NSUTF8StringEncoding];

NSXMLParser *parser = [[NSXMLParser alloc] initWithData:aData];

all works now, once again thanks.

A: 

This was awesome guys!!! It saved my night as I had been racking up my brains and that too this being my second IPhone App this answer was a real lifesaver.

A: 

In my application I am trying to parse a url for some url it's working for some other like below

"http://rss.cnn.com/rss/edition.rss" give the following error

I got NSXMLSpaceRequiredError and error code is 65

how to solve this