如何使用NSXMLParser正确设置我的对象?

如何使用NSXMLParser正确设置我的对象?

问题描述:

我想要做的是让解析器遍历XML的每一行,然后将这些元素分配给一个对象。在该行的末尾,该对象被添加到NSMutableString。我只想要为每一行XML设置一个faxRecipient对象。如何使用NSXMLParser正确设置我的对象?

这里是出把XML的一个样本:

<ContactId>23</ContactId><Name>name1</Name><Fax>+12345222</Fax><Company /><Rec>0</Rec><ContactId>24</ContactId><Name>name2</Name><Fax>+78903333</Fax><Company /><Rec>0</Rec> 

我的代码却是越野车,我不知道在哪里何去何从:在viewDidLoad方法

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

    if ([elementName isEqual:@"ContactId"]) { 
     faxRecipient =[[FaxRecipient alloc]init]; 
     remoteRecipientString = [[NSMutableString alloc]init]; 
    } 

    else if ([elementName isEqual:@"Name"]) { 
     faxRecipient =[[FaxRecipient alloc]init]; 
     remoteRecipientString = [[NSMutableString alloc]init]; 


    }else if ([elementName isEqual:@"Fax"]) { 
     faxRecipient =[[FaxRecipient alloc]init]; 
     remoteRecipientString = [[NSMutableString alloc]init]; 
    } 
    else if ([elementName isEqual:@"Company"]) { 
    faxRecipient =[[FaxRecipient alloc]init]; 
    remoteRecipientString = [[NSMutableString alloc]init]; 
    } 

} 

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

    [remoteRecipientString appendString:string]; 

} 


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


    if ([elementName isEqual:@"ContactId"]) { 
     faxRecipient.contactID = remoteRecipientString; 
     [remoteRecipientItems addObject:faxRecipient]; 
     [faxRecipient release]; 
     faxRecipient = nil; 
     [remoteRecipientString release]; 
     remoteRecipientString = nil; 
    } 


    if ([elementName isEqual:@"Name"]) { 
     faxRecipient.name = remoteRecipientString; 
     [remoteRecipientItems addObject:faxRecipient]; 
     [faxRecipient release]; 
     faxRecipient = nil; 
     [remoteRecipientString release]; 
     remoteRecipientString = nil; 
    } 


    if ([elementName isEqual:@"Fax"]) { 
     faxRecipient.fax = remoteRecipientString; 
     [remoteRecipientID addObject:faxRecipient]; 
     [remoteRecipientString release]; 
     remoteRecipientString = nil; 
     [faxRecipient release]; 
     faxRecipient = nil; 
    } 

    if ([elementName isEqual:@"Company"]) { 
     faxRecipient.company = remoteRecipientString; 
     [remoteRecipientID addObject:faxRecipient]; 
     [remoteRecipientString release]; 
     remoteRecipientString = nil; 
     [faxRecipient release]; 
     faxRecipient = nil; 
    } 


} 
+0

如果以下答案无效,请务必提供有关您遇到的问题的其他详细信息。 – CodeGuru 2011-06-01 21:02:08

您的XML是否传回*元素名称,例如:

<FaxRecipient><ContactId>23</ContactId><Name>name1</Name><Fax>+12345222</Fax><Company /><Rec>0</Rec><ContactId>24</ContactId><Name>name2</Name><Fax>+78903333</Fax><Company /><Rec>0</Rec> </FaxRecipient> 

如果是,那么你应该在你的didStart和你DidEnd如检查是开始元素:

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *) elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 
{ 
if ([elementName isEqualToString:@"FaxRecipient"]) 
    { 
    faxRecipient =[[FaxRecipient alloc]init]; 
    } 
else if ([elementName isEqualToString:@"ContactId"] || 
     [elementName isEqualToString:@"Name"] || 
        [elementName isEqualToString:@"Fax"] || 
     [elementName isEqualToString:@"Company"]) 
{ 
remoteRecipientString = [[NSMutableString alloc]init]; 
} 
} 

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *) elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ 
if ([elementName isEqual:@"FaxRecipient"]) 
    { 
    [remoteRecipientItems addObject:faxRecipient]; 
    [faxRecipient release]; 
    faxRecipient = nil; 
    } 
else if ([elementName isEqual:@"ContactId"]) { 
    faxRecipient.contactID = remoteRecipientString; 
} 
else if ([elementName isEqual:@"Name"]) { 
    faxRecipient.name = remoteRecipientString; 
} 
else if ([elementName isEqual:@"Fax"]) { 
    faxRecipient.fax = remoteRecipientString; 
} 
else if ([elementName isEqual:@"Company"]) { 
    faxRecipient.company = remoteRecipientString; 
} 

if ([elementName isEqualToString:@"ContactId"] || 
    [elementName isEqualToString:@"Name"] || 
    [elementName isEqualToString:@"Fax"] || 
    [elementName isEqualToString:@"Company"]) 
{ 
[remoteRecipientString release]; 
    remoteRecipientString = nil; } 
} 

如果没有,那么你就需要检查你即将解析第一个元素(ContactId)并在那里执行didStartElement中的附加初始化。然后在DidEndElement中,当刚刚找到最后一个元素(Rec)时,执行addobject。如果您的XML的结构方式能够识别它返回的对象,而不仅仅是基于返回的XML中元素的当前位置的硬代码,那将是最好的。

+0

是的,我没有“FaxRecipient”类型的机箱。我确实尝试了你的第二种方法,但它可以正常工作,但是我收到了内存泄漏。请参阅:http://*.com/questions/6273931/where-is-the-memory-leak-in-my-uitableviewcontroller/6274009#comment-7322888 – jini 2011-06-08 05:27:59

的Alloc您的NSMutableString然后将字符串附加到此可修改字符串

如果您只是在读取数据,我建议使用TBXML进行解析,因为它比NSXMLParser(imo)更快更容易使用。

这里是哪里,我从元素以文本,并给予他们一个对象我的分析方法之一:

- (void)parseWeekEvents 
{  
TBXML *tbxml; 
TBXMLElement *rootXMLElement; 
TBXMLElement *node_channel; 
TBXMLElement *node_item; 
TBXMLElement *node_traverse; 

NSString *fullEventURL; 
fullEventURL = @"http://www.millersville.edu/calendar/rss.php?q=&c=&date=01-06-2011&mode=index"; 

self.eventsDict = [[NSMutableDictionary alloc] init]; 
self.datesArray = [[NSMutableArray alloc] init]; 
[eventsDict release]; 
[datesArray release]; 

tbxml = [TBXML tbxmlWithURL:[NSURL URLWithString:fullEventURL]]; 
rootXMLElement = tbxml.rootXMLElement; 

if(rootXMLElement) 
{ 
    node_channel = [TBXML childElementNamed:@"channel" parentElement:rootXMLElement]; 

    if(node_channel) 
    { 
     node_item = [TBXML childElementNamed:@"item" parentElement:node_channel]; 

     while(node_item) 
     { 
      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

      EventArticleObject *currentEvent = [[[EventArticleObject alloc] init] autorelease]; 

      NSString *title; 
      NSString *link; 
      NSString *date; 

      node_traverse = [TBXML childElementNamed:@"title" parentElement:node_item]; 
      title = [TBXML textForElement:node_traverse]; 

      [currentEvent setTitle:title]; 

      node_traverse = [TBXML childElementNamed:@"link" parentElement:node_item]; 
      link = [TBXML textForElement:node_traverse]; 
      [currentEvent setLink:link]; 

      node_traverse = [TBXML childElementNamed:@"description" parentElement:node_item]; 
      description = [TBXML textForElement:node_traverse]; 
      [currentEvent setDescription:description]; 

      node_traverse = [TBXML childElementNamed:@"pubDate" parentElement:node_item]; 
      date = [TBXML textForElement:node_traverse]; 

      if(![datesArray containsObject:date]) 
      { 
       [datesArray addObject:date]; 
      } 

      NSString *eventDate = [currentEvent date]; 
      NSMutableArray *temp = [eventsDict objectForKey:eventDate]; 
      if(!temp) 
      { 
       temp = [NSMutableArray array]; 
       [temp addObject:currentEvent]; 
       [eventsDict setObject:temp forKey:eventDate]; 
      } else { 
       [temp addObject:currentEvent]; 
      } 
      node_item = node_item -> nextSibling; 

      [pool drain]; 
     } 
    } 
    } 
} 

希望这会有所帮助,让我知道如果您有任何问题。