2010-10-19 86 views
1

我有一個小問題,我似乎無法解決。我有一個XML頁面,內容如下:NSString與XML到NSArray與NSDictionary

<?xml version="1.0" encoding="utf-8"?> 
<ArrayOfCategory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://api.tradera.com"> 
    <Category Id="1612" Name="Category 1"> 
    <Category Id="161213" Name="Category 1 subcat 1"> 
     <Category Id="340761" Name="Category 1, subcat 1, subcat 1" /> 
     <Category Id="340761" Name="Category 1, subcat 1, subcat 2" /> 
     <Category Id="340761" Name="Category 1, subcat 1, subcat 3" /> 
     <Category Id="340761" Name="Category 1, subcat 1, subcat 4" /> 
     <Category Id="340761" Name="Category 1, subcat 1, subcat 5" /> 
    </Category> 
    <Category Id="161213" Name="Category 1 subcat 2"></category> 
    <Category Id="161213" Name="Category 1 subcat 3"> 
     <Category Id="340761" Name="Category 1, subcat 3, subcat 1" /> 
    </category> 
    </Category> 
    <Category Id="1612" Name="Category 1"> 
    [...] 
    </category> 
</ArrayOfCategory> 

我想要做的是將其轉換成使用詞典的數組。 因此,這應該是一個數組,其中鍵爲0 = dictorionary,其中包含項Id,Name和subcats。子卡應該是具有相同密鑰的數組。

到目前爲止,我有以下代碼:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [self startParsing]; 
} 

- (void)startParsing { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"categories" ofType:@"xml"]; 

    NSData *xmlData = [[NSData alloc] initWithContentsOfFile:path]; 
    NSXMLParser *parser = [[[NSXMLParser alloc] initWithData:xmlData] autorelease]; 
    [parser setDelegate:self]; 
    [parser parse]; 
} 

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict { 
    NSLog(@"Started %@, attribute: %@", elementName, attributeDict); 
} 

但我似乎無法弄清楚如何使用的NSXMLParser,有什麼建議創建這個數組?

最好的問候,保羅 Peelen

回答

2

正如你需要跟蹤你在裏面

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict { 
    NSLog(@"Started %@, attribute: %@", elementName, attributeDict); 
} 

節什麼水平聊天討論。在這裏可能使用全球伊娃?