KissXML 简易教程
fmms
13年前
<p>文章来源 <a href="/misc/goto?guid=4959500575145200931" rel="nofollow">http://ixhan.com/2010/03/tutorial-of-kissxml-iphone/</a></p> <p>KissXML 是我觉得最简单方便的XML解析器了,更重要是很简单的支持Xpath</p> <p>在项目中使用前先简单配置下.</p> <p></p> <ul> <li>在这里下载源代码 <a href="/misc/goto?guid=4959500575230764789" rel="nofollow">here</a> </li> <li>添加除了DDXMLTesting 外的文件</li> <li>根据下图配置libxml</li> </ul> <div> <span style="line-height:normal;font-family:Verdana, Arial, Helvetica, sans-serif;color:#000000;">打开项目属性</span> </div> <div> 添加如下内容 </div> <div> <br /> </div> <div> <p>OTHER_LDFLAGS = -lxml2</p> <p>HEADER_SEARCH_PATHS = /usr/include/libxml2</p> <p>如图</p> <p><img title="KissXML 简易教程" border="0" alt="KissXML 简易教程" src="https://simg.open-open.com/show/eabc37c243ad3cdde9ff2c8a1a075d54.png" width="639" height="673" /></p> <p>然后就可以使用KissXML了</p> <pre class="brush:xml; toolbar: true; auto-links: false;"><smil xmlns="http://www.w3.org/2000/SMIL20/CR/Language"> <head> </head> <body> <par dur="120000ms" > <text region="Text" src="att000.txt" /> </par> <par dur="120000ms" > <text region="Text" src="att010.txt" /> </par> <par dur="10000ms" > <img region="Image" src="att020.jpg"/> </par> <par dur="120000ms" > <text region="Text" src="att040.txt" /> </par> <par dur="10000ms" > <img region="Image" src="att120.gif"/> </par> </body> </smil></pre> <p></p> 我们的目标是获取该xml媒体内容的 src 地址 <pre class="brush:cpp; toolbar: true; auto-links: false;">//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 }</pre> <p></p> 注意第一行,我把xmlns给删除了,(否则kissxml无法正常使用xpath,原因位置) </div>