开发者

TouchXML problem

I have got following xml which I need to parse using TouchXML.

<?xml version="1.0"?>
<categories>
    <category0>
    <title>Alcoholic Drinks</title>
        <description>Buy beers, wines, sprits and champagne from the top o开发者_开发百科nline alocholic drink stores.&#xD;
                 Whatever your tipple you are sure to find a drinks supplier from our top shops below:
        </description>
        <status>1</status>
        <popularStatus></popularStatus>
        <order></order>
        <link>alcoholic-drinks</link>
        <id>1</id>
    </category0>
    <category1>
        <title>Art and Collectibles</title>
        <description>Are you looking to buy contemporary or fine art, or do you prefer to make your own artwork?&# 
                &#xD;
                Whether type of artwork or craft materials you are looking for, you are certain to find one of the shops below more than helpful:
        </description>
        <status>1</status>
        <popularStatus></popularStatus>
        <order></order>
        <link>art-and-collectibles</link>
        <id>2</id>
    </category1>
    <category2>
        <title>Auctions</title>
        <description>Are you looking for the UK's biggest and best Auction Sites?&#xD;
                The team at safebuyer.co.uk have scoured the web to find the UK's favourite auctions, so why wait, start your bidding now!
        </description>
        ...
        ...
        ...

I am thinking to create two loops from root node in order to fetch title and link but coudnt figure out how to do it. Can anybody help please.


If you can change your XML file and make all the category tag same. You can put all ... Instead of ... and ....

So that would be pretty easy to parse. You just need to make category class and all the tag would be parse automatically if you have correct xml parsing code.


CXMLNode *node;
for(i=0; i<10 ; i++){
    NSString *xpath = [NSString stringWithFormat:@"//category%d/title", i]; 
    NSArray *title = [[node nodesForXPath:xpath] stringValue];
}

Use the above code..


CXMLElement *element;
NSArray *titleItems = [[NSArray alloc] initWithArray:[element nodesForXPath:@"//category" error:nil]];  
for(CXMLElement *item in titleItems){
NSString *title = [[item selectSingleNode:@"title"] stringValue];

}

Note: category node should be repeating.....


The code below gives you a dictionary where keys are titles and data are the links. Of course, if your XML document is "big", this is not the best way to do it.

CXMLDocument *doc = [[[CXMLDocument alloc] initWithXMLString:theXML options:0 error:nil] autorelease];
NSArray *categories = nil;
NSMutableDictionary* results = nil;
categories = [doc nodesForXPath:@"/categories/*[starts-with(name(), 'category')]" error:nil];
if (categories != nil && [categories count] > 0)
{
         results = [NSMutableDictionary dictionaryWithCapacity:[categories count]];
         for (CXMLElement *category in categories)
         {
              NSArray* titles = [category elementsForName:@"title"];
              if ([titles count] >0)
              {
                   NSArray* links = [category elementsForName:@"link"];
                   [result setObject:([links count]>0?[[links objectAtIndex:0] stringValue]:nil;
                           forKey:[[titles objectAtIndex:0] stringValue]];
              } 
         }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜