呼吸de艺术

Live in your world, get owned in mine

Tutorial of kissXML(iPhone)

| Comments

KissXML is a good approach for parsing xml data, and the x-path function make it more powerful.

Integrate With You Project And KissXML

  • Download source codes form here

  • Add all the files to your project (excluding DDXMLTesting)

  • Configure you project to work with libxml

click Project –> Edit Project Settings

You’ll be adding this to your compiler instructions

OTHER_LDFLAGS = -lxml2

HEADER_SEARCH_PATHS = /usr/include/libxml2

Using KissXML

Here is a quick demo to indicate you how it works.

For example, we need to get the SRC value of each media field from the target xml file.

[code lang=“xml”]

[/code]

Here are the codes !

[code lang=“objc”] //hack to remove xmlns => avoid xpath search not works xmlStr = [xmlStr stringByReplacingOccurrencesOfString:@“xmlns” withString:@“noNSxml”]; NSMutableArray* contents = [NSMutableArray array];

NSError error = nil; DDXMLDocument xmlDoc = [[DDXMLDocument alloc] initWithXMLString:xmlStr options:0 error:&error];

if (error) { NSLog(@“%@”,[error localizedDescription]); return nil; }

NSArray* resultNodes = nil; resultNodes = [xmlDoc nodesForXPath:@“//audio | //text | //image | //img” error:&error];

if (error) { NSLog(@“%@”,[error localizedDescription]); return nil; }

for(DDXMLElement resultElement in resultNodes) { NSString name = [resultElement name]; //audio , text or other media type NSString* fileName = [[resultElement attributeForName:@“src”] stringValue]; // 0.txt } [/code]

Note, I replaced the “xmlns” inside the xml file, because it weird xpath would failed if namespace available at a XML file(it might a bug)

And at last, have fun!

Comments