IOS: Working with XML files

Source: Internet
Author: User

Nsxmlparser is the standard library class
Book.xml
<?XML version= "1.0" encoding= "UTF-8"?><!--Document created By:lishujun, code-style with Xmlgrid Online XML Editor, Http://xmlgrid.net -< Book>    <!--<summary> <author> Jin Yong </author> <content>sfdgasidfhasdfb</content> &l T;/summary> -<Chapters>    <Chaptername= "Hello">Hello,world</Chapter>    <Chaptername= "Lishujun">i ' m lishujun sdfsdfs aaaasdfsdf qqqqqqqq</Chapter>    <Chaptername= "Lishujun2">I ' M lishujun2</Chapter></Chapters></ Book>

XmlElement.h
#import <Foundation/Foundation.h>@interface* * * attributedict; @end
Xmlelement.m
#import " XmlElement.h " @implementation XmlElement @synthesize elementname; @synthesize text; @synthesize attributedict; @end

BookXmlParser.h

#import <Foundation/Foundation.h>#import"XmlElement.h"@ Interface Bookxmlparser:nsobject <NSXMLParserDelegate>{    *_element;     *_parser;} -(ID) Initwithdata: (nsdata*) data; -(BOOL) parse; @end

Bookxmlparser.m

#import "BookXMLParser.h"#import "XmlElement.h"@implementationBookxmlparser-(ID) Initwithdata: (nsdata*) data{if(self) {_parser=[[Nsxmlparser Alloc]initwithdata:data]; _parser.Delegate=Self ; }    returnSelf ;}-(BOOL) parse{_element=Nil; return[_parser parse];}-(void) Parser: (Nsxmlparser *) Parserdidstartelement: (NSString*) elementname NamespaceURI: (NSString*) NamespaceURI qualifiedname: (NSString*) QName attributes: (nsdictionary*) attributedict{[self processelement]; _element=[[XmlElement alloc]init]; _element.elementname=elementname; _element.attributedict=attributedict; _element.text=@"";}-(void) Parser: (Nsxmlparser *) parser foundcharacters: (NSString *)string{    if([_element.text isequaltostring:@""]) {_element.text=string; //NSLog (@ "Now element is%@", _element.text);    }}-(void) processelement{if(_element = =Nil) {        return; } NSLog (@"Process:"); NSLog (@"%@, %@", _element.elementname, _element.text);}

IOS: Working with XML files

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.