2013-11-25 1 views
1

NSXMLParser를 알아 내려고 시도했습니다. & 왜 이것이 작동하지 않는 것인지 잘 모르겠습니다. 나는 먼저 퍼팅을해야합니다. & 성 (姓)과 성 (姓)은 같지만 번호가 출력됩니다.NSXMLParser "XMLElement : 0x8d61200"을 출력했지만 "XMLElement : Richard"를 원합니다

XML은 내 문제로 점점 오전 내 AppDelegate.mi에서

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

내들은 XMLElement 클래스에서
#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; 

} 

#import "XMLElement.h" 
@implementation XMLElement 


- (NSMutableArray *) subElements{ 

if (_subElements == nil){ 

    _subElements = [[NSMutableArray alloc] init]; 

} 

return _subElements; 

} 
@end 

입니다

2013 -11-21 15 : 59 : 53.2 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] : 59 : 53.216 XML을 [7595 : 70B 응용 프로그램 창은 응용 프로그램 실행 내가

2013년 11월 21일 15 싶어

의 끝에서 루트 뷰 컨트롤러가 예상된다 ] XML이 구문 분석됩니다. 2013년 11월 21일 15 : 59 : 53.219 XML [7595 : 70B ( "61들은 XMLElement" "들은 XMLElement : 리처드" "들은 XMLElement 슨" ) 2013년 11월 21일 15시 59분 : 53.223 XML [7595 : 70b] 응용 프로그램 시작 마지막에 응용 프로그램 창에 루트보기 컨트롤러가 있어야합니다.

+0

안녕과에 오신 것을 환영합니다! 내가 할 수만 있다면 작은 요청이야? 질문에서 코드의 양을 줄일 수있는 기회가 있습니까? 그렇지 않으면 매우 간단한 문제를 해결하는 데 도움이되는 많은 코드가 있습니다. – Monolo

답변

1

를 들어, XMLElement 클래스에서이 메소드를 오버라이드 (override) :

#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

개체를 인쇄하는 데 기본 설명 방법이 사용 된 것 같습니다. 이 경우 당신이 당신이 그것을 설정하는 방법 예를

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