IOS使用不同父项下的多个同名标记解析XML

时间:2013-01-03 18:11:13

标签: ios xml-parsing

我正在尝试使用NSXMLPARSER解析XML文件。 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是我想要为公司和文档检索的值,作为两个单独的项目。

我可以使用NSXMLPARSER成功解析公司的val,如下所示:

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

我刚刚学习,可能有一个简单的答案,但我被困了

1 个答案:

答案 0 :(得分: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;
}