Objective-C - 如何单独解析我的 XML 的属性?

Objective-C - how can separately parse attributes of my XML?

我正在开发一个在 UITableView 中使用 XML 解析的应用程序。我不知道我是如何分别解析属性 rok1rok2。如果我使用 [_element isEqualToString: @ "jmeno"] 应用程序运行良好,但它在 UITableView 中显示给我 rok1rok2 并且不想要它。感谢您的帮助。

这是我的解析器代码:

- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qualifiedName
    attributes:(NSDictionary *)attributeDict {

    self.element = elementName;

    if ([_element isEqualToString:@"rok1"])
    {
        _item    = [[NSMutableDictionary alloc] init];
        self.nazev   = [[NSMutableString alloc] init];
        self.definice    = [[NSMutableString alloc] init];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{ 
    if ([self.element isEqualToString:@"nazev"])
    {
        [self.nazev appendString:string];
    }
    else if ([self.element isEqualToString:@"def"])
    {
        [self.definice appendString:string];
    }
}

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

    if ([elementName isEqualToString:@"rok1"])
        {
        Slova *thisSvatek = [[Slova alloc] initWithName:self.nazev
                                              definice:self.definice];
        [self.svatkyArray addObject:thisSvatek];
    }
    self.element = nil;
}

还有我的例子XML:

<?xml version="1.0" encoding="UTF-8" ?>
<menu>
    <rok1>
        <jmeno>
        <nazev>Prvni</nazev>
        <def>blblbla</def>
        <jmeno>
        <nazev>Druhy</nazev>
        <def>blbablabal</def>
        </jmeno>
   </rok1>
   <rok2>
        <jmeno>
        <nazev>Prvni</nazev>
        <def>blblbla</def>
        </jmeno>
   </rok2>
</menu>

您需要在解析 XML 文档时跟踪您的 "state"。一种简单的方法是简单地添加一个布尔值 属性,表明您正在处理 'inside' 目标元素。

- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qualifiedName
    attributes:(NSDictionary *)attributeDict {

    self.element = elementName;

    if ([elementName isEqualToString:@"rok1"])
    {
        self.foundTarget=YES;
        self.nazev   = [[NSMutableString alloc] init];
        self.definice    = [[NSMutableString alloc] init];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{ 
    if (self.foundTarget) {
        if ([self.element isEqualToString:@"nazev"])
        {
            [self.nazev appendString:string];
        }
        else if ([self.element isEqualToString:@"def"])
        {
            [self.definice appendString:string];
        }
    }
}

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

    if ([elementName isEqualToString:@"rok1"])
    {
        Slova *thisSvatek = [[Slova alloc] initWithName:self.nazev
                                              definice:self.definice];
        [self.svatkyArray addObject:thisSvatek];
        self.foundTarget=NO;
    }
    self.element = nil;
}

一种更复杂的方法是构建某种数据结构,例如字典,代表您的 XML

- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qualifiedName
    attributes:(NSDictionary *)attributeDict {

    self.element = elementName;

    if ([elementName isEqualToString:@"rok1"]  || [elementName.isEqualToString:@"rok2"])
    {
        self.nazev   = [[NSMutableString alloc] init];
        self.definice    = [[NSMutableString alloc] init];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{ 
        if ([self.element isEqualToString:@"nazev"])
        {
            [self.nazev appendString:string];
        }
        else if ([self.element isEqualToString:@"def"])
        {
            [self.definice appendString:string];
        }
}

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

    if ([elementName isEqualToString:@"rok1"]  || [elementName.isEqualToString:@"rok2"])
        Slova *thisSvatek = [[Slova alloc] initWithName:self.nazev
                                              definice:self.definice];
        self.rokDictionary[elementName]=thisSvatek; // self.rokDictionary is an NSMutableDictionary
    }
    self.element = nil;
}

要分离出各种'jmeno'标签,你需要在didStartElement&didEndElement-

中处理它们
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qualifiedName
    attributes:(NSDictionary *)attributeDict {

    self.element = elementName;

    if ([elementName isEqualToString:@"rok1"]  || [elementName.isEqualToString:@"rok2"])
    {
        self.jmenoArray=[NSMutableArray new];
    }
    else if ([elementName isEqualToString:@"jmeno"]) {
        self.nazev   = [[NSMutableString alloc] init];
        self.definice    = [[NSMutableString alloc] init];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{ 
        if ([self.element isEqualToString:@"nazev"])
        {
            [self.nazev appendString:string];
        }
        else if ([self.element isEqualToString:@"def"])
        {
            [self.definice appendString:string];
        }
}

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

    if ([elementName isEqualToString:@"jmeno"]) {
        Slova *thisSvatek = [[Slova alloc] initWithName:self.nazev
                                              definice:self.definice];
        [self.jemnoArray addObject:thisSvatek];
    } 
    else if ([elementName isEqualToString:@"rok1"]  || [elementName.isEqualToString:@"rok2"])

        self.rokDictionary[elementName]=self.jmenoArray; // self.rokDictionary is an NSMutableDictionary
        self.jmenoArray=nil;
    }
    self.element = nil;
}