How to access grand child element?

为君一笑 提交于 2019-12-24 00:09:58

问题


I'm trying to learn XMLMapper, So I thought of using my W3 Schools XML example about books to try and map them my self.

So I'm trying to print title, author from an array of Books.

Books.XML

<?xml version="1.0" encoding="utf-8"?>
<bookstore>
    <book>
        <title>Everyday Italian</title>
        <author>Giada De Laurentiis</author>
        <year>2005</year>
        <price>30.00</price>
    </book>
    <book>
        <title>Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
    </book>
    <book>
        <title>XQuery Kick Start</title>
        <author>James McGovern</author>
        <author>Per Bothner</author>
        <author>Kurt Cagle</author>
        <author>James Linn</author>
        <author>Vaidyanathan Nagarajan</author>
        <year>2003</year>
        <price>49.99</price>
    </book>
    <book>
        <title>Learning XML</title>
        <author>Erik T. Ray</author>
        <year>2003</year>
        <price>39.95</price>
    </book>
</bookstore>

BooksXMLMappable.swift file:

import Foundation
import XMLMapper

class BookStore: XMLMappable {
    required init?(map: XMLMap) {
        //Empty
    }

    var nodeName: String!

    var books: [Book]?

    func mapping(map: XMLMap) {
        books <- map["book"]

    }

}

class Book: XMLMappable {
    required init?(map: XMLMap) {
        //Empty
    }

    var nodeName: String!

    var title: String!
    var author: String?
    var year: Int?
    var price: Double?


    func mapping(map: XMLMap) {
        //category <- map.attributes["category"]
        title <- map["title"]
        author <- map["author"]
        year <- map["year"]
        price <- map["price"]
    }

}

Now I tried to run this in my ViewController's ViewDidLoad:

let storeObject = XMLMapper<BookStore>().map(XMLfile: "books.xml")
        print(storeObject?.books?.first?.author ?? "nil")

And I have successfully printed author for the first book, but I can't find a way to print author's of all the books without getting nil.

And if a book has multiple authors which one will be printed and how to print them all ?

Sorry if my question is too easy to solve, or a very basic one, but I'm trying to learn.

Thanks in advance.


回答1:


used for in loop for print author like this

let storeObject = XMLMapper<BookStore>().map(XMLfile: "books.xml")
for book in storeObject.books {
      print(book.author)
}



回答2:


You are getting a nil in author value, in the third book, because there are more than one authors.

Since version 1.4.4 of XMLMapper you can map those cases using an Array.

In your case, specifically, you can map the author tag using an Array of String like:

class Book: XMLMappable {
    required init?(map: XMLMap) {
        //Empty
    }

    var nodeName: String!

    var title: String!
    var authors: [String]?
    var year: Int?
    var price: Double?


    func mapping(map: XMLMap) {
        //category <- map.attributes["category"]
        title <- map["title"]
        authors <- map["author"]
        year <- map["year"]
        price <- map["price"]
    }
}

Then you can access all the authors, looping the authors Array.

Hope this helps.



来源:https://stackoverflow.com/questions/53054971/how-to-access-grand-child-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!