TOUCHXML 解析 xml
fmms
13年前
<p>既然要使用touchXml解析xml。那么首先应该下载touchxml类库。<span style="color:#800000;"><a href="/misc/goto?guid=4959500574246614033" rel="nofollow" target="_blank"><span style="color:#800000;">猛击我</span></a></span>。点击左边的zip下载。</p> <p><img title="TOUCHXML 解析 xml" border="0" alt="TOUCHXML 解析 xml" src="https://simg.open-open.com/show/3fddf9f384005bff4bfbb3a44e9714db.png" width="700" height="175" /></p> <p>打开下载好的类库。找到里面的Source文件夹。这就是需要用到的类库。</p> <p>然后在xcode建立一个项目。在项目中新建一个文件夹,名字随便,就叫touchxml吧。然后将Source中的文件</p> <p>拖入touchxml文件夹中。尽量将Copy itmes into destination group’s folder(if needed)勾选上。</p> <p><img title="TOUCHXML 解析 xml" border="0" alt="TOUCHXML 解析 xml" src="https://simg.open-open.com/show/e304a27a04944257424e84d3588709d6.png" width="375" height="180" /></p> <p>这时候,你编译他是不会通过的。你需要首先添加libxml2 library。右键Frameworks。通过下面的操作</p> <p><img title="TOUCHXML 解析 xml" border="0" alt="TOUCHXML 解析 xml" src="https://simg.open-open.com/show/b8f29e25d18767324e187a15b3bf350e.png" width="514" height="180" /></p> <p>找到libxml2.dylib添加到工程中。这还没有结束,不能使用touchxml。还需要在工程选项中进行配置。</p> <p>点击project-》Edit Porject Settings。在窗口的搜索栏中输入 header search.然后双击</p> <p><span style="color:#000000;">Header Search Paths 后面空白。点击左下角+,打上对号,然后在Path中输入</span><span style="color:#ff6600;"><strong>/usr/include/libxml2</strong></span></p> <p> </p> <p><img title="TOUCHXML 解析 xml" border="0" alt="TOUCHXML 解析 xml" src="https://simg.open-open.com/show/a11840cb465a9e576da0820296e09876.png" width="593" height="222" /></p> <p>在搜索框中输入other linker flags 。添加上字符<span style="color:#ff6600;"><strong>-lxml2。<span style="color:#000000;">然后command +B编译,success。</span></strong></span></p> <p><span style="color:#ff6600;"><strong><span style="color:#000000;">这个,我们就来个练习用一下这个touchxml。</span></strong></span></p> <p><strong>起手我们用它的时候,先在项目里建立一个xml文件。没有xml解析什么呀。~~~~!</strong></p> <p><strong>在Resources文件夹下Add-》New File,选择Mac OS x下的 Resource。然后选择stringFile。</strong></p> <p><strong>文件名随意,就xmlfile.xml吧。内容可以复制下面的内容。</strong></p> <strong><pre class="brush:xml; toolbar: true; auto-links: false;"><books> <iphonebook> <id>01</id> <name>iPhoneDeveloper's</name> <page>123</page> </iphonebook> <iphonebook> <id>02</id> <name>ipadDeveloper's</name> <page>220</page> </iphonebook> <wp7book> <id>03</id> <name>wp7developer</name> <page>281 </page> </wp7book> <wp7book> <id>04</id> <name>windows phone 7</name> <page>300</page> </wp7book> </books></pre> <p></p> </strong> <p><span style="color:#993366;font-size:12px;"><span style="color:#993366;font-size:12px;"><strong>直接上代码说话吧。。。。记得引用它的头文件。</strong><span style="color:#ff0000;">#import "TouchXML.h" </span></span></span></p> <pre class="brush:cpp; toolbar: true; auto-links: false;">- (void)viewDidLoad { //获得文件路径 NSString *XMLPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"userInfo.xml"]; //取得数据 NSData *XMLData = [NSData dataWithContentsOfFile:XMLPath]; //生成CXMLDocument对象 CXMLDocument *document = [[CXMLDocument alloc] initWithData:XMLData options:0 error:nil ]; //[self parseDire:document]; [self parseRoot:document]; [super viewDidLoad]; }</pre> <p></p> <p><span style="line-height:22px;font-family:楷体;font-size:15px;">[self parseDire:document];</span></p> <p><span style="font-family:楷体;"><span style="font-size:15px;">[self parseRoot:document];</span> 这两个</span><span><span style="font-family:黑体;">是两种解析的方式,可以分别尝试下。</span></span></p> <pre class="brush:cpp; toolbar: true; auto-links: false;">- (void) parseDire:(CXMLDocument *) document { NSArray *books = NULL; books = [document nodesForXPath:@"//iphonebook" error:nil]; for (CXMLElement *element in books) { if ([element isKindOfClass:[CXMLElement class]]) { NSMutableDictionary *item = [[NSMutableDictionary alloc] init]; for (int i = 0; i < [element childCount]; i++) { if ([[[element children] objectAtIndex:i] isKindOfClass:[CXMLElement class]]) { [item setObject:[[element childAtIndex:i] stringValue] forKey:[[element childAtIndex:i] name] ]; NSLog(@"%@", [[element childAtIndex:i] stringValue]); } } //NSLog(@"%@", item); } } // books = [document nodesForXPath:@"//wp7book" error:nil]; for (CXMLElement *element in books) { if ([element isKindOfClass:[CXMLElement class]]) { NSMutableDictionary *item = [[NSMutableDictionary alloc] init]; for (int i = 0; i < [element childCount]; i++) { if ([[[element children] objectAtIndex:i] isKindOfClass:[CXMLElement class]]) { [item setObject:[[element childAtIndex:i] stringValue] forKey:[[element childAtIndex:i] name] ]; NSLog(@"%@", [[element childAtIndex:i] stringValue]); } } //NSLog(@"%@", item); } } }</pre> <p></p> <p><span><span style="font-family:黑体;">-------------------------------</span></span></p> <pre class="brush:cpp; toolbar: true; auto-links: false;">- (void) parseRoot:(CXMLDocument *) document { CXMLElement *root = [document rootElement]; NSArray *books = [root children]; for (CXMLElement *element in books) { if ([element isKindOfClass:[CXMLElement class]]) { NSMutableDictionary *item = [[NSMutableDictionary alloc] init]; for (int i = 0; i < [element childCount]; i++) { if ([[[element children] objectAtIndex:i] isKindOfClass:[CXMLElement class]]) { if ([[element name] isEqualToString:@"iphonebook"]) { [item setObject:[[element childAtIndex:i] stringValue] forKey:[[element childAtIndex:i] name]]; NSLog(@"1%@", [[element childAtIndex:i] stringValue] ); } if ([[element name] isEqualToString:@"wp7book"]) { [item setObject:[[element childAtIndex:i] stringValue] forKey:[[element childAtIndex:i] name]]; NSLog(@"2%@", [[element childAtIndex:i] stringValue] ); } } } //NSLog(@"%@", item); } } }</pre> <p></p> <div class="cnblogs_code"> 出处: <a href="/misc/goto?guid=4959500574331506460" rel="nofollow">http://www.cnblogs.com/CCSSPP/archive/2011/11/13/2246582.html</a> </div>