2012-04-20 149 views
0

我無法完全理解NSXMLParser的流程以及與它們關聯的委託方法。是否有任何方法或任何示例,詳細說明如何使用NSXMLParser完成分析。我有另一個XML,我需要在解析XMl後將相關的qno,tin,tout和answer存儲到相應的字符串中。 PFB XML。如何使用NSXMLParser將XML的節點存儲到NSArray中

<?xml version="1.0" encoding="UTF-8"?> 
<ParticipantService> 
    <Response> 
     <FileName>CustomerSkillsIntro</FileName> 
     <playlist> 
      <question answer="t" qno="1" tin="71" title="Greet" tout="73"/> 
      <question answer="t" qno="2" tin="74" title="Have Name Tag" tout="77"/> 
      <question answer="t" qno="3" tin="78" title="Greet" tout="83"/> 
      <question answer="t" qno="4" tin="109" title="Helping Do My Job" tout="112"/> 
      <question answer="t" qno="5" tin="131" title="Greet Happily" tout="134"/> 
      <question answer="t" qno="6" tin="141" title="Stay cheerful when resident is crabby" tout="144"/> 
      <question answer="t" qno="7" tin="151" title="Bond with the new resident" tout="154"/> 
      <question answer="t" qno="8" tin="161" title="Welcome cheerfully" tout="164"/> 
      <question answer="t" qno="9" tin="169" title="Offer Help" tout="172"/> 
      <question answer="t" qno="10" tin="178" title="Help with interest" tout="181"/> 
      <question answer="t" qno="11" tin="183" title="Accompany" tout="186"/> 
      <question answer="t" qno="12" tin="189" title="Pay attention to 2 resudents" tout="192"/> 
      <question answer="t" qno="13" tin="199" title="Juggle the two accurately" tout="202"/> 
      <question answer="t" qno="14" tin="207" title="Bring in other help when needed" tout="212"/> 
      <question answer="t" qno="15" tin="219" title="Correct response I can ask" tout="222"/> 
      <question answer="t" qno="16" tin="231" title="Be charming" tout="237"/> 
      <question answer="t" qno="17" tin="247" title="Respond and delegate" tout="250"/> 
      <question answer="t" qno="18" tin="261" title="Apologize" tout="263"/> 
      <question answer="t" qno="19" tin="266" title="Offer activities" tout="270"/> 
      <question answer="t" qno="20" tin="273" title="Be sensitive to needs" tout="276"/> 
      <question answer="t" qno="21" tin="287" title="Offer anything you need" tout="290"/> 
      <question answer="t" qno="22" tin="311" title="Take off shoes, honor unusual request" tout="315"/> 
      <question answer="t" qno="23" tin="328" title="Always available menu explained" tout="331"/> 
      <question answer="t" qno="24" tin="333" title="Willing to stay beyond shift" tout="336"/> 
      <question answer="t" qno="25" tin="377" title="Explain policy" tout="380"/> 
      <question answer="t" qno="26" tin="390" title="Understand resident" tout="396"/> 
     </playlist> 
     <path>lmsstaging.2xprime.com</path> 
     <EncodedVideoURL>HTTP://lmsstaging.2xprime.com/test/vdos/Alzheimers.mp4</EncodedVideoURL> 
    </Response> 
    <RequestStatus> 
     <Code>1</Code> 
     <Status>SUCCESS</Status> 
     <Message/> 
    </RequestStatus> 
</ParticipantService> 

是否有人可以解釋我如何解析這個XML和有關的NSXMLParser和委託方法的工作原理的詳細說明?我想將「tin」和「tout」存儲到NSArray中,但我無法理解如何逐個解析節點。這將非常有幫助。

+0

感謝@Jennis用於格式化代碼。我在如何發佈確切的XML方面遇到困難。 – 2012-04-20 08:37:20

+0

您好:D – 2012-04-20 08:57:50

回答

1

夫婦的委託方法都存在於NSXML Parser-

-(BOOL) parse:(NSData *)xmlData 

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 

您可以使用方法後面的代碼行,你的情況didStartElement-

if([elementName isEqualToString:@"question"]) { 
    NSString *answer = [attributeDict valueForKey:@"answer"]; 
    NSString *qno = [attributeDict valueForKey:@"qno"]; 
    NSString *tin = [attributeDict valueForKey:@"tin"]; 
    NSString *title = [attributeDict valueForKey:@"title"]; 

    // Now You can store these in your preferable data models - data objects/array/dictionary 
} 

編輯 -

@interface Question : NSObject 

@property (nonatomic, retain) NSString *answer; 
@property (nonatomic, retain) NSString *qno; 
@property (nonatomic, retain) NSString *tin; 
@property (nonatomic, retain) NSString *title; 

@end 

@implementation Question 

@synthesize answer = _answer; 
@synthesize qno = _qno; 
@synthesize tin = _tin; 
@synthesize title = _title; 

- (void) dealloc { 
    self.answer = nil; 
    self.qno = nil; 
    self.tin = nil; 
    self.title = nil; 

    [super dealloc];   
} 
@end 

現在在您的didStartElement方法中 -

NSMutableArray *questionsArray = [NSMutableArray array]; 
if([elementName isEqualToString:@"question"]) { 
    Question *questionObject = [[Question alloc] init]; 
    questionObject.answer = [attributeDict valueForKey:@"answer"]; 
    questionObject.answerqno = [attributeDict valueForKey:@"qno"]; 
    questionObject.answertin = [attributeDict valueForKey:@"tin"]; 
    questionObject.answertitle = [attributeDict valueForKey:@"title"]; 

    [questionsArray addObject:questionObject]; 
    [questionObject release]; 
} 

您可以在課堂級別創建此數組,並在需要的地方使用。

EDIT 2 - 向從陣列 -

//Suppose dataArray contains information - 
for (int i = 0; i < [dataArray count]; i++) { 
    Question *obj = [dataArray objectAtIndex:i]; 
    NSLog(@"%@",obj.answer); 
    NSLog(@"%@",obj.qno); 
    NSLog(@"%@",obj.tin); 
    NSLog(@"%@",obj.title); 
} 
+0

'didEndElement'沒有'attributeDict'參數 – MrTJ 2012-04-20 08:33:15

+1

對不起,它是didStartElement。 – rishi 2012-04-20 08:49:07

+0

@RIP:在我的例子中,我有26個問題。如何存儲特定於某個問題的所有值?我的意思是我對每個問題沒有不同的價值觀。我如何存儲每個值,單獨區分問題否? – 2012-04-20 09:13:36

2
NSXMLParser

是所謂基於事件的XML解析器SAX type解析器提取數據。它從頭開始讀取您的XML,每次它找到一個新元素,一個關閉元素或字符數據時,它會通知您。這是通過委託完成的,如果這些事件通過實現回調函數發生,你必須指定你想要做什麼。當您解析示例XML,它會調用或多或少這些功能:

parser:yourParser didStartElement:@"playlist" namespaceURI:@"" qualifiedName:@"" attributes:attribDict 
// attribDict empty 

parser:yourParser didStartElement:@"question" namespaceURI:@"" qualifiedName:@"" attributes:attribDict 
// attribDict = {@"answer" -> @"t", @"qno" -> @"2", @"tin" -> @"71", @"title" -> @"Greet", @"tout" -> @"73"} 

parser:yourParser didEndElement:@"question" namespaceURI:@"" qualifiedName:@"" 

// ...repeating the last two calls for each question... 

parser:yourParser didEndElement:@"playlist" namespaceURI:@"" qualifiedName:@"" 

所以,你應該實現didStartElement是這樣的:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict { 
    if (elementName == @"question") { 
    // save the elements of attributeDict in your array 
    } 
} 
相關問題