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

Parsing array of same keyed Items #13

Open
ilendemli opened this issue May 7, 2018 · 3 comments
Open

Parsing array of same keyed Items #13

ilendemli opened this issue May 7, 2018 · 3 comments

Comments

@ilendemli
Copy link

Hello!

Is there a way to parse something such as

<TextLines>
<Text>Line1</Text>
<Text>Line2</Text>
<Text>Line3</Text>
<Text>Line4</Text>
<Text>Line5</Text>
</TextLines>

As it gets recognized as a dictionary, only the last one is returned when using something like this:

public struct TextLine: Decodable {
  public let text: String

  enum CodingKeys: String, CodingKey {
    case text = "Text"
  }
}

public struct Document: Decodable {
  public let textLines: [TextLine]

  enum CodingKeys: String, CodingKey {
    case textLines = "TextLines"
  }
}

Which isn't even a correct representation of the XML. I think it should be something like this if I am correct but this fails:

public struct TextLines: Decodable {
  public let text: [String]

  enum CodingKeys: String, CodingKey {
    case text = "Text"
  }
}

public struct Document: Decodable {
  public let textLines: TextLines

  enum CodingKeys: String, CodingKey {
    case textLines = "TextLines"
  }
}

Any suggestions?

@regexident
Copy link

This should be resolved with 0.0.2, no?

@KaiTeuber
Copy link

The Texline example works for me as longs it's only an array of Strings. If more complex structures are give it doesn't work for me.
Here what the XML might light like:

<TextLines>
    <line ag="348">US0231351067</line>
    <line ag="891">DE0007472060</line>
</TextLines>

I expect an array of liines and each line should have ag and line values. Like this:

struct TextLines: Decodable {
   let lines: [Line]
        
   enum CodingKeys: String, CodingKey {
        case lines = "line"
    }
}
    
struct Line: Decodable {
    let line: String
    let ag: String
}

But all I can reach is an DecodingError.typeMismatch. Any ideas?

@joshbirnholz
Copy link

joshbirnholz commented Oct 8, 2018

I'm having trouble with this, too. I'm trying to parse XML like this and I'm not sure how I would do it.

<?xml version="1.0" encoding="UTF-8" ?>
<apps>
    <app id="31012" type="menu" version="1.9.26">Movie Store and TV Store</app>
    <app id="tvinput.hdmi1" type="tvin" version="1.0.0">HDMI 1</app>
    <app id="tvinput.hdmi2" type="tvin" version="1.0.0">HDMI 2</app>
    <app id="tvinput.hdmi3" type="tvin" version="1.0.0">HDMI 3</app>
    <app id="tvinput.cvbs" type="tvin" version="1.0.0">Component</app>
    <app id="13535" subtype="rsga" type="appl" version="5.3.10">Plex</app>
    <app id="12" subtype="ndka" type="appl" version="4.2.90615014">Netflix</app>
    <app id="837" subtype="ndka" type="appl" version="1.0.70500270">YouTube</app>
    <app id="8378" subtype="rsga" type="appl" version="4.5.36">HBO GO</app>
</apps>

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

No branches or pull requests

4 participants