Resolved NSXMLParser fails after first element?

Discussion in 'Mac Programming' started by ArtOfWarfare, Jul 17, 2012.

  1. ArtOfWarfare, Jul 17, 2012
    Last edited: Jul 17, 2012

    macrumors 604


    Nov 26, 2007
    Here's my code that creates the parser:

    - (TSManager*)initWithRootNode:(Ogre::SceneNode*)node level:(NSString*)theLevelID
        if (self = [super init])
            rootNode[2] = node;
            levelID = theLevelID;
            [b]NSString* firstFilePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"Contents/Resources/media/levels/%@00.xml", theLevelID]];
            [self loadFile:[NSURL fileURLWithPath:firstFilePath]];[/b]
        return self;
    - (void)loadFile:(NSURL*)URL
        NSLog(@"Attempting to load a file: %@", [URL description]);
        NSXMLParser* parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
        parser.delegate = self;
        [parser parse];
        //[parser release];
    - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
        NSLog(@"Error occured: %@", [parseError localizedDescription]);
    - (void)parserDidStartDocument:(NSXMLParser *)parser
    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
        NSLog(@"Finished element.");
    - (void)parser:(NSXMLParser*)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
        NSLog(@"Parser did start element.");
        if ([elementName isEqualToString:@"star"])
            float X = [[attributeDict objectForKey:@"X"] floatValue];
            float Y = [[attributeDict objectForKey:@"Y"] floatValue];
            float Z = [[attributeDict objectForKey:@"Z"] floatValue];
            star[starCount] = [[TSStar alloc] initAtX:X Y:Y Z:Z relativeTo:rootNode[2]];
    Here's the log output I'm getting:
    2012-07-17 19:34:41.542 TwiStar[42854:303] Attempting to load a file: file://localhost/Users/Taylor/Library/Developer/Xcode/DerivedData/TwiStar-avyztnbwsppjwyfneuwcywqgnhib/Build/Products/Debug/
    2012-07-17 19:34:41.543 TwiStar[42854:303] Started.
    2012-07-17 19:34:41.543 TwiStar[42854:303] Parser did start element.
    2012-07-17 19:34:41.545 TwiStar[42854:303] Finished element.
    2012-07-17 19:34:41.547 TwiStar[42854:303] Error occured: The operation couldn’t be completed. (NSXMLParserErrorDomain error 5.)
    Here's the contents of my XML file (which is also attached to my post - I added the .txt to the end just so the forums would let me upload it. The extension is just .xml in my project.)
    <?xml version="1.0" encoding="UTF-8"?>
    <star X="0" Y="12" Z="-50"> </star>
    <star X="6" Y="10.4" Z="-100"> </star>
    <star X="10.4" Y="6" Z="-150"> </star>
    <star X="12" Y="0" Z="-200"> </star>
    <star X="10.4" Y="-6" Z="-250"> </star>
    <star X="6" Y="-10.4" Z="-300"> </star>
    <star X="0" Y="-12" Z="-350"> </star>
    <star X="-6" Y="-10.4" Z="-400"> </star>
    <star X="-10.4" Y="-6" Z="-450"> </star>
    <star X="-12" Y="0" Z="-500"> </star>
    <star X="-10.4" Y="6" Z="-550"> </star>
    <star X="-6" Y="10.4" Z="-600"> </star>
    <star X="-3" Y="5.2" Z="-650"> </star>
    <star X="0" Y="0" Z="-700"> </star>
    Any ideas for why it's able to read the first attribute just fine but then fails to read the others?

    Edit: T.T - Solved already, again. I removed all the closing star tags and it worked fine... now the question is, why? It seems to me that my XML file was properly formatted before, and now it is not?

    Edit 2X: I added <main> before my first <star [...]>, put the </star>'s back, and added </main> after the last </star>... and now it continues to work. So, I guess this is resolved, seconds after I made it and before anyone responded.

    Still, I guess if anyone googles the title in the future, they'll see the solution.

    Attached Files:

  2. macrumors 604

    Aug 9, 2009
    Without looking at your code, that's not a well-formed XML document. A well-formed XML document must have a single root element. Yours doesn't.

    A likely interpretation of your XML is the first star element is being interpreted as the single root element. It has attributes (as you noted). There are no other valid elements after that, because as an XML document, everything after the first star element is malformed (not enclosed within the single root element).
  3. macrumors G5


    Nov 25, 2005
    Your xml file should be something like ths:
    <?xml version="1.0" encoding="UTF-8"?>
      <star X="0" Y="12" Z="-50"> </star>
      <star X="6" Y="10.4" Z="-100"> </star>
    So why did it work with the </star> removed?
    Because the parser thought you had a "star" element with attributes x,y,z and an element of type "star" which in turn has an element of type star and so on... The error would only be found at the end of the document when all the closing tags are missing.

Share This Page