XML解析iPhone后数组计数为1

发布于 2024-12-14 05:19:53 字数 3597 浏览 0 评论 0原文

我正在 iPhone 应用程序中解析航班时间。

下面是 XML 中的一个片段

<?xml version="1.0" encoding="iso-8859-1"?>
<airport xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<flights lastUpdate="2011-11-07T10:49:43">
<flight uniqueID="2131560">
  <airline>DY</airline>
  <flight_id>DY1052</flight_id>
  <dom_int>S</dom_int>
  <schedule_time>2011-11-04T06:00:00</schedule_time>
  <arr_dep>D</arr_dep>
  <airport>SVG</airport>
  <via_airport>CPH</via_airport>
  <check_in>C</check_in>
</flight>
<flight uniqueID="2136807">
  <airline>SK</airline>
  <flight_id>SK308</flight_id>
  <dom_int>D</dom_int>
  <schedule_time>2011-11-07T07:15:00</schedule_time>
  <arr_dep>D</arr_dep>
  <airport>SVG</airport>
  <via_airport>SVG</via_airport>
  <check_in>EF</check_in>
</flight>
...

我将每个航班存储在 Flight 对象中,如下所示:

@interface Flights : NSObject{
NSInteger flightUniqueID;
NSString *airline;
NSString *flight_id;
NSString *dom_int;
NSDate *schedule_time;
NSString *arr_dep;
NSString *airport;
NSString *check_in;
NSString *status;
NSString *via_airport;
NSString *gate;
NSString *delayed;

}
@property(nonatomic, readwrite)NSInteger flightUniqueID;
@property(nonatomic, retain)NSString *airline;
@property(nonatomic, retain)NSString *flight_id;
@property(nonatomic, retain)NSString *dom_int;
@property(nonatomic, retain)NSDate *schedule_time; 
@property(nonatomic, retain)NSString *arr_dep;
@property(nonatomic, retain)NSString *airport;
@property(nonatomic, retain)NSString *check_in;
@property(nonatomic, retain)NSString *status;
@property(nonatomic, retain)NSString *via_airport;
@property(nonatomic, retain)NSString *gate;
@property(nonatomic, retain)NSString *delayed;

我使用 NSXMLParser 来解析 XML

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

if ([elementName isEqualToString:@"airport"]) {
    allFlights = [[NSMutableArray alloc]init];
}
else if([elementName isEqualToString:@"flight"]){
    flight = [[Flights alloc]init];

    flight.flightUniqueID = [[attributeDict objectForKey:@"uniqueID"]integerValue];

}

}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 
  if(!currentElementValue) 
    currentElementValue = [[NSMutableString alloc] initWithString:string];
else
    [currentElementValue appendString:string];


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

if ([elementName isEqualToString:@"flights"]) {
    return;
}
if ([elementName isEqualToString:@"flight"]) {
    NSLog(@"Adding object: %@" , flight);
    [allFlights addObject:flight];

    [flight release];
    flight = nil;
}else
    [flight setValue:currentElementValue forKey:elementName];

   [currentElementValue release];
currentElementValue = nil;
}

当我使用 NSLog“航班”时,我得到以下输出:

Adding object: <Flights: 0xsomenumber>
...
Adding object: <Flights: 0x7064360>

无论航班中有多少航班,该输出都会继续xml。

但是,我添加每个对象的数组仅存储最后一个飞行对象。如果我在解析完成后尝试对数组进行 NSLog,它会打印出最后一个对象,而不会打印出其他内容。

显然,我需要将所有航班存储在数组中,以便在解析完成后我可以在表格视图中显示所有航班。

我怎样才能实现这个目标?我错过了什么吗?

我尝试在不释放航班对象的情况下解析 XML,但这也不起作用。

I'm parsing flight times in my iPhone app.

Here's a snippet from the XML

<?xml version="1.0" encoding="iso-8859-1"?>
<airport xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<flights lastUpdate="2011-11-07T10:49:43">
<flight uniqueID="2131560">
  <airline>DY</airline>
  <flight_id>DY1052</flight_id>
  <dom_int>S</dom_int>
  <schedule_time>2011-11-04T06:00:00</schedule_time>
  <arr_dep>D</arr_dep>
  <airport>SVG</airport>
  <via_airport>CPH</via_airport>
  <check_in>C</check_in>
</flight>
<flight uniqueID="2136807">
  <airline>SK</airline>
  <flight_id>SK308</flight_id>
  <dom_int>D</dom_int>
  <schedule_time>2011-11-07T07:15:00</schedule_time>
  <arr_dep>D</arr_dep>
  <airport>SVG</airport>
  <via_airport>SVG</via_airport>
  <check_in>EF</check_in>
</flight>
...

I store each flight in a Flight object which looks like this:

@interface Flights : NSObject{
NSInteger flightUniqueID;
NSString *airline;
NSString *flight_id;
NSString *dom_int;
NSDate *schedule_time;
NSString *arr_dep;
NSString *airport;
NSString *check_in;
NSString *status;
NSString *via_airport;
NSString *gate;
NSString *delayed;

}
@property(nonatomic, readwrite)NSInteger flightUniqueID;
@property(nonatomic, retain)NSString *airline;
@property(nonatomic, retain)NSString *flight_id;
@property(nonatomic, retain)NSString *dom_int;
@property(nonatomic, retain)NSDate *schedule_time; 
@property(nonatomic, retain)NSString *arr_dep;
@property(nonatomic, retain)NSString *airport;
@property(nonatomic, retain)NSString *check_in;
@property(nonatomic, retain)NSString *status;
@property(nonatomic, retain)NSString *via_airport;
@property(nonatomic, retain)NSString *gate;
@property(nonatomic, retain)NSString *delayed;

I use an NSXMLParser to parse the XML

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

if ([elementName isEqualToString:@"airport"]) {
    allFlights = [[NSMutableArray alloc]init];
}
else if([elementName isEqualToString:@"flight"]){
    flight = [[Flights alloc]init];

    flight.flightUniqueID = [[attributeDict objectForKey:@"uniqueID"]integerValue];

}

}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 
  if(!currentElementValue) 
    currentElementValue = [[NSMutableString alloc] initWithString:string];
else
    [currentElementValue appendString:string];


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

if ([elementName isEqualToString:@"flights"]) {
    return;
}
if ([elementName isEqualToString:@"flight"]) {
    NSLog(@"Adding object: %@" , flight);
    [allFlights addObject:flight];

    [flight release];
    flight = nil;
}else
    [flight setValue:currentElementValue forKey:elementName];

   [currentElementValue release];
currentElementValue = nil;
}

When i NSLog "flight", I get this output:

Adding object: <Flights: 0xsomenumber>
...
Adding object: <Flights: 0x7064360>

which goes on for however many flights there are in the xml.

However, the array I add each object to only stores the last flight object. If I try to NSLog the array after the parsing is done, it prints out the last object, and nothing else.

Obviously I need to have all the flights stored in the array, so I can show all of them in my tableview after the parsing is done.

How can I achieve this? Am I missing something?

I tried to parse the XML without releasing the flight object, but that didn't work either.

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

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

发布评论

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

评论(2

烙印 2024-12-21 05:19:53

您将 allFlights 设置为 airport 元素上的空数组。我的猜测是你想在启动时初始化它一次。

但是,每个航班元素中都有嵌套的机场元素,因此您最终只能得到最后一个航班。尝试在其他地方初始化 allFlights。

You set allFlights to an empty array upon airport elements. My guess is that you want to initialize it once upon start.

However, there are nested airport elements in each flight element, so you end up only with the last flight. Try initializing allFlights somewhere else.

热风软妹 2024-12-21 05:19:53

解析完成后,尝试

NSLog(@"the number of objects are -> %d", [Your Array object count]);

看看显示了什么????

如果它显示正确的对象数量,则意味着所有对象都已添加。
你可以使用像

for (Flight *objFlight in arrFlights)
{
    NSLog (@"Flight Name -> %@",objFlight.strFlihtName);
}

快乐编码这样的循环来打印它们......

After the completion of parsing operation , try

NSLog(@"the number of objects are -> %d", [Your Array object count]);

and see what does it show ?????

If it shows correct number of objects , that means all the objects are added.
and you may print them using looping like

for (Flight *objFlight in arrFlights)
{
    NSLog (@"Flight Name -> %@",objFlight.strFlihtName);
}

Happy Coding...

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