NSXMLParser输出“XMLElement:0x8d61200”但想要“XMLElement:Richard”

时间:2013-11-25 15:49:28

标签: ios iphone objective-c nsxmlparser

我正在试图找出NSXMLParser,&我不知道为什么这不起作用。我应该把First&姓氏和年龄,但它输出一个数字。

XML

<?xml version="1.0" encoding="UTF-8"?>

<root>

<person id="1">

    <firstName>Anthony</firstName>

    <lastName>Robbins</lastName>

    <age>51</age>

</person>

<person id="2">

    <firstName>Richard</firstName>

    <lastName>Branson</lastName>

    <age>61</age>

 </person>

</root>

在我的AppDelegate.m中我有

#import "AppDelegate.h"
#import "XMLElement.h"

@interface AppDelegate () <NSXMLParserDelegate>

@property (nonatomic, strong) NSXMLParser *xmlParser;

@property (nonatomic, strong) XMLElement *rootElement;

@property (nonatomic, strong) XMLElement *currentElementPointer;

@end



@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

NSString *xmlFilePath = [[NSBundle mainBundle] pathForResource:@"MyHTML"
                                                        ofType:@"HTML"];

NSData *xml = [[NSData alloc] initWithContentsOfFile:xmlFilePath];

self.xmlParser = [[NSXMLParser alloc] initWithData:xml];

self.xmlParser.delegate = self;

if ([self.xmlParser parse]){

    NSLog(@"The XML is parsed.");


    /* self.rootElement is now the root element in the XML */

    XMLElement *element = self.rootElement.subElements[1];

    NSLog(@"%@", element.subElements);



} else{

    NSLog(@"Failed to parse the XML");

}

self.window = [[UIWindow alloc] initWithFrame:

               [[UIScreen mainScreen] bounds]];

self.window.backgroundColor = [UIColor whiteColor];

[self.window makeKeyAndVisible];

return YES;

}

- (void)parserDidStartDocument:(NSXMLParser *)parser{

self.rootElement = nil;

self.currentElementPointer = nil;

}

  - (void)        parser:(NSXMLParser *)parser

    didStartElement:(NSString *)elementName

       namespaceURI:(NSString *)namespaceURI

      qualifiedName:(NSString *)qName

         attributes:(NSDictionary *)attributeDict{

  if (self.rootElement == nil){

    /* We don't have a root element. Create it and point to it */

    self.rootElement = [[XMLElement alloc] init];

    self.currentElementPointer = self.rootElement;

  } else {

    /* Already have root. Create new element and add it as one of

     the subelements of the current element */

    XMLElement *newElement = [[XMLElement alloc] init];

    newElement.parent = self.currentElementPointer;

    [self.currentElementPointer.subElements addObject:newElement];

    self.currentElementPointer = newElement;

  }

  self.currentElementPointer.name = elementName;

  self.currentElementPointer.attributes = attributeDict;

  }


  - (void)        parser:(NSXMLParser *)parser

    foundCharacters:(NSString *)string{

if ([self.currentElementPointer.text length] > 0){

    self.currentElementPointer.text =

    [self.currentElementPointer.text stringByAppendingString:string];

} else {

    self.currentElementPointer.text = string;

  }
}


  - (void)        parser:(NSXMLParser *)parser

      didEndElement:(NSString *)elementName

       namespaceURI:(NSString *)namespaceURI

      qualifiedName:(NSString *)qName{



self.currentElementPointer = self.currentElementPointer.parent;
 }


- (void)parserDidEndDocument:(NSXMLParser *)parser{

self.currentElementPointer = nil;

 }

在我的XMLElement类中是

 #import "XMLElement.h"
@implementation XMLElement


- (NSMutableArray *) subElements{

if (_subElements == nil){

    _subElements = [[NSMutableArray alloc] init];

}

return _subElements;

}
@end

我正在解决问题

2013-11-21 15:59:53.216 XML [7595:70b]解析XML。 2013-11-21 15:59:53.219 XML [7595:70b]( “XMLElement:0x8d61200”, “XMLElement:0x8d61270”, “XMLElement:0x8d612e0” ) 2013-11-21 15:59:53.223 XML [7595:70b]应用程序窗口在应用程序启动结束时应该有一个根视图控制器

我想要

2013-11-21 15:59:53.216 XML [7595:70b]解析XML。 2013-11-21 15:59:53.219 XML [7595:70b]( “XMLElement:理查德”, “XMLElement:布兰森”, “XMLElement:61” ) 2013-11-21 15:59:53.223 XML [7595:70b]应用程序窗口在应用程序启动结束时应该有一个根视图控制器

2 个答案:

答案 0 :(得分:1)

这就是你设置它的方式:

#import "XMLElement.h"
@implementation XMLElement

- (NSMutableArray *) subElements {
    if (_subElements == nil){
        _subElements = [[NSMutableArray alloc] init];
    }
    return _subElements;
 }

- (NSString *)description {
    return [NSString stringWithFormat:"XMLElement: %@", self.text];
}

@end

答案 1 :(得分:0)

似乎默认描述方法用于打印对象。 在这种情况下,您应该在XMLElement类中覆盖该方法,例如

- (NSString *)description
{
    return [NSString stringWithFormat:"XMLElement: %@", self.text];
}