从URL解析和加载XML数据

时间:2015-06-24 13:46:49

标签: ios xml parsing

我需要从这个特定地址(https://www.cnb.cz/cs/financni_trhy/devizovy_trh/kurzy_devizoveho_trhu/denni_kurz.xml)获取XML数据。但是当我运行应用程序时,没有任何反应。老实说,我不知道如何从上面的xml id 1,id 2等获得。我很乐意接受你的任何建议。 THX

- (id)initWithArray: (NSMutableArray *)slovoArray {
    self = [super init];
    if (self) {
        self.slovoArray = slovoArray;
    }
    return self;
}
- (void)parseXMLFile
{
    NSURL *url = [[NSURL alloc] initWithString:@"https://www.cnb.cz/cs/financni_trhy/devizovy_trh/kurzy_devizoveho_trhu/denni_kurz.xml"];

    self.parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
      self.parser.delegate = self;
     [self.parser parse];  
}
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qualifiedName
    attributes:(NSDictionary *)attributeDict {

    self.element = elementName;

    if ([_element isEqualToString:@"radek"]) {

        _item    = [[NSMutableDictionary alloc] init];
        self.kod   = [[NSMutableString alloc] init];
        self.kurz    = [[NSMutableString alloc] init];   
    }
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if ([self.element isEqualToString:@"kod"])
    {
        [self.kod appendString:string];
    }
    else if ([self.element isEqualToString:@"kurz"])
    {
        [self.kurz appendString:string];
    }
}
- (void)parser:(NSXMLParser *)parser
 didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName {

    if ([elementName isEqualToString:@"radek"]) {
        Slova *thisSlovo = [[Slova alloc] initWithName:self.kod
                                              kurz:self.kurz];
        [self.slovoArray addObject:thisSlovo];
    }
    self.element = nil; 
}
@end

2 个答案:

答案 0 :(得分:1)

试试这个

 NSURL *url = [NSURL URLWithString: @"Enter Here your webservice url" ];
 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  NSURLResponse* response;
  NSError* error;
 NSData* result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
 NSString *  rsltStr = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
 NSError *parseError = nil;
 NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:rsltStr error:&parseError];  // In this I have used XMLReader file

从此处下载XML阅读器文件: - XML Reader Download

有关XMLReader的更多帮助,请访问: - SOAP webservice calling in iOS with xml parsing

答案 1 :(得分:0)

为了从URL读取XML数据,我最近为iOS编写了一个名为ConiferXML的简单XML解析器,它可能会满足您的需求。您可以在GitHub查看。如果这不起作用,GitHub上还有另一个图书馆,它有点复杂但做同样的事情。