iOS growth path-XML parsing with Nsxmlparser

Source: Internet
Author: User

Again to parse the XML, and some understanding, if there is wrong place, please point to the younger brother, thank you!

[HTML]View Plaincopy
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <result>
  3. <meeting addr="203">
  4. <creator> Zhang </creator>
  5. <member>
  6. <name> Zhang Yi </name>
  7. <age>20</age>
  8. </member>
  9. <member>
  10. <name> Zhang San </name>
  11. <age>21</age>
  12. </member>
  13. <member>
  14. <name> four </name>
  15. <age>22</age>
  16. </member>
  17. </Meeting>
  18. <meeting addr="204">
  19. <creator> Lee </creator>
  20. <member>
  21. <name> Lee </name>
  22. <age>20</age>
  23. </member>
  24. <member>
  25. <name> Lie triple </name>
  26. <age>21</age>
  27. </member>
  28. <member>
  29. <name> John Doe </name>
  30. <age>22</age>
  31. </member>
  32. </Meeting>
  33. </result>


1. Getting data from an XML file: storing it in a string

[CPP]View Plaincopy
    1. A bundle is a directory that contains the resources that the program will use
    2. NSString *path=[[nsbundle Mainbundle] pathforresource:@"test" oftype:@"xml"];
    3. NSString *_xmlcontent=[[nsstring alloc] Initwithcontentsoffile:path encoding:nsutf8stringencoding Error:nil];


Use Nsxmlparser to implement parsing:

[Plain]View Plaincopy
  1. Brief description of Nsxmlparser analysis
  2. 1. Is the Sax method parsing
  3. 2. Need to create a Nsxmlparser instance (alloc)
  4. and create the parser (Initwithdata:)
  5. Defining a delegate for the parser (setdelegate:)
  6. Running the parser (parser)
  7. ++++++ when parser initializes and executes the parse statement ([parser parse]), the program jumps to the proxy method and walks to the first proxy method ++++++
  8. 3. This parsing method is implemented using its proxy nsxmlparserdelegate
  9. The first proxy method: Begins processing XML data, which iterates through the entire XML, identifying the element node name
  10. -(void) Parser: (Nsxmlparser *) parser didstartelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) QName attributes: (nsdictionary *) attributedict;
  11. The second proxy method is to get the information data stored in the text node
  12. -(void) Parser: (Nsxmlparser *) parser foundcharacters: (NSString *) string;
  13. Third Proxy method: stores information obtained from the second proxy method
  14. -(void) Parser: (Nsxmlparser *) parser didendelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) qName
  15. This is the process of parsing, and in this process will repeatedly execute the three proxy method, until the traversal is completed
  16. Other than that:
  17. Parse the method that starts execution
  18. -(void) Parserdidstartdocument: (Nsxmlparser *) parser;
  19. Parsing the way to end execution
  20. -(void) Parserdidenddocument: (Nsxmlparser *) parser;
  21. This method is executed when a parsing error occurs.
  22. -(void) Parser: (Nsxmlparser *) parser parseerroroccurred: (Nserror *) parseerror;


Parsing begins:

2. Preparatory work:

[CPP]View Plaincopy
    1. Nsxmlparser *parse=[[nsxmlparser alloc] initwithdata:[_xmlcontent datausingencoding:nsutf8stringencoding];
    2. [Parse setdelegate:self];
    3. [Parse parse];
    4. [Parse release];


3. Three proxy methods are implemented specifically:

[CPP]View Plaincopy
  1. First Proxy method:
  2. -(void) Parser: (Nsxmlparser *) parser didstartelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI qualifiedname: (NSString *) QualifiedName attributes: (nsdictionary *) attributedict
  3. {
  4. //Determine if it is meeting
  5. if ([elementname isequaltostring:@"Meeting"]) {
  6. //Judge attribute node
  7. if ([Attributedict objectforkey:@"addr"]) {
  8. //Gets the value in the attribute node
  9. NSString *addr=[attributedict objectforkey:@"addr"];
  10. }
  11. }
  12. //Judge member
  13. if ([ElementName isequaltostring:@"Member"]) {
  14. NSLog (@"Member"]);
  15. }
  16. }
  17. A second Proxy method:
  18. -(void) Parser: (Nsxmlparser *) parser foundcharacters: (NSString *) string
  19. {
  20. //Get the data in the text node because the following method saves the data obtained here, so define a global variable (a modifiable string)
  21. //nsmutablestring *element = [[nsmutablestring alloc]init];
  22. //The assignment is null here in order to empty the last assignment
  23. [Element setstring:@"];
  24. [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 .
  25. }
  26. A third proxy method:
  27. -(void) Parser: (Nsxmlparser *) parser didendelement: (NSString *) elementname
  28. NamespaceURI: (NSString *) NamespaceURI qualifiedname: (NSString *) QName {
  29. NSString *str=[[nsstring alloc] initwithstring:element];
  30. if ([ElementName isequaltostring:@"creator"]) {
  31. NSLog (@"creator=%@", str);
  32. }
  33. if ([elementname isequaltostring:@"name"]) {
  34. NSLog (@"name=%@", str);
  35. }
  36. if ([ElementName isequaltostring:@"age"]) {
  37. NSLog (@"age=%@", str);
  38. }
  39. [STR release];
  40. }


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
      1. -(void) Parser: (Nsxmlparser *) parser parseerroroccurred: (Nserror *) parseerror{
      2. NSLog (@"%@", [parseerror description]);
      3. }

iOS growth path-XML parsing with Nsxmlparser

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.