Getting started using KissXML

Getting Started

The project contains two folders within its root directory:

  • KissXML - Contains the implementation files.
  • UnitTesting - Various testing code to ensure KissXML operates just like Apple's classes.

To use KissXML within your project, there are two steps:

  1. Copy the KissXML folder into your project (ignore the UnitTesting folder)
  2. Configure Xcode to work with libxml2

Details

The KissXML folder contains:

  • DDXML.h (Standard header file you will #import)
  • DDXMLDocument
  • DDXMLElement
  • DDXMLNode
  • Categories/NSString+DDXML
  • Private/DDXMLPrivate.h
  • Additions/DDXMLElementAdditions (this is optional, all the others listed above are required)

After adding the KissXML folder you'll need to add libxml2 to your Xcode project. There are 2 steps to this process:

  1. Tell Xcode where it can find the libxml2 header files so it will compile
  2. Tell Xcode where it can find the libxml2 compiled library so it will link (last step of compiling)

You'll be adding this to your compiler instructions (at project level, not target level)

  • OTHER_LDFLAGS = -lxml2
  • HEADER_SEARCH_PATHS = /usr/include/libxml2

KissXML_Xcode4Setup1 


KissXML_Xcode4Setup2 


KissXML_Xcode4Setup3 


KissXML_Xcode4Setup4 


KissXML_Xcode4Setup5

Automatic Reference Counting (ARC)

The latest versions of KissXML use ARC. If you're not using ARC in your project, learn how to properly flag the KissXML files as ARC in your Xcode project on the ARC page.


转载:https://github.com/robbiehanson/KissXML/wiki/GettingStarted



写一个解析XML数据的简单Demo(以KissXml为例)    (在iOS代码库中浏览本帖)

1、添加附件里面的KissXML到工程

2、加入libxml2.dylib 到Frameworks

3、修改工程信息,右击Targets下工程名选“Get Info”,进入修改Header Search Paths值为:/usr/include/libxml2,如附件图


附件2为Demo,可运行。


PS:本Demo是以解析本地数据为例,解析网页数据可用:
NSString *content = [NSString stringWithContentsOfURL: url  
                                                 encoding:NSUTF8StringEncoding
                                                    error:&error];
    [string release];
    [self parsedDataFromString:content];
}  


-(void)parsedDataFromString:(NSString *)str{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    DDXMLDocument *doc = [[DDXMLDocument alloc] initWithXMLString:str options:0 error:nil];

/////解析
    NSArray *items = [doc nodesForXPath:kXML error:nil];
    
    for (DDXMLElement *obj in items) {
        xmlData *data = [[xmlData alloc] init];
        
        DDXMLElement *aUser = [obj elementForName:KUSER];
        if(aUser)
            data.user = aUser.stringValue;
        DDXMLElement *aTitle = [obj elementForName:KTITLE];
        if(aTitle)
            data.title = aTitle.stringValue;
        DDXMLElement *content = [obj elementForName:KCONTENT];
        if(content)
            data.content = content.stringValue;
        NSDictionary *XmlDictionary;
        XmlDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                         data.user,@"kUsers",
                         data.title,@"kTitles",
                         data.content,@"kContents",
                         nil];
        [self performSelectorInBackground:@selector(parsedXml:) withObject:XmlDictionary];
        [data release];
    }
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    [doc release];
}


图片:Screen shot 2011-01-17 at 下午03.20.05.png 
Getting started using KissXML_第1张图片
附件:    KissXML.zip (23 K) 下载次数:1828
附件:    parserDataWithKissXml.zip (48 K) 下载次数:2348

转载:http://www.cocoachina.com/bbs/read.php?tid=44701


你可能感兴趣的:(Getting started using KissXML)