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.
< text region = "Text" src = "att000.txt" /> |
< text region = "Text" src = "att010.txt" /> |
< img region = "Image" src = "att020.jpg" /> |
< text region = "Text" src = "att040.txt" /> |
< img region = "Image" src = "att120.gif" /> |
Here are the codes !
xmlStr = [xmlStr stringByReplacingOccurrencesOfString: @"xmlns" withString: @"noNSxml" ]; |
NSMutableArray * contents = [ NSMutableArray array]; |
DDXMLDocument* xmlDoc = [[DDXMLDocument alloc] initWithXMLString:xmlStr options:0 error:&error]; |
NSLog ( @"%@" ,[error localizedDescription]); |
NSArray * resultNodes = nil ; |
resultNodes = [xmlDoc nodesForXPath: @"//audio | //text | //image | //img" error:&error]; |
NSLog ( @"%@" ,[error localizedDescription]); |
for (DDXMLElement* resultElement in resultNodes) |
NSString * name = [resultElement name]; |
NSString * fileName = [[resultElement attributeForName: @"src" ] stringValue]; |
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!
No comments:
Post a Comment