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