NSXML 解析器读取属性

发布于 2024-11-09 08:13:57 字数 1898 浏览 0 评论 0原文

我有一个重复的元素,我需要读取它的属性并将它们发送给委托,

xml 是:

<special>
   <day date="22/04/2011" name="Easter Friday">Closed</day>
   <day date="23/04/2011" name="Easter Saturday">10:00-16:00</day>
   <day date="24/04/2011" name="Easter Sunday">Closed</day>
   <day date="25/04/2011" name="Anzac Day">13:00-17:00</day>
   <day date="26/04/2011" name="Easter Tuesday">09:00-18:00</day>
</special>

我只能将日期和名称的最后一个属性传递给委托,我知道为什么会发生这种情况,但我不知道如何修复它。有人可以帮助我

吗,这是我的目标 C 代码

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
    attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"special"]) {
        storeAppDelegate.openingHoursSpecialDelegate = [[NSMutableArray alloc] init];
    }else if ([elementName isEqualToString:@"day"]) {
        openingHoursView = [[OpeningHoursView alloc] init];
        openingHoursView.name = [attributeDict objectForKey:@"name"];
        openingHoursView.date = [attributeDict valueForKey:@"date"];
      }
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"special"])
        return;
    if ([elementName isEqualToString:@"day"]){
        [storeAppDelegate.openingHoursSpecialDelegate addObject:openingHoursView];
        [openingHoursView release];
        openingHoursView = nil;     

    }   
}

openingHoursSpecialDelegate 是应用程序委托中的可变数组,而 OpeningHoursView 是一个 NSObject,其名称和日期作为另一个类中的字符串。他们还获取应用程序委托的值,并且它也只是 XML 文件中“日期”和“名称”属性的最后读取值。 我正在使用 NSXML 解析器 所以我的问题又是如何让“openingHoursView.name”和“openingHoursView.date”将它们得到的每个值写入 openingHoursSpecialDelegate 而不是像现在这样覆盖它们

I have a element that is repeating and i need to read it's attributes and send them to the delegate

the xml is:

<special>
   <day date="22/04/2011" name="Easter Friday">Closed</day>
   <day date="23/04/2011" name="Easter Saturday">10:00-16:00</day>
   <day date="24/04/2011" name="Easter Sunday">Closed</day>
   <day date="25/04/2011" name="Anzac Day">13:00-17:00</day>
   <day date="26/04/2011" name="Easter Tuesday">09:00-18:00</day>
</special>

i only get to past the last attributes for date and name to the delegate and i know why is this happening but i dont know how to fix it. can someone help me

here is my objective C code

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
    attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"special"]) {
        storeAppDelegate.openingHoursSpecialDelegate = [[NSMutableArray alloc] init];
    }else if ([elementName isEqualToString:@"day"]) {
        openingHoursView = [[OpeningHoursView alloc] init];
        openingHoursView.name = [attributeDict objectForKey:@"name"];
        openingHoursView.date = [attributeDict valueForKey:@"date"];
      }
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"special"])
        return;
    if ([elementName isEqualToString:@"day"]){
        [storeAppDelegate.openingHoursSpecialDelegate addObject:openingHoursView];
        [openingHoursView release];
        openingHoursView = nil;     

    }   
}

openingHoursSpecialDelegate is a mutable array in the app delegate and OpeningHoursView is a NSObject that has name and date as strings in it in another class. They also get the value of the app delegate and it is also only the last read value for "date" and "name" attributes from the XML file .
I'm working with NSXML parser
so again my question is how to get "openingHoursView.name" and "openingHoursView.date" to write every value they get to openingHoursSpecialDelegate and not overwrite them as it happens now

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

起风了 2024-11-16 08:13:57

我找不到代码有什么问题。我已将上述代码放入一个小型测试项目中(进行了一些细微的更改以使其独立运行),并且它对我来说运行良好。

数组(
“复活节星期五,2011 年 4 月 22 日”,
“复活节星期六,2011 年 4 月 23 日”,
“复活节星期日,2011 年 4 月 24 日”,
“澳新军团日,2011 年 4 月 25 日”,
“复活节星期二,2011 年 4 月 26 日”)

示例项目

您需要更改路径我已经在 test2AppDelegate 类中进行了硬编码,以指向包含您上面发布的 XML 的文件。

I can't find anything wrong with the code. I've put the above code into a small test project (with minor changes to make it run standalone), and it runs fine for me.

Array (
"Easter Friday, 22/04/2011",
"Easter Saturday, 23/04/2011",
"Easter Sunday, 24/04/2011",
"Anzac Day, 25/04/2011",
"Easter Tuesday, 26/04/2011" )

Example project

You'll need to change the path I've hardcoded in the class test2AppDelegate, to point to a file containing the XML you posted above.

澉约 2024-11-16 08:13:57

我已经在我的项目中解决了这个问题。但是我正在使用 libxml2。
问题是(日节点)你必须将 5 个不同的值设置为相同的键(日),这就是你得到最后一个属性的原因。

Already i have workout this problem in my project.But i am using libxml2.
The problem is (day node) you have to set the 5 different value to same key (day) thats why you get last attribute .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文