Help solving NSXMLParser error 39 : NSXMLParserAttributeNotStartedError
Asked Answered
P

1

7

I've been searching for about two hours now trying to find out what this error is and how to solve it. Apple's documents gladly says

NSXMLParserAttributeNotStartedError Attribute is not started. Available in Mac OS X v10.3 and later. Declared in NSXMLParser.h.

but fails to provide any help whatsoever. Can anyone help me out here? Any nudge in the right direction is greatly appreciated.

In viewDidLoad, I have:

NSURL *xmlUrl = [NSURL fileURLWithPath:xmlFilepath];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlUrl];
[xmlParser setDelegate:self];
[xmlParser setShouldResolveExternalEntities:NO];
BOOL success = [xmlParser parse];

No problems with getting the xml, I can successfully read up until the root element, then it breaks.

The rest of the related code goes like:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
 if([elementName isEqualToString:@"foo"]) {
  foo = [[NSMutableArray alloc] init];
 } else if([elementName isEqualToString:@"bar"]) {
  bar = [[Bar alloc] init];
 }
}

- (void)parser:(NSXMLParser *)parser foundAttributeDeclarationWithName:(NSString *)attributeName forElement:(NSString *)elementName type:(NSString *)type defaultValue:(NSString *)defaultValue {
  if ([elementName isEqualToString:@"bar"]) {
  if ([attributeName isEqualToString:@"barAttrib1"]) {
   [bar setBarAttrib1:[defaultValue intValue]];
  } else if ([attributeName isEqualToString:@"barAttrib2"]) {
   [bar setBarAttrib2:defaultValue];
  } 
 }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
 // nothing inside the elements, just attributes
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
 if ([elementName isEqualToString:@"bar"]) {  
  [foo addObject:bar];  
  [bar release];
  bar = nil;
 }
}

Thanks in advance.

EDIT: example xml

<foo>
  <bar barAttrib1=1 barAttrib2="hello" />
  <bar barAttrib1=2 barAttrib2="world" />
</foo>
Puddling answered 2/8, 2010 at 13:11 Comment(2)
Please post the example XML tooTonne
+1 for asking a very good question.Cosmetician
S
7

If this is XML, then quotes are REQUIRED around the attribute value. Try

<foo>
  <bar barAttrib1="1" barAttrib2="hello" />
  <bar barAttrib1="2" barAttrib2="world" />
</foo>

Note the quotes around the values of barAttrib1

Stansbury answered 2/8, 2010 at 15:39 Comment(1)
arr... But of course. >.< I must be way too stressed out that I overlooked that. I'll try that once I get back to the office tomorrow. Thanks!Puddling

© 2022 - 2024 — McMap. All rights reserved.