2.创建一个工程,在Classes中导入下载的库中的(commonsource)中的内容
3.因为TouchXml使用的是libxml2 这个库,所以在项目中设置添加库的路径, 下图示
4设置好后解析代码如下:
-(void)updateArray:(NSString*)url
{
NSError *error;
NSURLResponse *response;
NSData *dataReply;
NSString *stringReply;
NSMutableArray *bookArray;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"GET"];
dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (dataReply == nil && error != nil) {
return;
}
else {
stringReply = [[NSString alloc] initWithData:dataReply encoding:NSUTF8StringEncoding];
#ifdef shwo_data
NSLog(@"%@",stringReply);
#endif
bookArray = [[NSMutableArray alloc] init];
[bookArray addObjectsFromArray:[self parseXml:stringReply]];
[stringReply release];
}
int arrayCount = 0;
for (BookInfo *bookInfo in bookArray)
{
arrayCount++;
NSLog(@"%@,%d",bookInfo.book_id,arrayCount);
}
[bookArray release];
}
-(NSMutableArray*)parseXml:(NSString*)xmlString
{
if (0 == [xmlString length]) {
return nil;
}
NSMutableArray *item = [[[NSMutableArray alloc] init] autorelease];
CXMLDocument *doc = [[CXMLDocument alloc] initWithXMLString:xmlString options:0 error:nil];
NSArray *resultNodes = nil;
resultNodes = [doc nodesForXPath:@"//homepage" error:nil]; //根节点
if ([resultNodes count])
{
CXMLElement *rootElement = [resultNodes lastObject];
if (rootElement)
{
NSArray *_bookElements = [rootElement elementsForName:@"test1"];
CXMLElement *refiningRootElement = [_bookElements lastObject];
if (refiningRootElement)
{
NSArray *_bookRefiningElements = [refiningRootElement elementsForName:@"item"];
for (CXMLElement *_bookElement in _bookRefiningElements)
{
BookInfo *booklist = [[BookInfo alloc] init];
NSArray *dataInfo = [_bookElement attributes];
if ([dataInfo count]) { //属性
booklist.book_id = [(CXMLNode*)[dataInfo objectAtIndex:0] stringValue];
booklist.book_vmagid = [(CXMLNode*)[dataInfo objectAtIndex:1] stringValue];
booklist.book_color= [(CXMLNode*)[dataInfo objectAtIndex:2] stringValue];
booklist.book_simg_url= [(CXMLNode*)[dataInfo objectAtIndex:3] stringValue];
booklist.book_bimg_url = [(CXMLNode*)[dataInfo objectAtIndex:4] stringValue];
booklist.book_brandname = [(CXMLNode*)[dataInfo objectAtIndex:5] stringValue];
booklist.book_caption = [(CXMLNode*)[dataInfo objectAtIndex:6] stringValue];
booklist.book_discaption = [(CXMLNode*)[dataInfo objectAtIndex:7] stringValue];
booklist.book_level = [[(CXMLNode*)[dataInfo objectAtIndex:8] stringValue] intValue];
booklist.book_desc = [(CXMLNode*)[dataInfo objectAtIndex:9] stringValue];
booklist.book_date = [(CXMLNode*)[dataInfo objectAtIndex:10] stringValue];
booklist.book_nodename = [(CXMLNode*)[dataInfo objectAtIndex:11] stringValue];
}
[item addObject:booklist];
[booklist release];
}
}
}
}
NSLog(@"item count = %d",[item count]);
[doc release];
return item;
}
<homepage version="17493">
<test count="15">
<item id="1" testid="4402" color=""simg="http://.../24/2011062416474851.jpg" bimg="http://..../6/24/2011062416474822.jpg" brandname="史蒂夫" caption="2011年7月" discaption="115" level="5" desc="史蒂夫搜房史蒂夫是方式萨芬是 。" date="2011-07-02" nodename="电风扇" articlestars="4.5" ></item>
<item id="2" testid="4476" color="" simg="http://....../2011070117441835.jpg" bimg="http://..../2011070117441822.jpg" brandname="大师傅" caption="2011年7月" discaption="7" level="5" desc="史蒂夫是 山东省搜房是大师傅的是发是分身乏术方式是是搜房省省方式方式是是" date="2011-07-05" nodename="发生地" articlestars="4" ></item>
<item id="3" testid="4482" color="" simg="http://...../7/4/2011070416422894.jpg" bimg="http://...../7/4/2011070416422897.jpg" brandname="BQ" caption="2011年6月30日" discaption="820B" level="5" desc="史蒂夫史蒂夫山东搜房是方法搜房是是史蒂夫是。" date="2011-07-05" nodename="是" articlestars="3" ></item>
</test>
参考来源: http://blog.csdn.net/flyhawk007j2me/article/details/6004154