NSXMLParser delegates Handling Attributes

2019-08-13 03:01发布

I am currently trying to get my parser delegates to work with a custom NSObject I have created for the attributes of the xml I am receiving..

This is the XML that is being read in to my parser delegates

<Rows>
<Row SKATERID="706" MANUFACTURER="GAZ" ISFACT="F" ISSKATE="F"/>
<Row SKATERID="318" MANUFACTURER="MAN" ISFACT="F" ISSKATE="T"/>
//...
</Rows>

This is what I have for my -parser:didStartElement:namespaceURI:qualifiedName:attributes: method:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ( [elementName isEqualToString:@"Row"]) {

        manufactureNSObject = [[ManufactureNSObject alloc] init];

        manufactureNSObject.ISFACT = [attributeDict objectForKey:@"ISFACT"];
         manufactureNSObject.ISSKATE = [attributeDict objectForKey:@"ISSKATE"];
         manufactureNSObject.MANUFACTURER = [attributeDict objectForKey:@"MANUFACTURER"];
         manufactureNSObject.SKATERID = [attributeDict objectForKey:@"SKATERID"];

        NSLog(@"%@ %@ %@ %@", manufactureNSObject.ISFACT, manufactureNSObject.ISSKATE, manufactureNSObject.MANUFACTURER, manufactureNSObject.SKATERID);
    }    
} 

My NSLog prints out all of the correct values fine, the next part that I am stuck on is the - parser:didEndElement:namespaceURI:qualifiedName: method, nothing apart from the didEndElement is coming through correctly... this is what I have for that method.

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    NSLog(@"%@", elementName);
    NSLog(@"%@", namespaceURI);
    NSLog(@"%@", qName);
}

This is what my output looks like from the NSLogs.

2011-10-13 10:04:16.529 Code[52605:207] F F GAZ 76
2011-10-13 10:04:16.531 Code[52605:207] Row
2011-10-13 10:04:16.535 Code[52605:207] (null)
2011-10-13 10:04:16.537 Code[52605:207] (null)
2011-10-13 10:04:16.537 Code[52605:207] F T MAN 38
2011-10-13 10:04:16.538 Code[52605:207] Row
2011-10-13 10:04:16.539 Code[52605:207] (null)
2011-10-13 10:04:16.540 Code[52605:207] (null)

I specifically would like help with trying to complete the second method and get this all working correctly.

All the help so far has been greatly appreciated.

3条回答
女痞
2楼-- · 2019-08-13 03:21

Create an interface NSMutableString that you can pass the string from parser:foundCharacters into. From there, implement a similar structure as you did in parser:didStartElement: only this time, you'll use that NSMutableString and pass its value into the object that is represented by the element being closed.

查看更多
成全新的幸福
3楼-- · 2019-08-13 03:35

For the type of xml file you are parsing, the

parser:foundCharacters:
is never called. This delegate method is called in such cases:

<mytag>this is the tag content</mytag>

while in your case only attributes are present and no tag content, that is:

<mytag attr1="value1" attr2="value2"></mytag>

As far as the other method:

parser:didEndElement:

its implementation is up to you. E.g. if you store an element inside a temporary variable then this method is useful to copy this variable inside another data structure.

查看更多
爷、活的狠高调
4楼-- · 2019-08-13 03:36

Just incase someone checks this page out in the future I have found out how to do this

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@",@"MANUFACTURER",cell.textLabel.text];
            NSArray *filterArray = [myDataArray filteredArrayUsingPredicate:predicate];
            [[self delegate] setManufactureSearchFields:filterArray withIndexPath:indexPath]; //This is where I pass the value back to the mainview

predicate are your friend.. and they are also good at isolating the values you want from a dictionary...

BOOM!

查看更多
登录 后发表回答