2011-06-30 71 views
-1

我有以下xml,我需要使用TouchXML解析。TouchXML問題

<?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> 
     ... 
     ... 
     ... 

我想從根節點創建兩個循環,以獲取標題和鏈接,但coudnt弄清楚如何做到這一點。任何人都可以幫忙嗎?

回答

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

} 

注:類節點應該重複.....

+0

節點命名爲等等,所以我不能用你的XPath –

+0

CXMLNode *節點;對於(i = 0; i <10; i ++){NSString * xpath = [NSString stringWithFormat:@「// category%d/title」,i]; NSArray * title = [[nodes nodesForXPath:xpath] stringValue]; } – Cintu

1

如果你可以改變你的XML文件,並讓所有的類別標籤一樣。你可以把所有...而不是...和...

所以這將是很容易解析。你只需要做類別類,如果你有正確的XML解析代碼,所有的標籤都會被自動解析。

+0

我不能改變XML。你能幫助解析當前的XML嗎? –

+0

你可以使用這個庫來解析你的xml。 http://allseeing-i.com/ASIHTTPRequest/How-to-use – Deeps

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

使用上面的代碼..

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]]; 
       } 
     } 
}