NSXMLParser simple parsing example, NSXMLParser simple Parsing

Source: Internet
Author: User

NSXMLParser simple parsing example, NSXMLParser simple Parsing

This article uses the following types of XML as an example:

<? Xml version = "1.0" encoding = "UTF-8"?>

<Resources>

<String name = "e0"> incorrect password </string>

<String name = "e1"> the mobile phone number does not exist </string>

<String name = "e2"> the city is not found </string>

<String name = "e3"> parameter exception </string>

<String name = "e4"> fish stock is empty </string>

<String name = "e6"> the file is empty </string>

<String name = "e7"> the parameter is abnormal or empty </string>

<String name = "e8"> publishing failed </string>

<String name = "e10"> the token is abnormal. Log On again. </string>

<String name = "e11"> the fishing spot does not exist </string>

<String name = "e12"> fishing point id not found </string>

<String name = "e13"> incorrect fishing point id </string>

<String name = "e14"> failed to add to favorites </string>

<String name = "e15"> added to favorites </string>

<String name = "e16"> the longitude and latitude parameters are incorrect </string>

<String name = "e18"> the city cannot be blank </string>

<String name = "e17"> no more fishing spots </string>

<String name = "e19"> insertion failed </string>

<String name = "e20"> incorrect mobile phone number or verification code </string>

<String name = "e21"> parameter type error </string>

<String name = "e22"> data writing error </string>

</Resources>


-(Void) parserXMLFile

{

// Open xml and close the file after reading the File Content

NSString * path = [[NSBundle mainBundle] pathForResource: @ "error" ofType: @ "xml"];

NSFileHandle * file = [NSFileHandle fileHandleForReadingAtPath: path];

NSData * data = [file readDataToEndOfFile];

[File closeFile];

// Initialize the parsing Tool

NSXMLParser * m_parser = [[NSXMLParser alloc] initWithData: data];

// Set proxy

[M_parser setDelegate: self];

// Start parsing, set the parsing result flag, and execute

BOOL flag = [m_parser parse];

If (flag ){

QLLog (@ "resolution successful ");

} Else {

QLLog (@ "resolution failed ");

}

}

// Start Parsing

-(Void) parserDidStartDocument :( NSXMLParser *) parser

{

DicXML = [[NSMutableDictionary alloc] initWithCapacity: 10];

}

// Start mark

-(Void) parser :( NSXMLParser *) parser didStartElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName attributes :( NSDictionary *) attributeDict

{

// QLLog (@ "elementName: % @ \ n namespaceURI: % @ \ n qName: % @ \ n attributeDic: % @", elementName, namespaceURI, qName, attributeDict );

If ([elementName isEqualToString: @ "string"]) {

KeyCode = [attributeDict objectForKey: @ "name"]; // error code

QLLog (@ "keyCode: % @", keyCode );

}

}

// End mark

-(Void) parser :( NSXMLParser *) parser didEndElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName {

// QLLog (@ "end: elementName: % @ \ n namespaceURI: % @ \ n qName: % @ \ n", elementName, namespaceURI, qName );

}

// Input this method when there is a value in the node

-(Void) parser :( NSXMLParser *) parser foundCharacters :( NSString *) string

{

QLLog (@ "key: % @", keyCode );

QLLog (@ "value: % @", string); // error description

String = [string stringByReplacingOccurrencesOfString: @ "\ n" withString: @ ""];

String = [string stringByReplacingOccurrencesOfString: @ "" withString: @ ""];

If (keyCode. length & string. length ){

[DicXML setObject: string forKey: keyCode];

}

}

// Resolution ends

-(Void) parserDidEndDocument :( NSXMLParser *) parser

{

QLLog (@ "dicData: % @", dicXML );

}

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.