-1

我有以下需要使用 TouchXML 解析的 xml。

<?xml version="1.0"?>
<categories>
    <category0>
    <title>Alcoholic Drinks</title>
        <description>Buy beers, wines, sprits and champagne from the top online 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>
        ...
        ...
        ...

我正在考虑从根节点创建两个循环以获取标题和链接,但无法弄清楚如何去做。有人可以帮忙吗。

4

4 回答 4

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

使用上面的代码..

于 2011-06-30T13:00:44.110 回答
1

如果您可以更改您的 XML 文件并使所有类别标签相同。你可以把 all ... 而不是 ... 和 ....

所以这很容易解析。您只需要创建类别类,如果您有正确的 xml 解析代码,所有标签都会自动解析。

于 2011-06-30T11:45:00.980 回答
0

下面的代码为您提供了一个字典,其中键是标题,数据是链接。当然,如果您的 XML 文档是“大”的,这不是最好的方法。

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]];
              } 
         }
}
于 2011-06-30T13:20:41.420 回答
0
CXMLElement *element;
NSArray *titleItems = [[NSArray alloc] initWithArray:[element nodesForXPath:@"//category" error:nil]];  
for(CXMLElement *item in titleItems){
NSString *title = [[item selectSingleNode:@"title"] stringValue];

}

注意:类别节点应该重复.....

于 2011-06-30T11:38:42.650 回答