DOM parsing for xml data

Source: Internet
Author: User

DOM parsing for xml data

Third-party tools required

#import GDataXMLNode.h
// Lazy loading. Initialization is performed during use. // properties are relatively lazy loading. You need to override the getter method-(NSMutableArray *) xmlDOMArray {if (_ xmlDOMArray = nil) {_ xmlDOMArray = [NSMutableArray array];} return _ xmlDOMArray ;}
@property (nonatomic, strong) NSMutableArray *xmlDOMArray;
-(IBAction) XMLDOMButtonDidClicked :( UIButton *) sender {// 1. Obtain the path NSString * path of the xml file = [[NSBundle mainBundle] pathForResource: @ myData ofType: @ xml]; // 2. Obtain the file content from the path as NSData object NSData * data = [[NSData alloc] initWithContentsOfFile: path]; // 3. initialize a GDataXMLDocument Object Based on data first (introduce a third-party class library and) GDataXMLDocument * xmlDocument = [[GDataXMLDocument alloc] initWithData: data options: 0 error: nil]; // 4. Get the root node GDataXMLElement * rootElement = [xmlDocument rootElement]; // 5. Start parsing NSArray * studentArray = [rootElement elementsForName: @ student]; for (GDataXMLElement * element in studentArray) {// get the name node only one node GDataXMLElement * nameElement = [[element elementsForName: @ name] firstObject]; // get the sex node GDataXMLElement * sexElement = [[element elementsForName: @ sex] firstObject]; // get the phone node GDataXMLElement * phoneElement = [[element elementsForName: @ phone] firstObject]; // create a modal StudentModal * modal = [[StudentModal alloc] init]; // retrieve the node value modal. name = [nameElement stringValue]; modal. sex = [sexElement stringValue]; modal. phoneNumber = [phoneElement stringValue]; // The _ xmlDOMArray [self. xmlDOMArray addObject: modal];} for (StudentModal * student in self. xmlDOMArray) {NSLog (@ % @, student. name, student. sex, student. phoneNumber);} [self. xmlDOMArray removeAllObjects];}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.