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

Mapping XML element of basic type with attributes #1

Closed
gcharita opened this issue Feb 3, 2018 · 1 comment
Closed

Mapping XML element of basic type with attributes #1

gcharita opened this issue Feb 3, 2018 · 1 comment
Assignees

Comments

@gcharita
Copy link
Owner

gcharita commented Feb 3, 2018

Mapping an XML element that have other elements of basic type and attributes:

<Person gender="male">
    <FirstName>First</FirstName>
    <LastName>Last</LastName>
</Person>

is easy:

class Person: XMLMappable {
    var nodeName: String!
    
    var gender: String?
    var firstName: String?
    var lastName: String?
    
    required init(map: XMLMap) {
        
    }
    
    func mapping(map: XMLMap) {
        gender <- map.attributes["gender"]
        firstName <- map["FirstName"]
        lastName <- map["LastName"]
    }
}

But if I try to map an XML element of basic type (like Person below) that have attributes:

<root>
    <Person gender="male">First Last</Person>
</root>

Using this:

class Root: XMLMappable {
    var nodeName: String!
    
    var person: String?
    
    required init(map: XMLMap) {
        
    }
    
    func mapping(map: XMLMap) {
        person <- map["Person"]
    }
}

I am getting nil in person property and using this:

class Root: XMLMappable {
    var nodeName: String!
    
    var person: Person?
    
    required init(map: XMLMap) {
        
    }
    
    func mapping(map: XMLMap) {
        person <- map["Person"]
    }
}

class Person: XMLMappable {
    var nodeName: String!
    
    var gender: String?
    var name: String?
    
    required init(map: XMLMap) {
        
    }
    
    func mapping(map: XMLMap) {
        gender <- map.attributes["gender"]
        //name <- get somehow innertext
    }
}

I am getting correctly the gender attribute but I don't know how to get Person's name.

@gcharita gcharita self-assigned this Feb 3, 2018
@gcharita
Copy link
Owner Author

gcharita commented Feb 3, 2018

With the next release (probably 1.4.2) you can map directly the text of the current XML node using the innerText property of XMLMap like:

class Person: XMLMappable {
    var nodeName: String!
    
    var gender: String?
    var name: String?
    
    required init(map: XMLMap) {
        
    }
    
    func mapping(map: XMLMap) {
        gender <- map.attributes["gender"]
        name <- map.innerText
    }
}

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

No branches or pull requests

1 participant