ios-senior6-Data Parsing (XML)

Source: Internet
Author: User

1. Basic concepts of analysis

Definition: Extracting data from pre-defined formats

Analytic premise: in advance to agree on a good format, data providers to provide data in a format, data acquisition by the format to obtain data

Common parsing for iOS development: XML parsing, JSON parsing

2.XML Data structure

XML: Extensible Markup Language, one of the mainstream data formats, that can be used to store and transfer data.

Functions: Data exchange, Content management, as a configuration file

Grammar:

1. Disclaimer

2. Nodes are represented by a pair of tags: start and end tags

3. The root node is the starting node and only one node can be nested

4. Nodes can have values, stored in a pair of children tags

Instance:

3. sax file to use when parsing xml


Sax:simple API for XML. Parsing data line by row (using coordinated callback mechanism) based on event-driven parsing

1.NSXMLParser is an XML parsing class that comes with iOS, parsing data in sax mode

2. The parsing process is callback by the Nsxmlparserdelegate protocol method

3. Parsing process: Start tag---value--end tag

Source:

System Sax Parsing: (results are stored using an array containing each dictionary)

1. Get the file path

NSString *filepath = [[NSBundle mainbundle] [email protected] "studentlnfor_xml" oftype:@ "txt"];

2. Get the NSData type of data

NSData *data = [NSData Datawithcontentsoffile:filepath];

NSLog (@ "data =%@", data);

3. Set up the Sax file and associate the associated XML file

Nsxmlparser *parser = [[Nsxmlparser alloc]initwithdata:data];

4. Setting up the agent

Parser.delegate = self;

5. Start parsing

[Parser parser];

#pragma mark-nsxmlparserdelegate protocol method

1. Start document parsing

-(void) Parserdidstartdocument: (Nsxmlparser *) Parser {

Initialize the array in this method (you can also lazy load)

Self.dataarray = [Nsmutablearray array];

}

2. Start parsing tags

-(void) Parser: (Nsxmlparser *) parser didstartelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) QName attributes: (nsdictionary<nsstring *,nsstring *>*) attributedict {

Get the relevant data based on the labels needed for your needs

if ([elementname isequaltostring:@ "Student"]) {

Student *stu = [[Student alloc] init];

The assignment operation is not required here, and the data object is added to the array

[Self.dataarray Addobject:stu];

NSLog (@ "%@", Self.dataarray);

}

Pass the current label value to a property that you just declared

Self.currentelement = elementname;

}

#pragma mark-3. Parse the contents of the tag and assign to the object

-(void) Parser: (Nsxmlparser *) parser foundcharacters: (NSString *) string {

Removes the associated student object from the array, removing the last element from the array each time, ensuring that the latest object content is removed every time

Student *stu = [Self.dataarray lastobject];

KVC Assignment Value

[Stu Setvalue:string ForKey:self.currentElement];

}

#pragma mark-4. End tag

-(void) Parser: (Nsxmlparser *) parser didendelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) qname{

Self.currentelement = nil;

}

#pragma mark-5. Ending document parsing

-(void) Parserdidenddoucument: (Nsxmlparser *) Parser {

Remove Array Checksum

For (Student *student in Self.dataarray) {

NSLog (@ "name =%@,gender =%@,age =%ld,hobby =%@", student.name,student.gender,student.age,student.hobby);

     }

}

#pragma mark-6. Error handling

-(void) Parser: (Nsxmlparser *) parser parseerroroccurred: (Nserror *) ParseError {

NSLog (@ "error: Error =%@", parseerror);

}

Second approach: using DOM parsing

Use of third parties: Gdataxmlnode (Google)

1. Need to introduce a dynamic library (LIBXML2.TBD)

2. Get the file path (IBID.)

3. Obtaining Data (IBID.)

4. Initialize an array of stored data

Self.dataarray = [Nsmutablearray array];

5. Set DOM Parsing (create parse document)

Gdataxmldocument *document = [[Gdataxmldocument alloc] Initwithdata:data options:0 Error:nil];

6. Get the root node (the document is the root node)

Gdataxmlelement *rootelement = document.rootelement;

7. Variable gets the corresponding child node (Studens is a child node, the document is the parent node)

For (Gdataxmlelement *studentelement in Rootelement.children) {

Student *student = [[Student alloc]init];

For (Gdataxmlelement *stuelement in Studentelement.children) {

[Stu SetVslue:stuElement.stringValue Forkey:stuelement];

}

[Self.dataarray Addobject:stu];

Loop display

For (Student *student in Self.dataarray) {

NSLog (@ "name =%@,gender =%@,age =%ld,hobby =%@", student.name,student.gender,student.age,student.hobby);

}

}

3. Using DOM parsing

(1) Set the header in build Settings Search Paths Add/USER/INCLUDE/LIBXML2

Set the other Linker in build settings Flags to add-LXML2

(2) Set Gdataxmlnode in Build phases, add-fno-objc-arc

ios-senior6-Data Parsing (XML)

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.