1

我正在开发一个将广播电台流式传输到应用程序的应用程序。广播电台名称和链接存储在 XML 文件中,如下所示:

<radiostacionet>
<radio>
<emri>Radio Gurbeti</emri>
<frekuenca>N/A</frekuenca>
<linku>http://192.184.9.79:8218</linku>
<image>
http://localhost/Apps/An/Shqipcom/data/source/img/radio/radiogurbeti.png
</image>
<dateandtime>
<day>27</day>
<month>09</month>
<year>2013</year>
<hour>13</hour>
<min>43</min>
<sec>00</sec>
</dateandtime>
<website>http://www.radiogurbeti.com/</website>
</radio>
<radio>
<emri>Radio STAR</emri>
<frekuenca>96.4 FM</frekuenca>
<linku>http://radio.1dhe1.com:9400</linku>
<image>
http://localhost/Apps/An/Shqipcom/data/source/img/radio/radiostargjilani.png
</image>
<dateandtime>
<day>27</day>
<month>09</month>
<year>2013</year>
<hour>13</hour>
<min>42</min>
<sec>02</sec>
</dateandtime>
<website>http://www.radiostargjilan.com/</website>
</radio>
<radio>
<emri>Radio VALA RINORE</emri>
<frekuenca>94.7 Fm</frekuenca>
<linku>http://84.22.46.10:8000/</linku>
<image>
http://localhost/Apps/An/Shqipcom/data/source/img/radio/radiovalarinore.png
</image>
<dateandtime>
<day>27</day>
<month>09</month>
<year>2013</year>
<hour>13</hour>
<min>41</min>
<sec>07</sec>
</dateandtime>
<website>http://www.valarinore.net/</website>
</radio>
<radio>
<emri>Radio Islame</emri>
<frekuenca>N/A</frekuenca>
<linku>http://server5.digital-webstream.de:33725</linku>
<image>
http://localhost/Apps/An/Shqipcom/data/source/img/radio/radioislame.png
</image>
<dateandtime>
<day>27</day>
<month>09</month>
<year>2013</year>
<hour>13</hour>
<min>35</min>
<sec>13</sec>
</dateandtime>
<website>http://www.radiovizioni.net/</website>
</radio>
<radio>
<emri>Radio Vizioni</emri>
<frekuenca>88.1 FM</frekuenca>
<linku>http://www.vera-ks.com:9222/</linku>
<image>
http://localhost/Apps/An/Shqipcom/data/source/img/radio/radiovizioni.png
</image>
<dateandtime>
<day>27</day>
<month>09</month>
<year>2013</year>
<hour>13</hour>
<min>34</min>
<sec>35</sec>
</dateandtime>
<website>http://www.radiovizioni.net/</website>
</radio>
<radio>
<emri>Radio Zëri Mitrovices</emri>
<frekuenca>N/A</frekuenca>
<linku>http://209.105.250.73:8202/</linku>
<image>
http://localhost/Apps/An/Shqipcom/data/source/img/radio/radiomitrovica.png
</image>
<dateandtime>
<day>27</day>
<month>09</month>
<year>2013</year>
<hour>13</hour>
<min>33</min>
<sec>40</sec>
</dateandtime>
<website>http://radiomitrovica.eu/</website>
</radio>
<radio>
<emri>Radio Ferizaj</emri>
<frekuenca>N/A</frekuenca>
<linku>http://87.106.23.138:2012</linku>
<image>
http://localhost/Apps/An/Shqipcom/data/source/img/radio/radioferiziaj.png
</image>
<dateandtime>
<day>27</day>
<month>09</month>
<year>2013</year>
<hour>13</hour>
<min>32</min>
<sec>52</sec>
</dateandtime>
<website>http://www.radioferizaji.net/</website>
</radio>
<radio>
<emri>Radio Gjakova</emri>
<frekuenca>N/A</frekuenca>
<linku>http://82.114.80.202:8000/</linku>
<image>
http://localhost/Apps/An/Shqipcom/data/source/img/radio/radiogjakova.png
</image>
<dateandtime>
<day>27</day>
<month>09</month>
<year>2013</year>
<hour>13</hour>
<min>31</min>
<sec>45</sec>
</dateandtime>
<website>http://www.radiogjakova.info/</website>
</radio>
<radio>
<emri>Radio Marimanga</emri>
<frekuenca>N/A</frekuenca>
<linku>http://91.82.85.71:9084/</linku>
<image>
http://localhost/Apps/An/Shqipcom/data/source/img/radio/radiomarimanga.png
</image>
<dateandtime>
<day>27</day>
<month>09</month>
<year>2013</year>
<hour>13</hour>
<min>30</min>
<sec>59</sec>
</dateandtime>
<website>http://www.radiomarimanga.com/</website>
</radio>
<radio>
<emri>Radio Kosova e Lire</emri>
<frekuenca>N/A</frekuenca>
<linku>http://www.radiokosovaelire.com/</linku>
<image>
http://localhost/Apps/An/Shqipcom/data/source/img/radio/radiokosovaelire.png
</image>
<dateandtime>
<day>27</day>
<month>09</month>
<year>2013</year>
<hour>13</hour>
<min>30</min>
<sec>05</sec>
</dateandtime>
<website>http://www.radiokosovaelire.com/</website>
</radio>
<radio>
<emri>Radio Love</emri>
<frekuenca>N/A</frekuenca>
<linku>http://91.121.23.6:8030</linku>
<image>
http://localhost/Apps/An/Shqipcom/data/source/img/radio/radiolove.png
</image>
<dateandtime>
<day>27</day>
<month>09</month>
<year>2013</year>
<hour>13</hour>
<min>29</min>
<sec>34</sec>
</dateandtime>
<website>http://dukagjini.com</website>
</radio>
<radio>
<emri>Radio Shqip</emri>
<frekuenca>N/A</frekuenca>
<linku>http://91.121.23.6:8020</linku>
<image>
http://localhost/Apps/An/Shqipcom/data/source/img/radio/radioshqip.png
</image>
<dateandtime>
<day>27</day>
<month>09</month>
<year>2013</year>
<hour>13</hour>
<min>29</min>
<sec>12</sec>
</dateandtime>
<website>http://dukagjini.com</website>
</radio>
<radio>
<emri>Radio HIT</emri>
<frekuenca>N/A</frekuenca>
<linku>http://91.121.23.6:8010</linku>
<image>
http://localhost/Apps/An/Shqipcom/data/source/img/radio/radiohit.png
</image>
<dateandtime>
<day>27</day>
<month>09</month>
<year>2013</year>
<hour>13</hour>
<min>27</min>
<sec>56</sec>
</dateandtime>
<website>http://dukagjini.com</website>
</radio>
<radio>
<emri>Radio Kosova</emri>
<frekuenca>99.99</frekuenca>
<linku>http://stream2.rtkit.com:8088</linku>
<image>
http://localhost/Apps/An/Shqipcom/data/source/img/radio/3_rating_not_important.png
</image>
<dateandtime>
<day>27</day>
<month>09</month>
<year>2013</year>
<hour>10</hour>
<min>48</min>
<sec>34</sec>
</dateandtime>
<website>rtklive.com</website>
</radio>
<radio>
<emri>Radio dukagjini</emri>
<frekuenca>99.6</frekuenca>
<linku>http://s1.voscast.com:8226</linku>
<image>
http://localhost/Apps/An/Shqipcom/data/source/img/radio/3_rating_important.png
</image>
<dateandtime>
<day>26</day>
<month>09</month>
<year>2013</year>
<hour>16</hour>
<min>52</min>
<sec>01</sec>
</dateandtime>
<website>google.com</website>
</radio>
<radio>
<emri>Radio Bluesky</emri>
<frekuenca>101.2</frekuenca>
<linku>http://stream2.rtkit.com:8098</linku>
<image>
http://localhost/Apps/An/Shqipcom/data/source/img/radio/cio_feed_logo.png
</image>
<dateandtime>
<day>26</day>
<month>09</month>
<year>2013</year>
<hour>11</hour>
<min>25</min>
<sec>22</sec>
</dateandtime>
<website>example.com</website>
</radio>
</radiostacionet>

我正在使用 NSXMLParser 解析 XML,我想从文档中提取名称和链接(emri 和 linku)。

在我的 RadioViewController.h 文件中,我声明了以下变量:

@interface RadioViewController : UIViewController <NSXMLParserDelegate>

{
    NSMutableArray *_idName;
    NSMutableArray *_idLink;
    BOOL isRadio;
}

在 RadioViewConrtoller.m 文件中,我编写了以下代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    _idName = [[NSMutableArray alloc] init];
    _idLink = [[NSMutableArray alloc] init];

    NSURL *URL = [NSURL URLWithString:@"my_link"];
    NSXMLParser * parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
    [parser setDelegate:self];
    [parser parse];
}

最后是解析器方法:

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

    if ([elementName isEqualToString:@"radio"]) {
        isRadio = YES;
    }
    else if ([elementName isEqualToString:@"radio"] && isRadio){
        NSString *idString = [attributeDict objectForKey:@"emri"];
        NSString *isbnString = [attributeDict objectForKey:@"linku"];
        [_idName addObject:idString];
        [_idLink addObject:isbnString];
        NSLog(@"Radio id is: %@ and Link is: %@",idString,isbnString);
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
}

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

    if ([elementName isEqualToString:@"emri"]) {
        isRadio=NO;
        NSLog(@"Id Array count is :%d",[_idName count]);
    }
}

没有问题,当我运行应用程序时,我收到以下日志消息: 在此处输入图像描述

任何帮助将不胜感激,因为我看不出我在这里可能做错了什么。

非常感谢。

花岗岩

4

2 回答 2

3

错字!

if ([elementName isEqualToString:@"radio"] && isRadio){

应该:

if ([elementName isEqualToString:@"emri"] && isRadio){
//                                 ^^^^

isRadio应该真的是inRadio,更具描述性。

于 2013-09-30T08:39:45.427 回答
0

在您的代码中,不会执行didStartElement方法中的else if条件。因为您已经在 if 条件中检查元素名称是单选。所以你的 else if 条件包含非无线电元素。所以对象没有添加到数组中。像这样更改代码

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

    if ([elementName isEqualToString:@"radio"] && isRadio) {
        NSString *idString = [attributeDict objectForKey:@"emri"];
        NSString *isbnString = [attributeDict objectForKey:@"linku"];
        [_idName addObject:idString];
        [_idLink addObject:isbnString];
        NSLog(@"Radio id is: %@ and Link is: %@",idString,isbnString);

    }
    else if ([elementName isEqualToString:@"radio"] && !isRadio){
       isRadio = YES;
    }
}
于 2013-09-30T08:44:13.740 回答