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];}