Five proxy methods used by XML document parsing
1. document parsing begins parserdidstartdocument:
2. Document parsing end Parserdidenddocument:
3. Document parsing elements start Didstartelement:
4. Document parsing element End Didendelement:
5. Parsing the contents of a document Element parser Foundcharacters:
#import<UIKit/UIKit.h>@interfaceViewcontroller:uiviewcontroller<nsxmlparserdelegate>/** * Global collection for adding dictionaries*/@property (strong,nonatomic) Nsmutablearray*arr;/** * Display the corresponding element name and value values*/@property (strong,nonatomic) nsmutabledictionary*dic;/** * On the surface, str is actually a value in the dictionary.*/@property (strong,nonatomic) NSString*str;@end#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller//The system comes with XML file parsing- (void) viewdidload{[Super Viewdidload]; //specifying an XML fileNSString *path=[[nsbundle Mainbundle] Pathforresource:@" Person"OfType:@"XML"]; //Convert to data ObjectNSData *data=[NSData Datawithcontentsoffile:path]; //specify an initial value for parserNsxmlparser *parser=[[Nsxmlparser alloc] initwithdata:data]; Parser.Delegate=Self ; BOOL Bol=[parser Parse]; //returns the result of the parseNSLog (@"Bol is%d", BOL); }/** * Five methods used by the agent **//** * Document parsing begins startdocument * * @param parser*/-(void) Parserdidstartdocument: (Nsxmlparser *) parser{//InitializeSelf.arr=[Nsmutablearray array]; }/** * Document parsing End enddocument * * @param parser*/-(void) Parserdidenddocument: (Nsxmlparser *) parser{//contents of the output collectionNSLog (@"%@", Self.arr);}/** * Document parsing element starts startelement * * @param parser Resolved Object * @param elementname element name * @param NamespaceURI naming Space * @param description of QName * @param Dictionary of attributedict Properties*/-(void) Parser: (Nsxmlparser *) parser didstartelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) QName attributes: (nsdictionary<nsstring *,nsstring *> *) attributedict{//Locate the user element in the document start initializing dictionary dic if([ElementName isequaltostring:@"User"]) { //to add a property element to the dictionarySelf.dic=[Nsmutabledictionary dictionary]; [Self.dic setdictionary:attributedict]; }}/** Document parsing element End EndElement * * @param parser Resolved Object * @param elementname element name * @param NamespaceURI namespace * @param description information of QName * @param Dictionary of attributedict Properties*/-(void) Parser: (Nsxmlparser *) parser didendelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) qname{//The element is added to the dictionary only when the keyword matches the name or age. if([ElementName isequaltostring:@"name"]|| [ElementName isequaltostring:@" Age"]) { //dic already contains 3 key-value pairs[Self.dic setObject:self.str forkey:elementname]; } //adding a dictionary to arr is not available until the User element label is found: DiC Else if([ElementName isequaltostring:@"User"]) {[Self.arr addObject:self.dic]; }}/** * Parse the contents of a file Element foundcharacters * * @param parser Parse Object * @param text content displayed by string*/-(void) Parser: (Nsxmlparser *) parser foundcharacters: (NSString *)string{ //assigning a local string to strSelf.str=string;}@end
An analytic method of the IOS XML system's self-bringing