XML parsing Sax

Source: Internet
Author: User


Obtain the parsed file path first
Nsstring * xmlpath = [[nsbundle mainbundle] pathforresource: @ "person" oftype: @ "XML"];

2. Initialize an nsdata object based on the file path
Nsdata * Data = [nsdata datawithcontentsoffile: xmlpath];

3. Create a parsing Tool Object
Nsxmlparser * xmlparser = [[nsxmlparser alloc] initwithdata: Data];

4. Set proxy
Xmlparser. Delegate = self;

(1) obey the agreement

@ Interface view: uiview <nsxmlparserdelegate>

@ End

5. Start Parsing
[Xmlparser parse];


6. Implement proxy Methods

Triggered when the start tag is read <>
-(Void) parser :( nsxmlparser *) parser didstartelement :( nsstring *) elementname namespaceuri :( nsstring *) namespaceuri qualifiedname :( nsstring *) QNAME attributes :( nsdictionary *) attributedict
{
Nslog (@ "start % @", attributedict );

Processing Method when data is stored in the attribute of the start tag
Open up space for Arrays
If ([elementname isequaltostring: @ "persons"]) {
Self. Arr = [nsmutablearray array];
} Else if ([elementname isinclutostring: @ "person"])
{
Open up space for person objects
Self. Person = [[person alloc] init] autorelease];
[Self. Arr addobject: Self. person];

} Else if ([elementname isinclutostring: @ "name"])
{
Self. Person. Name = attributedict [@ "name"];
}

Attribute attribute



When the data is stored between the start tag and the end tag


If ([elementname isequaltostring: @ "persons"]) {

When the start tag <persons> of the root node is read

Open up space for Arrays
Self. Arr = [nsmutablearray array];
}
Else
If ([elementname isequaltostring: @ "person"]) {
Open up space for person
Self. Person = [[person alloc] init] autorelease];
}


}

Triggered when the content after the tag is read.
-(Void) parser :( nsxmlparser *) parser foundcharacters :( nsstring *) String
{

Obtain data in this method when data is stored between the start tag and the end tag.

Nslog (@ "content % @", string );
Self. Str = string; temporary storage saves the read data
}


Triggered when the end tag is read </>
-(Void) parser :( nsxmlparser *) parser didendelement :( nsstring *) elementname namespaceuri :( nsstring *) namespaceuri qualifiedname :( nsstring *) QNAME
{
Nslog (@ "end % @", elementname );

When the data is stored between the start tag and the end tag, when the end tag is read, the data temporarily stored is taken out,



If ([elementname isequaltostring: @ "name"]) {
Self. Person. Name = self. STR;

}
Else if ([elementname isinclutostring: @ "person"])
{
When the person end tag is read, the person has been assigned a value and stored in the array.

[Self. Arr addobject: Self. person];
}
}

Triggered after resolution
-(Void) parserdidenddocument :( nsxmlparser *) parser
{
Nslog (@ "AAAA % @", self. Arr );
}

XML parsing Sax

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.