gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:07:56 27 4
gpt4 key购买 nike

我正在开发一个应用程序,在 UITableView 中使用 XML 解析。我无法弄清楚如何单独解析属性 rok1rok2。如果我使用 [_element isEqualToString: @ "jmeno"] 应用程序运行良好,但它在 UITableView 中一起向我显示 rok1 rok2 并且不想要它。感谢您的帮助。

这是我的解析器代码:

- (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 文档时跟踪您的“状态”。一种简单的方法是简单地添加一个 bool 值属性,指示您正在目标元素“内部”进行处理。

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

关于ios - Objective-C - 如何单独解析我的 XML 的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29594537/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com