2017-07-04 44 views
0

我有一個​​,但解析器中的函數能夠獲取xml文件的第一個對象,但無法將xml文件的所有類別都取到mutable array中。這裏是功能:XML解析器只加載第一個對象

let xmlparser = XmlParser() 
    xmlparser.loadRssFeed("FaceMask") 
    faceMaskArray = (xmlparser.faceMaskArray.firstObject as! FaceMaskCategories).faceMaskItemArray 
    collectionArray = faceMaskArray 

我的XML文件是這樣

<?xml version="1.0" encoding="utf-8"?> 
<facemasks> 
    <categories> 
     <category > 
      <id>0</id> 
      <title>Animals</title> 
      <image_name> leopard</image_name> 
      <items> 

       <item> 
        <landmarks_file_name>leopard</landmarks_file_name> 
        <item_id>1101</item_id> 
        <mask_title>leopard</mask_title> 
        <mask_image_name>leopard.png</mask_image_name> 
       </item> 

      </items> 
     </category> 
     <category > 
      <id>1</id> 
      <title>Animal</title> 
      <image_name>leopard</image_name> 
      <items> 
       <item> 
        <landmarks_file_name>leopard</landmarks_file_name> 
        <item_id>1102</item_id> 
        <mask_title>leopard</mask_title> 
        <mask_image_name>leopard.png</mask_image_name> 
       </item> 
      </items> 
     </category> 
     <category > 
      <id>2</id> 
      <title>Animals</title> 
      <image_name>tiger</image_name> 
      <items> 
       <item> 
        <landmarks_file_name>leopard</landmarks_file_name> 
        <item_id>1103</item_id> 
        <mask_title>tiger</mask_title> 
        <mask_image_name>tiger.png</mask_image_name> 
       </item> 
      </items> 
     </category> 
     <category > 
      <id>3</id> 
      <title>Animals</title> 
      <image_name>tiger</image_name> 
      <items> 
       <item> 
        <landmarks_file_name>leopard</landmarks_file_name> 
        <item_id>1104</item_id> 
        <mask_title>tigerz</mask_title> 
        <mask_image_name>tiger.png</mask_image_name> 
       </item> 
      </items> 
     </category> 
    </categories> 
</facemasks> 

該功能只獲得了第一個對象或從xml file正確的最後一個對象。任何人都可以請指導我如何正確地從xml文件中獲取所有類別。我非常感謝幫助。謝謝。

回答

0

檢查這個代碼:

func beginParsing() 
{ 
posts = [] 
parser = NSXMLParser(contentsOfURL: 
(NSURL(string:"http://images.apple.com/main/rss/hotnews/hotnews.rss"))!)! 
parser.delegate = self 
parser.parse() 
tbData!.reloadData() 
} 

這裏是越來越整個陣列分析器功能:

func parser(parser: NSXMLParser, didStartElement elementName: String, 
namespaceURI: String?, qualifiedName qName: String?, attributes 
attributeDict: [String : String]) 
{ 
element = elementName 
if (elementName as NSString).isEqualToString("item") // Replace this item with category. 
{ 
elements = NSMutableDictionary() 
elements = [:] 
title1 = NSMutableString() 
title1 = "" 
date = NSMutableString() 
date = "" 
} 
} 

它從陣列中的所有項目,「項目」實際上是標籤或每數組中的項目,就像示例XML中的「category」一樣。

這是樣本XML:

http://images.apple.com/main/rss/hotnews/hotnews.rss#sthash.1Mq673bO.dpuf

,並從每個項目分析數據:

func parser(parser: NSXMLParser!, foundCharacters string: String!) 
{ 
if element.isEqualToString("title") { 
title1.appendString(string) 
} else if element.isEqualToString("pubDate") { 
date.appendString(string) 
} 
} 

我使用本教程,它的正常工作:

http://www.theappguruz.com/blog/xml-parsing-using-nsxmlparse-swift