Very nice, `xml:",any"` was doing the trick. I've condensed the function 
and here's the (currently) final 
result: https://play.golang.org/p/G1eGw5gtk7C

func id3(axl []byte) string {
type Return struct {
Response struct {
Return string `xml:"return"`
} `xml:",any"`
}
var ret Return
err := xml.Unmarshal(axl, &ret)
if err != nil {
return fmt.Sprintf("unable to unmarshal id: %v", err) // I know I should 
use a separate error channel
}
return ret.Response.Return // might be empty if XML was invalid (no return 
element)
}

Thanks for all your comments!


Am Montag, 25. Februar 2019 05:44:34 UTC+1 schrieb Matt Harden:
>
> You don't have to use an xml.Decoder. You may be able to use the 
> `xml:",any"` tag for this case.
>
> type Response struct {
> XMLName xml.Name
> Return  string `xml:"return"`
> }
> type Outer struct {
> XMLName   struct{}   `xml:"outer"`
> Responses []Response `xml:",any"`
> }
>
>
> https://play.golang.org/p/j4w4-1uuYae
>
>
>
> On Sun, Feb 24, 2019 at 9:28 AM Steffen Wentzel <syncr...@gmail.com 
> <javascript:>> wrote:
>
>> Thanks for the response. I was hoping it would be possible with just 
>> xml.Unmarshal.
>>
>> I now have this implementation - any comments on that? (I'm okay with 
>> returning the error inline, it's just for logging purposes):
>>
>> Playground: https://play.golang.org/p/6J8XndWdlv_N
>>
>> func id2(axl []byte) string {
>> dec := xml.NewDecoder(bytes.NewReader(axl))
>> for {
>> tok, err := dec.Token()
>> if err != nil {
>> if err == io.EOF {
>> return "token not found"
>> }
>> return fmt.Sprintf("error decoding XML: %v", err)
>>
>> }
>> if tok, ok := tok.(xml.StartElement); ok {
>> if tok.Name.Local == "return" {
>> var id string
>> err := dec.DecodeElement(&id, &tok)
>> if err != nil {
>> return fmt.Sprintf("error decoding return element: %v", err)
>> }
>> return id
>> }
>> }
>> }
>> }
>>
>>
>>
>> Am Samstag, 23. Februar 2019 20:53:07 UTC+1 schrieb Tamás Gulácsi:
>>>
>>> Walk over it usin xml.Decoder.Token, and UnmarshalElement when found a 
>>> StartToken with a proper Name.Local.
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "golang-nuts" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to golang-nuts...@googlegroups.com <javascript:>.
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to