Go unmarshal xml into struct -
Go unmarshal xml into struct -
i have xml file , seems cant unmarshal info struct. can help me out here. never used xml before, prefered json before xml. edited post code , still gives me empty struct values
<envelope> <data> <order> <deliverydata> <del_country>belgique/belgie</del_country> <del_country_code>be</del_country_code> <del_company>false</del_company> <del_name>adam</del_name> <del_contact></del_contact> <del_firstname></del_firstname> <del_addressline1>durasweg 33</del_addressline1> <del_addressline2></del_addressline2> <del_areacode>1000</del_areacode> <del_city>bruxelles</del_city> <del_country>be</del_country> <del_language>fr</del_language> <del_modecode>71</del_modecode> <phone1>0032872180808</phone1> <email></email> <inv_third>438802</inv_third> <orderid>15787978</orderid> <parcelid>ne1578797801</parcelid> <orderdate>16/09/2014 14:22:54</orderdate> <shipping_date>16/09/2014 14:26:55</shipping_date> </deliverydata> </order> </data>
type deliverydata struct { xmlname xml.name `xml:"deliverydata"` country string `xml:"del_country"` } type envelope struct { xmlname xml.name `xml:"envelope"` info info `xml:"data"` } type info struct { xmlname xml.name `xml:data` orders []order `xml:order` } type order struct { xmlname xml.name `xml:"order"` deliverydata deliverydata `xml:"deliverydata"` }
there 2 reasons:
your xml malformed - should add together closing</envelope>
. your struct tags in data
malformed – they don't quote name of attribute - means the xml deserializer looks 'order' field, instead of orders
field. for measure: can find working illustration on http://play.golang.org/p/6-odocsonf
the relevant part my
type info struct { xmlname xml.name `xml:"data"` orders []order `xml:"order"` }
versus original
type info struct { xmlname xml.name `xml:data` orders []order `xml:order` }
xml go
Comments
Post a Comment