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

标签 iphone ios

我想要做的是让解析器遍历每一行 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/

相关文章:

iphone - 从 AFImageRequestOperation 中的成功 block 返回图像

html - 在 webview 中快速加载 html 文件,更改图像大小、字体和文本大小

ios - 使用第 5 代或第 4 代 iPod Touch 进​​行开发?

ios - 使用Parse的Facebook登录无法正常工作

java - Objective C 到 Android XML 响应

ios - 在 Xcode "XPC connection interrupted"中获取此消息

iphone - iOS 中的钥匙串(keychain)真的有效吗?

iphone - UISlider 和连续属性

ios - 在 XCTestCase 期间切换互联网可用性

iphone - 是否可以在没有 APNS 的情况下进行推送通知