2013-11-25 40 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]; 
}