jnunemaker / crack

Really simple JSON and XML parsing, ripped from Merb and Rails.
MIT License
541 stars 93 forks source link

Issue with array of elements when the key is an attribute of the nodw #55

Closed selaromi closed 9 years ago

selaromi commented 9 years ago

Hello. When parsing this xml:

<MatchData uID="g695008">
      <Stat Type="total tackle ranking">100</Stat>
      <Stat Type="total tackle">29</Stat>
      <Stat Type="total fouls ranking">84</Stat>
      <Stat Type="total fouls">19</Stat>
      <Stat Type="total accurate pass ranking">13</Stat>
      <Stat Type="total accurate pass">872</Stat>
      <Stat Type="total goals ranking">81</Stat>
      <Stat Type="total goals">1</Stat>
      <Stat Type="total goals conceded ranking">81</Stat>
      <Stat Type="total goals conceded">1</Stat>
      <Stat Type="total scoring att ranking">28</Stat>
      <Stat Type="total scoring att">29</Stat>
      <Stat Type="total was fouled ranking">76</Stat>
      <Stat Type="total was fouled">19</Stat>
      <Stat Type="total attempts conceded obox ranking">12</Stat>
      <Stat Type="total attempts conceded obox">17</Stat>
      <Stat Type="total pass ranking">15</Stat>
      <Stat Type="total pass">1013</Stat>
      <Stat Type="total won tackle ranking">111</Stat>
      <Stat Type="total won tackle">20</Stat>
      <Stat Type="total goals conceded ibox ranking">68</Stat>
      <Stat Type="total goals conceded ibox">1</Stat>
      <Stat Type="total attempts conceded ibox ranking">79</Stat>
      <Stat Type="total attempts conceded ibox">12</Stat>
      <Stat Type="total yellow card ranking">97</Stat>
      <Stat Type="total yellow card">1</Stat>
      <TeamData Side="Home" uID="t56" />
      <TeamData Side="Away" uID="t43" />
    </MatchData>

I'm getting an array of Stat with only the values of the nodes:

{"MatchData"=>{"Stat"=>["100", "29", "84", "19", "13", "872", "81", "1", "81", "1", "28", "29", "76", "19", "12", "17", "15", "1013", "111", "20", "68", "1", "79", "12", "97", "1"], "TeamData"=>[{"Side"=>"Home", "uID"=>"t56"}, {"Side"=>"Away", "uID"=>"t43"}], "uID"=>"g695008"}} 

And actually the Type is a value I want to retain. I have found a way to fix it, by wrapping the Stat value with like this:

<Stat Type="total yellow card ranking"><value>97</value></Stat>

But that means passing through the whole XML twice, And I wanted to ask if there was a better way to do it.

Excellent library, BTW :+1:

selaromi commented 9 years ago

Oh, I forgot, the XML received is from an external provider, and I can't make them change the format of the file for us.

Thank you very much!

jnunemaker commented 9 years ago

Crack is pretty old and inflexible. If you need custom things, you are probably better off with nokogiri or another xml parser. Sorry I don't have good news!