Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursive XML elements #38

Closed
aaomidi opened this issue Feb 16, 2021 · 6 comments
Closed

Recursive XML elements #38

aaomidi opened this issue Feb 16, 2021 · 6 comments
Labels

Comments

@aaomidi
Copy link

aaomidi commented Feb 16, 2021

Hi!

How would I go about handling recursive XML elements?

E.g.

<item>
  <id>1</id>
  <item>
    <id>2</id>
  </item>
</item>
@gcharita
Copy link
Owner

@aaomidi thanks for using XMLMapper.

You can use the optional strategy with a model class like this:

class Item: XMLMappable {
    var nodeName: String!
    
    var id: Int?
    var item: Item?

    required init?(map: XMLMap) {}
    
    func mapping(map: XMLMap) {
        id <- map["id"]
        item <- map["item"]
    }
}

@aaomidi
Copy link
Author

aaomidi commented Feb 16, 2021

Thank you!

@aaomidi
Copy link
Author

aaomidi commented Feb 23, 2021

I don't think this is working :(

Some code to demonstrate:

class NavPoint: XMLMappable, Identifiable {

    var nodeName: String!

    var navLabel: String!
    var contentSrc: String!
    var navPoint: NavPoint?

    required init?(map: XMLMap) { }
    
    func mapping(map: XMLMap) {
        print("start new")
        navLabel <- map["navLabel.text"]
        contentSrc <- map["content"].attributes["content.src"]
        print("start navLabel: \(navLabel!)")
        navPoint <- map["navPoint"]
        print("end navLabel: \(navLabel!)")
    }
}

Prints:

start new
start navLabel: Cover
end navLabel: Cover
start new
start navLabel: Area X: The Southern Reach Trilogy
end navLabel: Area X: The Southern Reach Trilogy
start new
start navLabel: Annihilation
end navLabel: Annihilation
start new
start navLabel: Authority
end navLabel: Authority
start new
start navLabel: Acceptance
end navLabel: Acceptance
start new
start navLabel: Area X Map
end navLabel: Area X Map

This is the first level nodes in the XML file.

@aaomidi
Copy link
Author

aaomidi commented Feb 23, 2021

I've taken a bit deeper of a look, and map definitely contains the values. It just never tries to construct the new one.

image

@aaomidi
Copy link
Author

aaomidi commented Feb 23, 2021

Please ignore me, I am an idiot and this was supposed to be mapped to an array, rather than an object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants