2013-11-25 43 views
1

我想弄清楚NSXMLParser,&我不知道为什么这不起作用。我应该把第一个&姓以及年龄,但它输出一个数字。NSXMLParser输出“XMLElement:0x8d61200”,但希望“XMLElement:Richard”

的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.mi有

#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.2 16 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]在应用程序启动结束时,应用程序窗口预计具有根视图控制器

+0

您好,欢迎!只是一个小的请求,如果我可以?任何机会你都可以减少问题中的代码量 - 只需要相关的部分?否则,需要阅读大量代码才能解决可能相当简单的问题。 – Monolo

回答

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 
0

看起来默认描述方法用于打印对象。 在这种情况下,你应该重写该方法在XMLElement类,例如

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