2011-03-15 39 views
0

我一直在玩XML,經歷了一些教程,但我的需求是特定的。假設你有一個CCLabel,你想從XML文件中加載數據到它。因此,在場景x中,您從XML的相關部分加載數據。例如,想象一下,基於場景提供頁碼。將XML數據加載到Cocos2D的文本字段中Mac

有沒有人有任何的例子,或任何人都可以指向我,我應該讀的任何東西?

問候。

回答

0

要閱讀XML,請參閱NSXMLParser指南。管理XML的結構取決於你,但實際上,當你從解析器中接收到讀取事件時,使用它讀取的文本應該是微不足道的。

假設你的XML文件看起來像:

<scene id=1> 
    <label id=1 text="Label Text" /> 
</scene> 
<scene id=2> 
    <label id=2 text="Scene 2 Text" /> 
</scene> 

你有兩個選擇 - 你可以在每次現場加載解析該文件,忽略比ID的內部其他一切= x(其中x是與id相關的某種場景引用),或者您可以在應用程序啓動時加載整個xml並將其存儲爲某種結構。我更喜歡第一種選擇。

作爲一個基本的例子(這仍然是更好,我聯繫到文檔中解釋),你的元素閱讀功能可以這樣做:

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

    if ([elementName isEqualToString:@"label"]) { 
    //attributeDict contains the attributes of the element, 
    //in your case, "text" is one of the keys, and "Label Text" would be the value 
    //this would pass the specific label text from the xml file to a function in your scene 
    [scene someFunctionForPopulatingLabel:[attributeDict objectForKey:@"text"]]; 
    } 

} 
相關問題