I want to convert my XML document into a hash in Ruby / Rail Actually, the default conversion by the Hash.Frame_XMM in the rail works for me except for a case.
I have & lt; Item-list & gt; element contains a list of items included, although these items can be of different types. For example, standard-object and special-items , each of which has different sets of hair elements.
& lt; Item-list & gt; & Lt; Standard item & gt; & Lt; Foo & gt; ... & lt; / Foo & gt; & Lt; Bars & gt; ... & lt; / Bars & gt; & Lt; / Standard item & gt; & Lt; Standard item & gt; & Lt; Foo & gt; ... & lt; / Foo & gt; & Lt; Bars & gt; ... & lt; / Bars & gt; & Lt; / Special Item & gt; & Lt; Special items & gt; & Lt; Baz & gt; ... & lt; / Baz & gt; & Lt; / Special Item & gt; & Lt; / Item-list & gt; This XML structure can be confusing for hash.from_xml because it does not know that both the standard items and special items are both items and should be at the same level. Therefore, from the above mentioned XML, hash The hash generated by fROM_xml will be:
{'item-list' = & gt; {'Standard item' = & gt; [{Foo '= & gt; '...', 'bar' = & gt; '...'}, {'foo' => '...', 'bar' = & gt; '...'}], 'special-object' = & gt; {'Falcon' = & gt; '...'}}} But I want to keep all the items as list members, such as:
{'item -list '= & gt; [{'Standard-object' = & gt; {'Foo' = & gt; '...', 'bar' = & gt; '...'}}, {'standard-object' = & gt; {'Foo' = & gt; '...', 'bar' = & gt; '...'}}, {'special-item' = & gt; {'Falcon' = & gt; '...'}}] Is it possible to expand / customize from the way you want to work for this case? If this is not possible, then what is the best way to get it? Given that this is the only element in which there is something wandering from a normal XML-to-Hash conversion, I do not think it is correct to implement the whole conversion routine where it has already been applied one thousand times Ho.
Another small note, Hash.to_xml also changes all dashes with underscores. Is there any way to stop this replacement?
This is the right behavior.
& lt; A & gt; & Lt; P & gt; A & lt; / P & gt; & Lt; P & gt; Two & lt; / P & gt; & Lt; / A & gt; Think about how this is being converted into a hash, the key 'B' can not be assigned to two values. The only solution is 'B' and 'two'. The value of an array containing 'B' is to be made. {a => {B = & gt; ['One', 'two']}}
This represents only the rail XML. You have to check an array value in a hash, and work accordingly.
Comments
Post a Comment