2

我有一个试图使用 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]];
}

我只是在学习,可能有一个简单的答案,但我被卡住了

4

1 回答 1

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;
}
于 2013-01-03T19:52:37.540 回答