从NSURLConnection使用libxml2
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从NSURLConnection使用libxml2相关的知识,希望对你有一定的参考价值。
/* Add follow the path to search path of your project ${SDKROOT}/usr/include/libxml2 Add follow the framework to your project libxml2.dylib 1. Define your parse class with XMLDownloadParseDelegate protocol 2. Instance XMLDownloadParse with your parse class 3. Instance NSURLConnection and begin downloading 4. Call connection:didReceiveResponse: and connection:didReceiveData: corresponding to NSURLConnection delegate 5. Parse in your parse class as you like */ // XMLDownloadParse.h #import <Foundation/Foundation.h> #import <libxml/tree.h> struct _xmlElementTag { const unsigned char *localneme; const unsigned char *prefix; const unsigned char *URI; }; struct _xmlElementTagNamespaces { int nb_namespaces; const unsigned char **namespaces; int nb_attributes; int nb_defaulted; const unsigned char **attributes; }; typedef struct _xmlElementTag xmlElementTag; typedef struct _xmlElementTagNamespaces xmlElementTagNamespaces; @protocol XMLDownloadParseDelegate - (void)startElementTag:(xmlElementTag *)tag WithNamespaces:(xmlElementTagNamespaces *)tagNamespace; - (void)endElementTag:(xmlElementTag *)tag; - (void)charactersFound:(const unsigned char *)ch len:(int)len; @end xmlParserCtxtPtr ptrContext; id<XMLDownloadParseDelegate> delegate; } @property(nonatomic, retain) id<XMLDownloadParseDelegate> delegate; - (id)initWithDelegate:(id<XMLDownloadParseDelegate>)aDelegate; - (void)startElementLocalName:(const xmlChar *)localname prefix:(const xmlChar *)prefix URI:(const xmlChar *)URI nb_namespaces:(int)nb_namespaces namespaces:(const xmlChar **)namespaces nb_attributes:(int)nb_attributes nb_defaulted:(int)nb_defaulted attributes:(const xmlChar **)attributes; - (void)endElementLocalName:(const xmlChar *)localname prefix:(const xmlChar *)prefix URI:(const xmlChar *)URI; - (void)charactersFound:(const xmlChar *)ch len:(int)len; @end // XMLDownloadParse.m #import "XMLDownloadParse.h" static void startElementHandler( void *ctx, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI, int nb_namespaces, const xmlChar **namespaces, int nb_attributes, int nb_defaulted, const xmlChar **attributes) { [(XMLDownloadParse*)ctx startElementLocalName:localname prefix:prefix URI:URI nb_namespaces:nb_namespaces namespaces:namespaces nb_attributes:nb_attributes nb_defaulted:nb_defaulted attributes:attributes]; } static void endElementHandler( void *ctx, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI) { [(XMLDownloadParse*)ctx endElementLocalName:localname prefix:prefix URI:URI]; } static void charactersFoundHandler( void *ctx, const xmlChar *ch, int len) { [(XMLDownloadParse*)ctx charactersFound:ch len:len]; } static xmlSAXHandler _saxHandlerStruct = { NULL, /* internalSubset */ NULL, /* isStandalone */ NULL, /* hasInternalSubset */ NULL, /* hasExternalSubset */ NULL, /* resolveEntity */ NULL, /* getEntity */ NULL, /* entityDecl */ NULL, /* notationDecl */ NULL, /* attributeDecl */ NULL, /* elementDecl */ NULL, /* unparsedEntityDecl */ NULL, /* setDocumentLocator */ NULL, /* startDocument */ NULL, /* endDocument */ NULL, /* startElement*/ NULL, /* endElement */ NULL, /* reference */ charactersFoundHandler, /* characters */ NULL, /* ignorableWhitespace */ NULL, /* processingInstruction */ NULL, /* comment */ NULL, /* warning */ NULL, /* error */ NULL, /* fatalError //: unused error() get all the errors */ NULL, /* getParameterEntity */ NULL, /* cdataBlock */ NULL, /* externalSubset */ XML_SAX2_MAGIC, /* initialized */ NULL, /* private */ startElementHandler, /* startElementNs */ endElementHandler, /* endElementNs */ NULL, /* serror */ }; @implementation XMLDownloadParse @synthesize delegate; - (id)initWithDelegate:(id<XMLDownloadParseDelegate>)aDelegate{ if (self = [super init]) { self.delegate = aDelegate; } return self; } if (ptrContext) { xmlFreeParserCtxt(ptrContext); } ptrContext = xmlCreatePushParserCtxt(&_saxHandlerStruct,self,NULL,0,NULL); } xmlParseChunk(ptrContext, (const char *)[data bytes], [data length], 0); } - (void)dealloc { self.delegate = nil; if (ptrContext) { xmlFreeParserCtxt(ptrContext); ptrContext = NULL; } [super dealloc]; } - (void)startElementLocalName:(const xmlChar *)localname prefix:(const xmlChar *)prefix URI:(const xmlChar *)URI nb_namespaces:(int)nb_namespaces namespaces:(const xmlChar **)namespaces nb_attributes:(int)nb_attributes nb_defaulted:(int)nb_defaulted attributes:(const xmlChar **)attributes { xmlElementTag tag; tag.localneme = localname; tag.prefix = prefix; tag.URI = URI; xmlElementTagNamespaces tagName; tagName.nb_namespaces = nb_namespaces; tagName.namespaces = namespaces; tagName.nb_attributes = nb_attributes; tagName.nb_defaulted = nb_defaulted; tagName.attributes = attributes; [self.delegate startElementTag:&tag WithNamespaces:&tagName]; } - (void)endElementLocalName:(const xmlChar *)localname prefix:(const xmlChar *)prefix URI:(const xmlChar *)URI { xmlElementTag tag; tag.localneme = localname; tag.prefix = prefix; tag.URI = URI; [self.delegate endElementTag:&tag]; } - (void)charactersFound:(const xmlChar *)ch len:(int)len { [self.delegate charactersFound:ch len:len]; } @end
以上是关于从NSURLConnection使用libxml2的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 NSURlConnection 从 Url 下载图像以保存在手机缓存中
NSURLConnection 完成错误 - 代码 -1100
如何从 NSURLConnection 委托发送多个 url 请求?