gpt4 book ai didi

objective-c - IOS 中的 XML 解析在标签中显示 NULL 值?

转载 作者:行者123 更新时间:2023-11-28 23:09:24 24 4
gpt4 key购买 nike

我正在使用 NSXMLParserDelegate 进行 XML 解析。当我在标签中打印值时,我得到的是空值...我正在使用从互联网上获取的简单 xml 文件。您也可以检查该文件中的内容。任何人都请帮忙..这是我的代码

#import "xmlViewController.h"

@implementation xmlViewController
@synthesize label;

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.bonifacedesigns.com/tuts/xmltest.xml"]];
receivedData=[[NSMutableData alloc]initWithContentsOfURL:url];
dataParser=[[NSXMLParser alloc]initWithData:receivedData];
dataParser.delegate=self;
[dataParser parse];
}

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
// if(!xmlString){
//
// }else
// {
// [xmlString setString:@""];
// }
if([elementName isEqualToString:@"elements"])
{
xmlString=[NSMutableString alloc];
[xmlString setString:@""];
}

}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
xmlString= (NSMutableString *)[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{

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

NSString *string=xmlString;
[xmlArray addObject:string];
label.text=[NSString stringWithFormat:@"%@",xmlArray];

}

}

@end

这是我的 XML 文件....我想打印已解析的 XML 数据!在我的标签中。帮助我。我已经编辑了代码并现在检查

<elements>
<element myData="XML Data Parsed!"/>
</elements>

检查这个:

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
// if(!xmlString){
//
// }else
// {
// [xmlString setString:@""];
// }


if([elementName isEqualToString:@"elements"])
{

xmlString=[[NSMutableString alloc]initWithCapacity:5];
}

}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
xmlString= (NSMutableString *)[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{

if([elementName isEqualToString:@"element"]){
[attributeDict objectForKey:@"myData"];
[xmlString setString:@""];
NSString *string=xmlString;
[xmlArray addObject:string];
label.text=[NSString stringWithFormat:@"%@",xmlArray];

}

我遇到错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to mutate immutable object with setString:'
*** First throw call stack:
(0x13ba052 0x154bd0a 0x1362a78 0x13629e9 0x1387970 0x2a26 0x9e1a35 0x5171532 0x9e002d 0x26b8 0xd764e 0x37a73 0x37ce2 0x37ea8 0x3ed9a 0xfbe6 0x108a6 0x1f743 0x201f8 0x13aa9 0x12a4fa9 0x138e1c5 0x12f3022 0x12f190a 0x12f0db4 0x12f0ccb 0x102a7 0x11a9b 0x2238 0x2195 0x1)
terminate called throwing an exception

最佳答案

您的代码中似乎有几个错误。

可变字符串 xmlString 未正确初始化,在线:

xmlString = [NSMutableString alloc]; // wrong

这应该看起来像:xmlString = [[NSMutableString alloc] initWithCapacity:X];

此外,当解析器找到字符时,您替换整个现有可变字符串,而不是将它们附加到现有内容,或者尝试这样做,因为根据代码,您只是简单地进行内存地址比较并浪费结果(==)。

// wrong
xmlString == (NSMutableString *)[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

这应该是这样的:

[xmlString appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];

最后在didEndElement中,我不确定你要实现什么,但是你将字符串对象引用添加到一个数组,然后设置标签文本来显示数组的内容...只要可变数组得到很好的分配和初始化(我真的不确定),标签应该至少显示数组内容。但是您可能会考虑“刷新”可变字符串 ([xmlString setString:@""];),而不是在每个元素上分配它。

编辑:

attributeDict 将属性名称映射为键及其值。我想您的属性值可以通过 [attributeDict objectForKey:@"myData"]; 访问。

关于objective-c - IOS 中的 XML 解析在标签中显示 NULL 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8646262/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com