我想要做的是让解析器遍历每一行 XML,然后将元素分配给一个对象。在行的末尾,对象被添加到 NSMutableString。我只想为每一行 XML 设置一个 faxRecipient 对象。
这是 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>
但是我的代码有问题,我不知道从这里去哪里:
-(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;
}
}
最佳答案
您的 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 中的当前位置的硬编码,那将是最好的。
关于iphone - 如何使用 NSXMLParser 正确设置我的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6184134/