XMLParsing icon indicating copy to clipboard operation
XMLParsing copied to clipboard

Parsing array of same keyed Items

Open ilendemli opened this issue 6 years ago • 3 comments

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?

ilendemli avatar May 07 '18 15:05 ilendemli

This should be resolved with 0.0.2, no?

regexident avatar Oct 01 '18 09:10 regexident

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?

KaiTeuber avatar Oct 05 '18 23:10 KaiTeuber

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>

joshbirnholz avatar Oct 08 '18 15:10 joshbirnholz