Return to Snippet

Revision: 27664
at June 19, 2010 16:01 by togino77


Updated Code
/*
 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

@interface XMLDownloadParse : NSObject {
	xmlParserCtxtPtr ptrContext;
	id<XMLDownloadParseDelegate> delegate;
}

@property(nonatomic, retain) id<XMLDownloadParseDelegate> delegate;

- (id)initWithDelegate:(id<XMLDownloadParseDelegate>)aDelegate;
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;

- (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;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    if (ptrContext) {
        xmlFreeParserCtxt(ptrContext);
    }
    ptrContext = xmlCreatePushParserCtxt(&_saxHandlerStruct,self,NULL,0,NULL);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    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

Revision: 27663
at June 19, 2010 15:55 by togino77


Initial Code
/*
 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

@interface XMLDownloadParse : NSObject {
	xmlParserCtxtPtr ptrContext;
	id<XMLDownloadParseDelegate> delegate;
}

@property(nonatomic, retain) id<XMLDownloadParseDelegate> delegate;

- (id)initWithDelegate:(id<XMLDownloadParseDelegate>)aDelegate;
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;

- (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;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    if (ptrContext) {
		xmlFreeParserCtxt(ptrContext);
    }
    ptrContext = xmlCreatePushParserCtxt(&_saxHandlerStruct,self,NULL,0,NULL);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    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

Initial URL

                                

Initial Description

                                

Initial Title
Using libxml2 from NSURLConnection

Initial Tags
xml, iphone

Initial Language
Objective C