首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用NSXMLParser解析多层XML

用NSXMLParser解析多层XML
EN

Stack Overflow用户
提问于 2014-04-14 20:28:06
回答 2查看 835关注 0票数 1

首先,如果术语不正确,请原谅。我正在尝试将来自旧金山BART系统的XML提要解析为一个UITableView,以了解解析是如何工作的。下面解析一个简单的提要是非常直接的。

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8" ?> 
<root>
  <uri><![CDATA[ http://api.bart.gov/api/sched.aspx?cmd=special ]]></uri>
  <holidays>
    <holiday>
      <name>New Year's Day (2009)</name> 
      <date>01/01/2009</date> 
      <schedule_type>Sunday</schedule_type> 
    </holiday>
    <holiday>
      <name>Presidents' Day</name> 
      <date>02/16/2009</date> 
      <schedule_type>Saturday</schedule_type> 
    </holiday>

我在那里感觉很自信。问题是,现在我已经转到了一种更复杂的格式,如下所示。

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8" ?> 
<root>
  <uri><![CDATA[ http://api.bart.gov/api/etd.aspx?cmd=etd&orig=RICH ]]></uri>
  <date>03/30/2011</date> 
  <time>02:43:27 PM PDT</time> 
  <station>
    <name>Richmond</name> 
    <abbr>RICH</abbr> 
  <etd>
    <destination>Fremont</destination> 
    <abbreviation>FRMT</abbreviation> 
    <estimate>
      <minutes>5</minutes> 
      <platform>2</platform> 
      <direction>South</direction> 
      <length>6</length> 
      <color>ORANGE</color> 
      <hexcolor>#ff9933</hexcolor> 
      <bikeflag>1</bikeflag> 
    </estimate>
    <estimate>
    <minutes>20</minutes> 
      <platform>2</platform> 
      <direction>South</direction> 
      <length>6</length> 
      <color>ORANGE</color> 
      <hexcolor>#ff9933</hexcolor> 
      <bikeflag>1</bikeflag> 
    </estimate>
  </etd>
  <etd>
    <destination>Millbrae</destination> 
    <abbreviation>MLBR</abbreviation> 
    <estimate>
      <minutes>Leaving</minutes> 
      <platform>2</platform> 
      <direction>South</direction> 
      <length>10</length> 
      <color>RED</color> 
      <hexcolor>#ff0000</hexcolor> 
      <bikeflag>1</bikeflag> 
    </estimate>
  </etd>
 </station>
 <message /> 
</root>

我面临的实际问题是如何在didStartdidEnd方法中构建填充表视图的数组,因为这是一个更复杂的结构。我所拥有的如下(在我沮丧到寻求帮助之前结束的审判),这是非常不正确的。

代码语言:javascript
复制
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {


element = elementName;

if ([element isEqualToString:@"station"]) {

    station = [[NSMutableDictionary alloc] init];
    name   = [[NSMutableString alloc] init];

}

if ([element isEqualToString:@"etd"]) {

    etd    = [[NSMutableDictionary alloc] init];
    destination   = [[NSMutableString alloc] init];

}

if ([element isEqualToString:@"estimate"]) {

    estimate    = [[NSMutableDictionary alloc] init];
    minutes   = [[NSMutableString alloc] init];
}

}

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

if ([elementName isEqualToString:@"station"]) {
    [station setObject:name forKey:@"name"];

    [data addObject:[station copy]];

}

if ([elementName isEqualToString:@"etd"]) {
    [etd setObject:destination forKey:@"destination"];        
    //[data addObject:[etd copy]];

}

if ([elementName isEqualToString:@"estimate"]) {
    [estimate setObject:minutes forKey:@"minutes"];
    //[data addObject:[estimate copy]];
}
}

我理解上面所做的是为每个元素创建一个单独的数组。最终的目标是在"x分钟“内从车站名称向下钻到目的地,再到出发。此外,我也不使用XCODE,在这种情况下,大多数魔术都发生在我身上,有大量用于解析简单XML的教程,还有许多与简单结构相关的问题,还有很多建议使用另一个解析器。这些不是我的选择,我被限制在没有XCODE或IB的纯代码中。这里的最后一个问题是,我将如何构建我的最终数组以允许钻取数据结构?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-15 02:03:35

看起来,走开,回来,新鲜的头脑,仍然创造奇迹。这就是我想出的解决办法。不要太难。

代码语言:javascript
复制
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {


element = elementName;

if ([element isEqualToString:@"station"]) {

    stationArray = [[NSMutableArray alloc] init];

    station = [[NSMutableDictionary alloc] init];
    name   = [[NSMutableString alloc] init];

}

else if ([element isEqualToString:@"etd"]) {

    destinationArray    = [[NSMutableArray alloc] init];
    destination   = [[NSMutableString alloc] init];

}

else if ([element isEqualToString:@"estimate"]) {

    estimateArray    = [[NSMutableArray alloc] init];
    minutes   = [[NSMutableString alloc] init];


}

}

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

///// Add Arrays to Dictionary, then Copy back to Array that sorts the Table

if ([elementName isEqualToString:@"station"]) {
    [station setObject:name forKey:@"name"];
    [station setObject:destinationArray forKey:@"destination"];
    [station setObject:estimateArray forKey:@"minutes"];
    [data addObject:[station copy]];

}

///// Add Destinations to an array

else if ([elementName isEqualToString:@"etd"]) {
    [destinationArray addObject:destination];


}

///// Add minutes to an array

else if ([elementName isEqualToString:@"estimate"]) {
    [estimateArray addObject:minutes];


}

}
票数 0
EN

Stack Overflow用户

发布于 2014-04-14 20:42:16

这不是一个直接的答案,但根据我的经验,NSXMLParser变得非常困难--您必须深入挖掘xml,我的解决方案总是使用XMLDictionary。当然这并不能解决您的问题,但是XMLDictionary确实使用了NSXMLParser,所以如果您下载这些文件,您将能够看到它们是如何处理更复杂的NSXMLParser结构的。

XMLDictionary实例

记住#导入"XMLDictionary.h“

代码语言:javascript
复制
NSString *googleString = @"http://www.google.com/"; // your xml url
NSDictionary *xml = [NSDictionary dictionaryWithXMLString:googleString];

然后,您可以挖掘字典,以检索您想要的数据。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23069735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档