Again to parse the XML, and some understanding, if there is wrong place, please point to the younger brother, thank you!
[HTML]View Plaincopy
- <? XML version= "1.0" encoding="UTF-8"?>
- <result>
- <meeting addr="203">
- <creator> Zhang </creator>
- <member>
- <name> Zhang Yi </name>
- <age>20</age>
- </member>
- <member>
- <name> Zhang San </name>
- <age>21</age>
- </member>
- <member>
- <name> four </name>
- <age>22</age>
- </member>
- </Meeting>
- <meeting addr="204">
- <creator> Lee </creator>
- <member>
- <name> Lee </name>
- <age>20</age>
- </member>
- <member>
- <name> Lie triple </name>
- <age>21</age>
- </member>
- <member>
- <name> John Doe </name>
- <age>22</age>
- </member>
- </Meeting>
- </result>
1. Getting data from an XML file: storing it in a string
[CPP]View Plaincopy
- A bundle is a directory that contains the resources that the program will use
- NSString *path=[[nsbundle Mainbundle] pathforresource:@"test" oftype:@"xml"];
- NSString *_xmlcontent=[[nsstring alloc] Initwithcontentsoffile:path encoding:nsutf8stringencoding Error:nil];
Use Nsxmlparser to implement parsing:
[Plain]View Plaincopy
- Brief description of Nsxmlparser analysis
- 1. Is the Sax method parsing
- 2. Need to create a Nsxmlparser instance (alloc)
- and create the parser (Initwithdata:)
- Defining a delegate for the parser (setdelegate:)
- Running the parser (parser)
- ++++++ when parser initializes and executes the parse statement ([parser parse]), the program jumps to the proxy method and walks to the first proxy method ++++++
- 3. This parsing method is implemented using its proxy nsxmlparserdelegate
- The first proxy method: Begins processing XML data, which iterates through the entire XML, identifying the element node name
- -(void) Parser: (Nsxmlparser *) parser didstartelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) QName attributes: (nsdictionary *) attributedict;
- The second proxy method is to get the information data stored in the text node
- -(void) Parser: (Nsxmlparser *) parser foundcharacters: (NSString *) string;
- Third Proxy method: stores information obtained from the second proxy method
- -(void) Parser: (Nsxmlparser *) parser didendelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) qName
- This is the process of parsing, and in this process will repeatedly execute the three proxy method, until the traversal is completed
- Other than that:
- Parse the method that starts execution
- -(void) Parserdidstartdocument: (Nsxmlparser *) parser;
- Parsing the way to end execution
- -(void) Parserdidenddocument: (Nsxmlparser *) parser;
- This method is executed when a parsing error occurs.
- -(void) Parser: (Nsxmlparser *) parser parseerroroccurred: (Nserror *) parseerror;
Parsing begins:
2. Preparatory work:
[CPP]View Plaincopy
- Nsxmlparser *parse=[[nsxmlparser alloc] initwithdata:[_xmlcontent datausingencoding:nsutf8stringencoding];
- [Parse setdelegate:self];
- [Parse parse];
- [Parse release];
3. Three proxy methods are implemented specifically:
[CPP]View Plaincopy
- First Proxy method:
- -(void) Parser: (Nsxmlparser *) parser didstartelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI qualifiedname: (NSString *) QualifiedName attributes: (nsdictionary *) attributedict
- {
- //Determine if it is meeting
- if ([elementname isequaltostring:@"Meeting"]) {
- //Judge attribute node
- if ([Attributedict objectforkey:@"addr"]) {
- //Gets the value in the attribute node
- NSString *addr=[attributedict objectforkey:@"addr"];
- }
- }
- //Judge member
- if ([ElementName isequaltostring:@"Member"]) {
- NSLog (@"Member"]);
- }
- }
- A second Proxy method:
- -(void) Parser: (Nsxmlparser *) parser foundcharacters: (NSString *) string
- {
- //Get the data in the text node because the following method saves the data obtained here, so define a global variable (a modifiable string)
- //nsmutablestring *element = [[nsmutablestring alloc]init];
- //The assignment is null here in order to empty the last assignment
- [Element setstring:@"];
- [Element appendstring:string]; //string is the value of the text node that is obtained, as long as the text node is fetched (including line breaks) and then judged in the next method .
- }
- A third proxy method:
- -(void) Parser: (Nsxmlparser *) parser didendelement: (NSString *) elementname
- NamespaceURI: (NSString *) NamespaceURI qualifiedname: (NSString *) QName {
- NSString *str=[[nsstring alloc] initwithstring:element];
- if ([ElementName isequaltostring:@"creator"]) {
- NSLog (@"creator=%@", str);
- }
- if ([elementname isequaltostring:@"name"]) {
- NSLog (@"name=%@", str);
- }
- if ([ElementName isequaltostring:@"age"]) {
- NSLog (@"age=%@", str);
- }
- [STR release];
- }
Attention:
When parsing XML data,
The first proxy method is executed whenever an element node is encountered, and if there is an attribute node, the value can be obtained directly in this method;
The second proxy method is executed whenever a text node is encountered, gets the value in the text node, and then distinguishes it from the third method.
This is also obtained if it is a newline character, because the newline character is also a text node, but the newline character after the end of an element node is not acquired.
For example: (line break 1)
<li> text Nodes </li>
(Line break 2)
there is a newline character before and after the element node, and only the newline character 1 is obtained, not the line break 2.
4. Handling Error: Printing error
[CPP]View Plaincopy
- -(void) Parser: (Nsxmlparser *) parser parseerroroccurred: (Nserror *) parseerror{
- NSLog (@"%@", [parseerror description]);
- }
iOS growth path-XML parsing with Nsxmlparser