NSData *xmlData = [[NSData alloc]initWithContentsOfFile:[NSString stringWithFormat:@"%@/",[[NSBundle mainBundle] resourcePath]]]; //2. Convert xmlData into xml document GDataXMLDocument *xmlDocument = [[GDataXMLDocument alloc]initWithData:xmlData options:0 error:nil]; //3.Get node GDataXMLElement *rootElement = ; //4. Get child nodes/*NSArray *elementArray = ; //Get the classone node GDataXMLElement *classoneElement = [elementArray firstObject]; for(GDataXMLElement *element in) { //Get the text value of the current node NSLog(@"%@",); if (>0) { GDataXMLElement *infoElement = [ lastObject]; //Get all attributes and attribute values of the node NSArray *attArray = ; for (GDataXMLElement *attElement in attArray) { //Get the attribute name and attribute value NSLog(@"%@-%@",,); } } } */ //Fuzzy search NSArray *eArray = [rootElement nodesForXPath:@"//info" error:nil]; NSLog(@"%@",eArray);
iPhone development parsing xml NSData
For specific content, please refer to the code analysis below:
xmlThe string is: <users> <user name="hoge" age="20" /> <user name="fuga" age="30" /> </users> //Parse button event-(IBAction)btnXml { NSString* str= [uitextview1 text]; //Convert string to data NSData *xmlData = [str dataUsingEncoding: NSUTF8StringEncoding]; //The parsing of xml requires the use of the NSXMLParser class, first declare an NSXMLParser object //Start the analysis NSXMLParser* xmlRead = [[NSXMLParser alloc] initWithData:xmlData];//Initialize the NSXMLParser object [xmlRead setDelegate:self];//Set the parsing method proxy for NSXMLParser object [xmlRead parse];//Calling the agent to parse the NSXMLParser object to see if the parsing is successful} // parser, read content from between two nodes- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { //NSLog(@"%@",string); } //Get the value at the end of the node- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { //NSLog(@"%@",elementName); } //Get the value of the node head- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { //NSLog(@"%@",elementName); if([elementName isEqualToString: @"user"]) { NSString* strName=[attributeDict valueForKey:@"name"]; NSString* strAge=[attributeDict valueForKey:@"age"]; NSString* strUser=; strUser=[strUser stringByAppendingFormat:@"Name: %@, Age: %@\n",strName,strAge]; [uitextview2 setText:strUser]; //NSLog(@"Name: %@,Age: %@", strName, strAge); } }