0

这是一个简单的 XML:

<MSG>
    <ID>123<ID>
    <Node>
       <ID>456<ID>
    </Node>
</MSG>

我有一个解析器,它是子类ParentParser并实现NSXMLParserDelegate

ParentParser是这样的:

- (id)initWithXmlString:(NSString *)xmlString
{
    if ( (self = [super init]) ) {
        NSString *str = [[NSString alloc] initWithString:xmlString];
        self.xml = str;
        [str release];

        self.storingData     = NO;
        self.receiveString   = [NSMutableString string];
        self.elementsToParse = [NSArray array];
    }

    return self;
}

这就是我解析 XML 的方式:

- (void)parser:(NSXMLParser *)parser 
 didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI 
 qualifiedName:(NSString *)qName{

   if (self.m_oMyObject) {
      if (self.storingData) {
         NSString *tempStr = [self.receiveString stringByTrimmingCharactersInSet:
                              [NSCharacterSet whitespaceAndNewlineCharacterSet]];
         [self.receiveString setString:@""];

         if ([elementName isEqualToString:kID]) {
             //Go in twice
         }
     //============ Code skips ===================

如您所见,kID, StringID已被解析两次。但我想拥有标签内的123唯一,而不是,我该如何解决?谢谢。456<Node>

4

1 回答 1

1

我不认为您可以跳过嵌套元素,但您可以通过增加实例变量in并减少它 inNSXMLParser来跟踪当前级别。levelparser:didStartElement:...parser:didEndElement:...

于 2012-08-17T04:57:59.267 回答