2013-01-03 92 views
2

我有一個XML文件,我嘗試使用NSXMLPARSER解析。 xml的結構如下:IOS在不同的父母下使用同一名稱的多個標籤解析XML

<report> 
    <Page1> 
    <row> 
     <company> 
      <val> 
      company 1 
      </val> 
     </company> 
     <TIME1> 
      <val> 
       time 1 
      </val> 
     </TIME1> 
     <document> 
      <val> 
      document 1 
      </val> 
     </document> 
    </row> 
     <row> 
     <company> 
      <val> 
      company 2 
      </val> 
     </company> 
     <TIME1> 
      <val> 
       time 2 
      </val> 
     </TIME1> 
     <document> 
      <val> 
      document 2 
      </val> 
     </document> 
    </row> 
    </Page1> 
</report> 

其中val是我想要爲公司和文檔檢索的值作爲兩個單獨的項目。

我能成功解析VAL使用的NSXMLParser這樣的公司:

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{ 
currentNodeContent2 = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; } 


- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 
{ 
if ([elementname isEqualToString:@"Company"]) 
    { 
     currentRfp = [rfp alloc]; 
     isStatus = YES; 
    } 
    if ([elementname isEqualToString:@"TIME1"]) 
    { 
     isStatus = NO; 
    } 
} 


- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{ 
    if (isStatus  ) 
    { 

     if ([elementname isEqualToString:@"val"]) 
     { 
      currentRfp.content2 = currentNodeContent2; 
     } 
     if ([elementname isEqualToString:@"val"]) 
     { 
      currentRfp.dateCreated2 = currentNodeContent2; 
     } 


    } 
    if ([elementname isEqualToString:@"row"]) 
    { 
     [self.rfps addObject:currentRfp]; 
     currentRfp = nil; 
     currentNodeContent2 = nil; 
    } 

} 

- (void) parser:(NSXMLParser *)parser foundCharacters2:(NSString *)string 
{ 
    currentNodeContent2 = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
} 

我只是學習,有可能是一個簡單的答案,但我堅持

回答

0

您可以使用下面的代碼這樣做:

- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 
{ 
    if ([elementname isEqualToString:@"row"]) 
    { 
     currentRfp = [rfp alloc]; 
    } 
    currentNodeContent2 = [[NSMutableString alloc] init]; 
} 

- (void) parser:(NSXMLParser *)parser foundCharacters2:(NSString *)string 
{ 
    currentNodeContent2 = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
} 

- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{ 
    if ([elementname isEqualToString:@"val"]) 
    { 
     return; 
    } 
    if ([elementname isEqualToString:@"TIME1"]) 
    { 
     //you get val of time1 here 
    } 
    if ([elementname isEqualToString:@"company"]) 
    { 
     //you get val of company here 
    } 
    if ([elementname isEqualToString:@"document"]) 
    { 
     //you get val of document here 
    } 
    if ([elementname isEqualToString:@"row"]) 
    { 
     [self.rfps addObject:currentRfp]; 
     currentRfp = nil; 
    } 
    [currentNodeContent2 release]; 
    currentNodeContent2 = nil; 
} 
+0

感謝您的快速回復。我試過這個,當我運行應用程序並訪問視圖時,我現在得到了EXC_BAD_ACCESS。有任何想法嗎? –

+0

事實證明,每個標籤(公司,文件,時間1)有另一個孩子的名字<文本因此,prser只是返回字tect –

+0

@BobbyCloutier:放置斷點並檢查它在哪裏崩潰。 –

相關問題