Create a structure from an xml

0

I'm using the SWXMLHash Framework to read the XML and I need to somehow create the following structure from XML

Structure to be created:

Catalogo
   Book1
    Autor = Gambardella
    Genre = Computer
   Book1 
   Book2
    Autor = Ralls
    Genre = Fantasy
   Book2

XML I'm using

<root>
      <catalog>
        <book id=\"bk101\">
          <author>Gambardella, Matthew</author>
          <title>XML Developer's Guide</title>
          <genre>Computer</genre>
          <price>44.95</price>
          <publish_date>2000-10-01</publish_date>
          <description>An in-depth look at creating applications with XML.</description>
        </book>
        <book id=\"bk102\">
          <author>Ralls, Kim</author>
          <title>Midnight Rain</title>
          <genre>Fantasy</genre>
          <price>5.95</price>
          <publish_date>2000-12-16</publish_date>
          <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>
      </catalog>
    </root>

Swift Code

for retorno in dados["root"]["catalog"]["book"]{
 //
}
    
asked by anonymous 10.06.2016 / 20:11

1 answer

1

Apparently Catalog is an array of books , correct? If you already have XML as a dictionary and the Book class is created, one way to read the dictionary would be as follows:

for rawBook in dados["root"]["catalog"] {
    guard let author = rawBook["author"] as? String,
          let genre = rawBook["genre"] as? String else {
              continue
          }
    let book = Book(autor: author, genre: genre)
    //...
}
    
12.06.2016 / 08:00