简体   繁体   English

如何访问已解析的XML数据并将其用于其他方法?

[英]How can I access parsed XML data and use it in other methods?

I have an XML file with a comment that has an ID on the bottom of my file that I am successfully NSLog"ing" out to the console. 我有一个带有注释的XML文件,该注释的文件底部具有一个ID,表示我已成功将NSLog“ ing”到控制台。 I used a NSXMLParser to find the comment via the foundComment method. 我使用NSXMLParser通过foundComment方法查找注释。 I then have a GetUID method at the bottom that I want to return my uid string. 然后,我在底部有一个GetUID方法,我想返回我的uid字符串。 I am not sure how to access my parsed data and use it in another separate method. 我不确定如何访问解析的数据并在另一种单独的方法中使用它。 Any help would be greatly appreciated. 任何帮助将不胜感激。

XML FILE: XML文件:

<audioTracks>0</audioTracks>
<!--7934fad5a3a -->

XMLParser.h XMLParser.h

#import <Foundation/Foundation.h>

@interface MyXmlParserDelegate : NSObject <NSXMLParserDelegate>

@end

XMLParser.mm XMLParser.mm

#import "XMLParser.h"

@implementation MyXmlParserDelegate

- (void) parserDidStartDocument:(NSXMLParser *)parser {

}

- (void)parser:(NSXMLParser *)parser
foundComment:(NSString *)uid{

NSLog(@"UID: %@", uid);

}

- (void) parserDidEndDocument:(NSXMLParser *)parser {

}

@end

MAIN.mm 主尺寸

-(NSString *)GetUID {

        NSString * uid = nil;

        NSXMLParser *parser = [[NSXMLParser alloc] initWithData:MyData];

        MyXmlParserDelegate *parserDelegate = [[MyXmlParserDelegate alloc] init];
        [parser setDelegate:parserDelegate];


        [parser parse];

    return uid;

} }

The best answer depends on how reusable you need the parser delegate to be. 最佳答案取决于解析器委托的可重用性。

If the MAIN object is the only customer, then you can eliminate the delegate class altogether, just make self the delegate where you allocate the parser in GetUID . 如果MAIN对象是唯一的客户,则可以完全消除委托类,只需使self成为在GetUID分配解析器的委托GetUID

A more general idea is keep your delegate class, and think of it specifically as a "uid finder". 一个更一般的想法是保留您的委托类,并专门将其视为“ uid finder”。 In this form, it would contain a public uid property which it would initialize during the parse. 以这种形式,它将包含一个公共uid属性,它将在解析期间初始化。 eg 例如

// in the delegate
- (void)parser:(NSXMLParser *)parser foundComment:(NSString *)uid {
    NSLog(@"UID: %@", uid);
    self.uid = uid;
}

// in the customer
MyXmlParserDelegate *parserDelegate = [[MyXmlParserDelegate alloc] init];
parser.delegate = parserDelegate;
[parser parse];

EDIT 编辑

return parserDelegate.uid;

We could get more general still, but the right approach will depend more on the nature of your model and degree to which the XML parsing process needs generalization. 我们可以得到更多的通用性,但是正确的方法将更多地取决于模型的性质以及XML解析过程需要概括的程度。

Side notes: It's customary to name classes and source files the same, so the MyXmlParserDelegate implementation file is better named "MyXmlParserDelegate.m". 旁注:通常将类和源文件命名为相同的名称,因此MyXmlParserDelegate实现文件最好命名为“ MyXmlParserDelegate.m”。 Also, unless your implementations contain C++, they should have the ".m" extensions. 另外,除非您的实现包含C ++,否则它们应具有“ .m”扩展名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM