An analytic method of the IOS XML system's self-bringing

Source: Internet
Author: User

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

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.