Lefora Free Forum
login join
Loading
826 views

parsing xml

Page 1
1–10
regular - member
54 posts

I have an nsstring that contains a bit of xml. I need to parse it. I have successfully used NSXMLParser to parse an xml page grabbed from the web. But this is a string and the parser wants NSData not a string, ala NSXMLParser *parser = [[NSXMLParser alloc] initWithData:someData]; So how can make a string into data?

Thank you very much,
                                     Todd

?
288 posts

Todd

There's an instance method in the NSString class to do this.

- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding

XML is very often encoding as UTF8.  I think the encoding is usually defined in the <!....!> nonesense at the front of the XML file.  I say nonesense, because XML doesn't make any sense to me.  I've been waiting years for the little boy in the crowd to shout "The king is in the altogether" about XML. http://www.w3schools.com/xml/xml_encoding.asp

I've never had to deal with XML in Cocoa.  I expect NSXMLParser is fine. There are a number of good looking libraries here:  http://www.cocoadev.com/index.pl?ObjectLibrary


__________________
?
288 posts



Todd

There's an instance method in the NSString class to do this.

- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding


XML is very often encoding as UTF8.  I think the encoding is usually defined in the <?....?> nonesense at the front of the XML file.  I say nonesense, because XML doesn't make any sense to me.  I've been waiting years for the little boy in the crowd to shout "The king is in the altogether" about XML. http://www.w3schools.com/xml/xml_encoding.asp

I've never had to deal with XML in Cocoa.  I expect NSXMLParser is fine. There are a number of good looking libraries here:  http://www.cocoadev.com/index.pl?ObjectLibrary

__________________
regular - member
54 posts

Hi there Robin! Darn, I tried  NSData *data = [aXMLString dataUsingEncoding:NSUTF8StringEncoding]; and it is not working correctly. So the problem is probably somewhere else. Back to the drawing board. Since we spoke last I have been writing a video metadata tagger, for mp4 videos for iTunes and have maneged to write about 3000 lines of code. This XML bit was supposed to be the easy part and left it till last, turns out it not the simplest part.....who woulda thunk it? Should have known better.....I will check out the other libraries and maybe I can kludge something together. have a great night.

Todd

regular - member
54 posts

Thank you robin, I tried the NSData *data = [aXMLString dataUsingEncoding:NSUTF8StringEncoding]; with the xml from the web and it works. I guess I need to figure out what is wrong with my failing xml. Little by little.......

?
288 posts


My opinion of XML is unprintable.  However it exists and has to be suffered - and I think that's where you are now.  Patience, you'll get there.  A good night's sleep with help a lot.

I recently wanted to read/write some data.  I wanted it to be possible for the user to edit the data with TextEdit.  After working a little with NSKeyedArchiver (XML), I gave up and found a library on Google Code to handle JSON in Cocoa.  Very pleased with the results and how easy it was to use.

__________________
regular - member
54 posts

I cannot figure this one out. I copied the value of the string as printed in the log and put it into textedit, made it all one line and put it into a string by hand, NSString *value=@"thexml"; and then used the above string to data method and it parsed correctly. But if the string is used without my intervention it fails to parse. The string comes from a commandLine utility to get metadata from videos called atomicParsley. Here is what I am using and I have marked what works and fails. The only thing I can think is something to do with encoding....

Here is the xml from the nslog that fails:
The Value: contains: <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.1"><dict><key>copy-warning</key><string></string><key>cast</key><array><dict><key>name</key><string>Michael Bell,Corey Burton,Chris Latta,Mary McDonald-Lewis,Bill Ratner,Hal Rayle,Neil Ross</string></dict></array><key>screenwriters</key><array><dict><key>name</key><string>Buzz Dixon</string></dict></array><key>producers</key><array><dict><key>name</key><string>John Ahern,Chuck Downs,Jim Duffy,Dan Thompson,Art Vitello,Sam Weiss</string></dict></array></dict></plist>


Thank you for any insight,
                                            Todd


        /* WORKS BOF */
        //NSString *stringToURL=@"http://thetvdb.com/api/aPRIVATE API #/series/73255/en.xml";
        //NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:stringToURL]];
        //NSString *agentString = @"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1";
        //[request setValue:agentString forHTTPHeaderField:@"User-Agent"];
        //NSError *error=nil;
        //NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
        //if (error) NSLog(@"Connection Failed");
        /* WORKS EOF */
       
        /* WORKS BOF */
        //NSString *tagValue=@"<?xml version=\"1.0\" encoding=\"UTF-8\" ?><Data><Series><id>73255</id><Actors>|Hugh Laurie|Robert Sean Leonard|</Actors><Genre>|Drama|</Genre><IMDB_ID>tt0412142</IMDB_ID></Series></Data>";
        //NSString *tagValue=@"<?xml version=\"1.0\" encoding=\"UTF-8\"?><plist version=\"1.1\"><dict><key>copy-warning</key><string></string><key>cast</key><array><dict><key>name</key><string>Michael Bell,Corey Burton,Chris Latta,Mary McDonald-Lewis,Bill Ratner,Hal Rayle,Neil Ross</string></dict></array><key>screenwriters</key><array><dict><key>name</key><string>Buzz Dixon</string></dict></array><key>producers</key><array><dict><key>name</key><string>John Ahern,Chuck Downs,Jim Duffy,Dan Thompson,Art Vitello,Sam Weiss</string></dict></array></dict></plist>";
        //NSString *tagValue=@"<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"><plist version=\"1.1\"><dict><key>copy-warning</key><string></string><key>cast</key><array><dict><key>name</key><string>Michael Bell,Corey Burton,Chris Latta,Mary McDonald-Lewis,Bill Ratner,Hal Rayle,Neil Ross</string></dict></array><key>screenwriters</key><array><dict><key>name</key><string>Buzz Dixon</string></dict></array><key>producers</key><array><dict><key>name</key><string>John Ahern,Chuck Downs,Jim Duffy,Dan Thompson,Art Vitello,Sam Weiss</string></dict></array></dict></plist>";
        /* WORKS EOF */
       
        /* FAILS BOF */
        NSString *tagValue=newTagValue; //newTagValue is xml returned from command line.
        /* FAILS EOF */

        NSData *data = [tagValue dataUsingEncoding:NSUTF8StringEncoding];
        NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
        [parser setDelegate:self];
        // Create a new, empty bannerArray
        casts = [[NSMutableArray alloc] init];

        // Do the parse
        [parser parse];
        [parser release];

?
288 posts

Were is the xml?

curl "http://thetvdb.com/api/aPRIVATE API#/series/73255/en.xml"

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/0.7.65</center>
</body>
</html>

I agree with you - I think it's the encoding.

__________________
regular - member
54 posts

That is basically just a url I used to make sure the delegate was working, it requires an developers API key. I can successfully get xml from the web and parse it. The XML in question is coming from a video file on my HD. I will put together a more comprehensive example.

Todd

?
288 posts

Todd

If it's OK on the web, and not OK from a command-line utility, I think it has to be in the encoding.  Most command-line programs are not good with unicode.

Write the stuff from the web into a file and dump it with od -x (or whatever utility you like for dumping files).  And do the same for the code from the command-line utility.  I'm sure you'll find a significant difference. 

__________________
Page 1
1–10

Locked Topic


You must be a member to post in this forum

Join Now!