Posted By

togino77 on 06/19/10


Tagged

xml iphone


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

umang_nine


Using libxml2 from NSURLConnection


 / Published in: Objective C
 

  1. /*
  2.  Add follow the path to search path of your project
  3.  ${SDKROOT}/usr/include/libxml2
  4.  Add follow the framework to your project
  5.  libxml2.dylib
  6.  
  7.  1. Define your parse class with XMLDownloadParseDelegate protocol
  8.  2. Instance XMLDownloadParse with your parse class
  9.  3. Instance NSURLConnection and begin downloading
  10.  4. Call connection:didReceiveResponse: and connection:didReceiveData: corresponding to NSURLConnection delegate
  11.  5. Parse in your parse class as you like
  12.  */
  13.  
  14. // XMLDownloadParse.h
  15.  
  16. #import <Foundation/Foundation.h>
  17. #import <libxml/tree.h>
  18.  
  19. struct _xmlElementTag {
  20. const unsigned char *localneme;
  21. const unsigned char *prefix;
  22. const unsigned char *URI;
  23. };
  24.  
  25. struct _xmlElementTagNamespaces {
  26. int nb_namespaces;
  27. const unsigned char **namespaces;
  28. int nb_attributes;
  29. int nb_defaulted;
  30. const unsigned char **attributes;
  31. };
  32.  
  33. typedef struct _xmlElementTag xmlElementTag;
  34. typedef struct _xmlElementTagNamespaces xmlElementTagNamespaces;
  35.  
  36. @protocol XMLDownloadParseDelegate
  37.  
  38. - (void)startElementTag:(xmlElementTag *)tag
  39. WithNamespaces:(xmlElementTagNamespaces *)tagNamespace;
  40. - (void)endElementTag:(xmlElementTag *)tag;
  41. - (void)charactersFound:(const unsigned char *)ch len:(int)len;
  42.  
  43. @end
  44.  
  45. @interface XMLDownloadParse : NSObject {
  46. xmlParserCtxtPtr ptrContext;
  47. id<XMLDownloadParseDelegate> delegate;
  48. }
  49.  
  50. @property(nonatomic, retain) id<XMLDownloadParseDelegate> delegate;
  51.  
  52. - (id)initWithDelegate:(id<XMLDownloadParseDelegate>)aDelegate;
  53. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
  54. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
  55.  
  56. - (void)startElementLocalName:(const xmlChar *)localname
  57. prefix:(const xmlChar *)prefix
  58. URI:(const xmlChar *)URI
  59. nb_namespaces:(int)nb_namespaces
  60. namespaces:(const xmlChar **)namespaces
  61. nb_attributes:(int)nb_attributes
  62. nb_defaulted:(int)nb_defaulted
  63. attributes:(const xmlChar **)attributes;
  64. - (void)endElementLocalName:(const xmlChar *)localname
  65. prefix:(const xmlChar *)prefix
  66. URI:(const xmlChar *)URI;
  67. - (void)charactersFound:(const xmlChar *)ch
  68. len:(int)len;
  69.  
  70. @end
  71.  
  72. // XMLDownloadParse.m
  73. #import "XMLDownloadParse.h"
  74.  
  75. static void startElementHandler(
  76. void *ctx,
  77. const xmlChar *localname,
  78. const xmlChar *prefix,
  79. const xmlChar *URI,
  80. int nb_namespaces,
  81. const xmlChar **namespaces,
  82. int nb_attributes,
  83. int nb_defaulted,
  84. const xmlChar **attributes)
  85. {
  86. [(XMLDownloadParse*)ctx
  87. startElementLocalName:localname
  88. prefix:prefix URI:URI
  89. nb_namespaces:nb_namespaces
  90. namespaces:namespaces
  91. nb_attributes:nb_attributes
  92. nb_defaulted:nb_defaulted
  93. attributes:attributes];
  94. }
  95.  
  96. static void endElementHandler(
  97. void *ctx,
  98. const xmlChar *localname,
  99. const xmlChar *prefix,
  100. const xmlChar *URI)
  101. {
  102. [(XMLDownloadParse*)ctx
  103. endElementLocalName:localname
  104. prefix:prefix
  105. URI:URI];
  106. }
  107.  
  108. static void charactersFoundHandler(
  109. void *ctx,
  110. const xmlChar *ch,
  111. int len)
  112. {
  113. [(XMLDownloadParse*)ctx
  114. charactersFound:ch len:len];
  115. }
  116.  
  117. static xmlSAXHandler _saxHandlerStruct = {
  118. NULL, /* internalSubset */
  119. NULL, /* isStandalone */
  120. NULL, /* hasInternalSubset */
  121. NULL, /* hasExternalSubset */
  122. NULL, /* resolveEntity */
  123. NULL, /* getEntity */
  124. NULL, /* entityDecl */
  125. NULL, /* notationDecl */
  126. NULL, /* attributeDecl */
  127. NULL, /* elementDecl */
  128. NULL, /* unparsedEntityDecl */
  129. NULL, /* setDocumentLocator */
  130. NULL, /* startDocument */
  131. NULL, /* endDocument */
  132. NULL, /* startElement*/
  133. NULL, /* endElement */
  134. NULL, /* reference */
  135. charactersFoundHandler, /* characters */
  136. NULL, /* ignorableWhitespace */
  137. NULL, /* processingInstruction */
  138. NULL, /* comment */
  139. NULL, /* warning */
  140. NULL, /* error */
  141. NULL, /* fatalError //: unused error() get all the errors */
  142. NULL, /* getParameterEntity */
  143. NULL, /* cdataBlock */
  144. NULL, /* externalSubset */
  145. XML_SAX2_MAGIC, /* initialized */
  146. NULL, /* private */
  147. startElementHandler, /* startElementNs */
  148. endElementHandler, /* endElementNs */
  149. NULL, /* serror */
  150. };
  151.  
  152. @implementation XMLDownloadParse
  153.  
  154. @synthesize delegate;
  155.  
  156. - (id)initWithDelegate:(id<XMLDownloadParseDelegate>)aDelegate{
  157. if (self = [super init]) {
  158. self.delegate = aDelegate;
  159. }
  160. return self;
  161. }
  162.  
  163. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  164. if (ptrContext) {
  165. xmlFreeParserCtxt(ptrContext);
  166. }
  167. ptrContext = xmlCreatePushParserCtxt(&_saxHandlerStruct,self,NULL,0,NULL);
  168. }
  169.  
  170. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
  171. xmlParseChunk(ptrContext, (const char *)[data bytes], [data length], 0);
  172. }
  173.  
  174. - (void)dealloc {
  175. self.delegate = nil;
  176. if (ptrContext) {
  177. xmlFreeParserCtxt(ptrContext);
  178. ptrContext = NULL;
  179. }
  180. [super dealloc];
  181. }
  182.  
  183. - (void)startElementLocalName:(const xmlChar *)localname
  184. prefix:(const xmlChar *)prefix
  185. URI:(const xmlChar *)URI
  186. nb_namespaces:(int)nb_namespaces
  187. namespaces:(const xmlChar **)namespaces
  188. nb_attributes:(int)nb_attributes
  189. nb_defaulted:(int)nb_defaulted
  190. attributes:(const xmlChar **)attributes
  191. {
  192. xmlElementTag tag;
  193. tag.localneme = localname;
  194. tag.prefix = prefix;
  195. tag.URI = URI;
  196.  
  197. xmlElementTagNamespaces tagName;
  198. tagName.nb_namespaces = nb_namespaces;
  199. tagName.namespaces = namespaces;
  200. tagName.nb_attributes = nb_attributes;
  201. tagName.nb_defaulted = nb_defaulted;
  202. tagName.attributes = attributes;
  203.  
  204. [self.delegate startElementTag:&tag WithNamespaces:&tagName];
  205. }
  206.  
  207. - (void)endElementLocalName:(const xmlChar *)localname
  208. prefix:(const xmlChar *)prefix
  209. URI:(const xmlChar *)URI
  210. {
  211. xmlElementTag tag;
  212. tag.localneme = localname;
  213. tag.prefix = prefix;
  214. tag.URI = URI;
  215.  
  216. [self.delegate endElementTag:&tag];
  217. }
  218.  
  219. - (void)charactersFound:(const xmlChar *)ch
  220. len:(int)len
  221. {
  222. [self.delegate charactersFound:ch len:len];
  223. }
  224.  
  225. @end

Report this snippet  

You need to login to post a comment.