NSXMLParser Problem

Discussion in 'iOS Programming' started by tadelv, Sep 22, 2008.

  1. tadelv macrumors newbie

    Sep 20, 2008
    I am having a problem with my own application that parses a xml file using NSXMLParser, however, the file does not have a valid xml start tag. This is how it looks like:
    		<credit>Weather Underground Personal Weather Station</credit>
    and so on...
    I also get this in the console:
    Operation could not be completed. (NSXMLParserErrorDomain error 5.)
    When i try to process a different file, it works perfectly, and this other file has a xml start tag.

    I tried allready to open the file to a string and then append the start tag, but when i try to open it the response from the server is like this:
    <TITLE>500 Internal Server Error</TITLE>
    <H1>Internal Server Error</H1>
    The server encountered an internal error or
    misconfiguration and was unable to complete
    your request.<P>
    Please contact the server administrator,
     support@wunderground.com and inform them of the time the error occurred,
    and anything you might have done that may have
    caused the error.<P>
    More information about this error may be available
    in the server error log.<P>
    <ADDRESS>Apache/1.3.33 Server at b2.wunderground.com Port 80</ADDRESS>
    Any suggestions? Using NSXMLDocument perhaps? I have tried NSXMLDocument with the "initWithContentsOfURL:xmlURL options:1 << 10 error:NULL" method but it returns null at initialization...
  2. fishkorp macrumors 68020


    Apr 10, 2006
    Ellicott City, MD
    Like mentioned before, you need to specifically set each part of your object, you can't just say that the whole element found is your object. So in my didStartElement I do:

    if ([elementName isEqualToString:@"WhatYoureLookingFor"]) {
    	item = [[NSMutableDictionary alloc] init];
            propertyOne = [[NSMutableString alloc] init];
            propertyTwo = [[NSMutableString alloc] init];
    Then in the didEndElement I do this:
    if ([elementName isEqualToString:@"WhatYoureLookingFor"]) {
    	[item setObject:propertyOne forKey:@"propOneTag"];
    	[item setObject:propertyTwo forKey:@"propTwoTag"];
           // etc...
  3. grimjim macrumors member

    May 24, 2003
    I don't recommend trying to use NSXMLDocument. It's not available on the iPhone (although it will work on the simulator). That's why we're all scratching away with NSXMLParser in the first place...
  4. tadelv thread starter macrumors newbie

    Sep 20, 2008
    Ok, so here is what i've gathered so far:

    The NSXMLParserErrorDomain error 5 is actually this:
    The document ended unexpectedly.
    Available in Mac OS X v10.3 and later.
    Declared in NSXMLParser.h
    my guess is still that this is because of the missing start tag.

    Still haven't found out why the NSString way doesnt work... and have sent a mail to support@wunderground, asking them to do something about it. They have it specified in the api documentation anyway, so i guess all i have to do is sit on my a** and wait for them to fix it :D
  5. grimjim macrumors member

    May 24, 2003
    I don't fully understand what you're doing, but I think I can make some guesses that might help.

    Firstly, I don't believe that the missing start tag (by which I assume you mean the <XML> tag) has anything to do with it. I can parse the XML from this site without it. You don't need it.

    Secondly, the HTML error that you are getting back is probably due to a problem in the URL you are submitting to the site. If I submit a malformed URL, I get this error. I think that this is where your problem really lies. I suggest that you check that the request you are sending is correct. Other than that, I think I will need more info/code to help any further.
  6. tadelv thread starter macrumors newbie

    Sep 20, 2008

    I just found out i was heading in the wrong direction with the <XML> tag...:rolleyes:

    Anyway, here is the console output:
    2008-09-24 19:09:52.253 iVreme[9162:20b] lat = 46.051244, lon = 14.503061
    2008-09-24 19:09:54.535 iVreme[9162:20b] started parsing for psw
    2008-09-24 19:09:54.537 iVreme[9162:20b] Got stuff: ILJUBLJA3
    2008-09-24 19:09:54.541 iVreme[9162:20b] request = 
    2008-09-24 19:09:54.542 iVreme[9162:20b] request = http://api.wunderground.com/weatherstation/WXCurrentObXML.asp?ID=ILJUBLJA3
    And the code(with comments in slovene):
    NSMutableString *request = [[[NSMutableString alloc] init] autorelease];
    	//Parsanje, da dobimo pws kodo
    	[request appendString:@"http://api.wunderground.com/auto/wui/geo/GeoLookupXML/index.xml?query="];
    	[request appendFormat:@"%@,%@",latitude,longitude];
    	[main parseXMLForLocationAtURL:request];
    	//Parsanje vremenskega stanja
    	//cleanup :)
    	[request setString:@""];
    	NSLog(@"request = %@", request);
    	[request appendString:@"http://api.wunderground.com/weatherstation/WXCurrentObXML.asp?ID="];
    	[request appendFormat:@"%@",[main postajaPWS]];
    	NSLog(@"request = %@", request);
    [main parseXMLFileAtURL:request];
    and the parsing code:
     NSURL *xmlURL = [NSURL URLWithString:URL];
    wUnderParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
        // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
        [wUnderParser setDelegate:self];
        // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
        [wUnderParser setShouldProcessNamespaces:NO];
        [wUnderParser setShouldReportNamespacePrefixes:NO];
        [wUnderParser setShouldResolveExternalEntities:NO];
    	NSLog(@"started parsing");
        imeFertig = NO;
    	[wUnderParser parse];
    What i am trying to do here is the following:

    - get the lat,long
    - submit them to the "http://api.wunderground.com/auto/wui/geo/GeoLookupXML/index.xml?query=" to get the city
    - parse the xml to get the Personal Weather Station ID
    - reset the "request" string
    - append the Personal Weather Station ID to "http://api.wunderground.com/weatherstation/WXCurrentObXML.asp?ID=" --> it works to here
    -parse the file for weather info

    The links are correct afaik, i tried copying/pasting to safari and they work so i am a little confused here. the only difference i see now is that the last url has an .asp ending

    Thank you for your help!

Share This Page