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

发布于 2024-12-23 09:38:49 字数 3514 浏览 4 评论 0原文

我正在使用 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

i m doing XML parsing using NSXMLParserDelegate..I m getting null value when i print the value in label...I m using simple xml file got in an internet.You can check what is in that file too.anyone pls help..Here is my code

#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

This is my XML file....I want to print XML Data Parsed! in my label.Help me with that.I have edited the code and check now

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

check this:

-(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];

       }

I m getting error:

*** 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

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

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

发布评论

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

评论(2

破晓 2024-12-30 09:38:49

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

可变字符串 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"]; 访问。

It seems you have several errors in your code.

The mutable string xmlString isn't properly initialized, on the line :

xmlString = [NSMutableString alloc]; // wrong

This should look like : xmlString = [[NSMutableString alloc] initWithCapacity:X];.

Also, when the parser finds characters, you replace the entire existing mutable string, instead of appending them to the existing content, or try to because according to the code, you simply make a memory address compare and waste the result (==).

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

This should look like :

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

Finally in didEndElement, I'm not sure what you are trying to achieve, but you add the string object reference to an array, and then set the label text to display the content of the array... as long as the mutable array is well allocated and initialized (what I'm really not sure), the label should display at least the array content. But you might consider to "flush" the mutable string ([xmlString setString:@""];) and not allocate it on every element.

EDIT :

The attributeDict maps attribute names as the keys with their values. I guess your attribute value can be accessed via [attributeDict objectForKey:@"myData"];.

羁拥 2024-12-30 09:38:49

我猜问题出在这一行:

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

== 更改为 =

希望不需要解释。

I guess that the problem is in this line:

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

Change == to =

Hope that explanation isn't needed.

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