2012-05-21 52 views
0

我知道它可能看起來像XML解析帖子的副本,但我真的不能理解節點和委託方法的行爲。我有一個XML ..如何使用帶有不同節點的NSXMLParser解析XML?

<?xml version="1.0" encoding="UTF-8"?> 
<ParticipantService> 
    <Response> 
     <CourseProperties> 
      <CourseTitle>AICC_Flash_Workshop_PPT_to_web_examples</CourseTitle> 
      <CourseCode>123456</CourseCode> 
      <Availability>Open</Availability> 
      <Status>In Progress</Status> 
      <ImageLink>HTTP://lmsstaging.2xprime.com/images/inprogress_icon.png</ImageLink> 
      <CategoryCode>0</CategoryCode> 
      <CategoryDesc>General</CategoryDesc> 
     </CourseProperties> 
     <CourseProperties> 
      <CourseTitle>Behaviours</CourseTitle> 
      <CourseCode>OBIUS</CourseCode> 
      <Availability>Open</Availability> 
      <Status>In Progress</Status> 
      <ImageLink>HTTP://lmsstaging.2xprime.com/images/inprogress_icon.png</ImageLink> 
      <CategoryCode>0</CategoryCode> 
      <CategoryDesc>General</CategoryDesc> 
     </CourseProperties> 
     <CourseProperties> 
      <CourseTitle>Customer Service Skills (Part - one)</CourseTitle> 
      <CourseCode>css_1</CourseCode> 
      <Availability>Open</Availability> 
      <Status>In Progress</Status> 
      <ImageLink>HTTP://lmsstaging.2xprime.com/images/inprogress_icon.png</ImageLink> 
      <CategoryCode>0</CategoryCode> 
      <CategoryDesc>General</CategoryDesc> 
     </CourseProperties> 

....

我的要求是,以存儲相關課程內容到相應的數組。所以我宣佈了6個nsmutablearray,但卻對如何從XMl中檢索數據感到困惑。我這樣

想出來的foundCharacters方法,我追加字符串的值作爲

videoUrlLink = [NSMutableString stringWithString:string]; 

和didEndElement方法

if ([elementName isEqualToString:@"CourseTitle"]) { 
     [courseDetailList addObject:string]; 

    } 

但在XML我能夠結束僅在數組中存儲一個值。請讓我知道我是否在某個地方出了問題?

回答

1

我假設你有一個名爲Course類和Course對象都有titlecodeavailability等特性。

使iVar currentCourse

然後,在你parser:didStartElement:namespaceURI:qualifiedName:attributes:(注:沒有開始,沒有結束!)方法:

if ([elementName isEqualToString:@"CourseProperties"]) { 
    //create a new course object 
    currentCourse = [[Course alloc] init]; 
} 

這使上下文後面的課程的所有屬性。在didEndElement:方法,你基本上所有的課程屬性做到這一點:

if ([elementName isEqualToString:@"CourseTitle"]) { 
    [currentCourse setTitle:string]; 
} 

而且,最後但並非最不重要的,一旦CourseProperties結束標記被發現,保存在某個地方新課程(也didEndElement:):

if ([elementName isEqualToString:@"CourseProperties"]) { 
    //create a new course object 
    [allMyCourses addObject:currentCourse]; 
    currentCourse = nil; 
} 
+0

在這種情況下如何從存儲的數據中檢索值?我的意思是如何獲得所有課程名稱,可用性,狀態e.t.c ....? –

+0

你已經提到在didEndElement:方法中爲currentCourse添加字符串。但是在這個元素中,你將不會擁有字符串屬性。你可以檢查一下嗎? –

相關問題