我有一个试图使用 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]];
}
我只是在学习,可能有一个简单的答案,但我被卡住了