2

I have the following XML string. I am trying to parse with XMLMapper (3rd party library).

I need to create array of

The xml is as follow;

<RandomDecimals>
<decimal>98258979</decimal>
<decimal>1000002218</decimal>
<decimal>1000002102</decimal>
<decimal>1000005713</decimal>
<decimal>1000002035</decimal>
<decimal>1000001434</decimal>
<decimal>1000001066</decimal>
<decimal>1000001498</decimal>
<decimal>1000002313</decimal>
<decimal>1000016914</decimal>
<decimal>1000017021</decimal>
<decimal>1000019039</decimal>
<decimal>52373625</decimal>
</RandomDecimals>

The code to parse to object is as follows;

class DecimalElement: XMLMappable {
    var nodeName: String!

    var decimal: String?

    required init(map: XMLMap) {

    }

    func mapping(map: XMLMap) {
        decimal <- map["decimal"]
    }
    }

    class Response: XMLMappable {
    var nodeName: String!

    var shouldReset: Bool!
    var randomDecimals: [DecimalElement]?

    required init(map: XMLMap) {

    }

    func mapping(map: XMLMap) {
        randomDecimals <- map["RandomDecimals.decimal"]
        shouldReset <- map["ShouldReset"]

    }

As a summary, I need to take decimals into randomDecimals array. But the I need to do it with XMLMapper.

What am I doing wrong?

BR,

Erdem

rmaddy
  • 314,917
  • 42
  • 532
  • 579
erdemgc
  • 1,701
  • 3
  • 23
  • 43

1 Answers1

1

If your XML looks something like this:

<root>
    <ShouldReset>true</ShouldReset>
    <RandomDecimals>
        <decimal>98258979</decimal>
        <decimal>1000002218</decimal>
        <decimal>1000002102</decimal>
        <decimal>1000005713</decimal>
        <decimal>1000002035</decimal>
        <decimal>1000001434</decimal>
        <decimal>1000001066</decimal>
        <decimal>1000001498</decimal>
        <decimal>1000002313</decimal>
        <decimal>1000016914</decimal>
        <decimal>1000017021</decimal>
        <decimal>1000019039</decimal>
        <decimal>52373625</decimal>
    </RandomDecimals>
</root>

then, you can solve your issue by replacing:

var randomDecimals: [DecimalElement]?

with:

var randomDecimals: [String]?

in your Response class.

class Response: XMLMappable {
    var nodeName: String!

    var shouldReset: Bool!
    var randomDecimals: [String]?

    required init(map: XMLMap) {

    }

    func mapping(map: XMLMap) {
        randomDecimals <- map["RandomDecimals.decimal"]
        shouldReset <- map["ShouldReset"]
    }
}

You can also use Array of Int64 to map directly the decimal numbers:

var randomDecimals: [Int64]?

Hope this helps.

gcharita
  • 7,729
  • 3
  • 20
  • 37