I want to know how to parse the attributes in below xml file
<ROOT_ELEMENT><RESPONSE READ_TAG="LEVEL_LIST" RESULT="" TEXT=""/><USER USER_NAME="newadmin01" TOKEN_ID="0.766003221016982" FULL_NAME="newadmin01, newadmin01"/><DATETIME UNFORMATTED_TEXT="Aug 10 2011 12:25PM" FORMATTED_TEXT="10 Aug 12:25"/><BREADCRUMB/><LEVEL_LIST><LEVEL ID="4519" NAME="Mega Mart" CHILD_EXISTS="Y" ADD_EDIT_PRIVILEGE="Y"/></LEVEL_LIST></ROOT_ELEMENT>
here it is my parser code
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
elemName = [[NSString alloc] initWithString:elementName];
NSLog(@"element Name = %@", elementName);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([elemName isEqualToString:@"RESPONSE"]) {
if (!currentValueString) {
currentValueString = [[NSMutableString alloc] initWithCapacity:1024];
}
[currentValueString appendString:string];
}
else if ([elemName isEqualToString:@"USER"]) {
if (!currentValueString) {
currentValueString = [[NSMutableString alloc] initWithCapacity:1024];
}
[currentValueString appendString:string];
}
if ([elemName isEqualToString:@"DATETIME"]) {
if (!currentValueString) {
currentValueString = [[NSMutableString alloc] initWithCapacity:1024];
}
[currentValueString appendString:string];
}
else if ([elemName isEqualToString:@"BREADCRUMB"]) {
if (!currentValueString) {
currentValueString = [[NSMutableString alloc] initWithCapacity:1024];
}
[currentValueString appendString:string];
}
else if ([elemName isEqualToString:@"LEVEL"]) {
if (!currentValueString) {
currentValueString = [[NSMutableString alloc] initWithCapacity:1024];
}
[currentValueString appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elemName isEqualToString:@"RESPONSE"]) {
[tableDataArray addObject:currentValueString];
[currentValueString release];
currentValueString = nil;
[elemName release];
elemName = nil;
}
else if ([elemName isEqualToString:@"USER"]) {
[tableDataArray addObject:currentValueString];
[currentValueString release];
currentValueString = nil;
[elemName release];
elemName = nil;
}
else if ([elemName isEqualToString:@"DATETIME"]) {
[tableDataArray addObject:currentValueString];
[currentValueString release];
currentValueString = nil;
[elemName release];
elemName = nil;
}
else if ([elemName isEqualToString:@"BREADCRUMB"]) {
[tableDataArray addObject:currentValueString];
[currentValueString release];
currentValueString = nil;
[elemName release];
elemName = nil;
}
else if ([elemName isEqualToString:@"LEVEL"]) {
[tableDataArray addObject:currentValueString];
[currentValueString release];
currentValueString = nil;
[elemName release];
elemName = nil;
}
}
Look at the NSXMLParserDelegate's message declaration
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
It has an attributeDict for the element you currently parse.
Iterate over the key-value pairs and deal with them as you like.
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
elemName = [[NSString alloc] initWithString:elementName];
NSLog(@"element Name = %@", elementName);
NSEnumerator *keyEnumerator = attributeDict.keyEnumerator()
// for each element the parser encounters:
// create a new dictionary to hold the attributes
NSMutableDictionary *myAttributes = [[NSMutableDictionary alloc] init];
// go through each attribute for the elementName tag
while ( (id aKey = [keyEnumerator nextObject]) )
// and store the attribute to the dictionary by copying over the values
[myAttributes setObject:[attributesDict objectForKey:aKey] forKey:aKey];
// after we caught all attributes and copied them into our own data structure
// put them into an instance dicionary to get a structure which holds
// all attributes for any element tag we encounter
// accessible by the elementName as key
[self.attributesByElementDictionary setObject:myAttributes forKey:elementName];
[myAttributes release];
}
Practically this would produce a dictionary for the - example: - RESPONSE tag: The dictionary would look like:
{
"READ_TAG": "LEVEL_LIST",
"RESULT": "",
"TEXT": ""
}
For a more general solution you would not use a flat dictionary but rather form some kind of tree structure. This is a little bit more work and more robust - but this little example should give you the idea to extract the attributes from a tag and process them afterwards
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.