Skip to content

Instantly share code, notes, and snippets.

@mmcgrana mmcgrana/elb_xml.go
Last active Aug 29, 2015

Embed
What would you like to do?
package main;
import (
"bytes"
"encoding/xml"
"fmt"
)
var ResponseExample = `
<DescribeInstanceHealthResponse xmlns="http://elasticloadbalancing.amazonaws.com/doc/2012-06-01/">
<DescribeInstanceHealthResult>
<InstanceStates>
<member>
<Description>N/A</Description>
<InstanceId>i-5e96b053</InstanceId>
<State>InService</State>
<ReasonCode>N/A</ReasonCode>
</member>
<member>
<Description>N/A</Description>
<InstanceId>i-c5f1ddce</InstanceId>
<State>InService</State>
<ReasonCode>N/A</ReasonCode>
</member>
<member>
<Description>N/A</Description>
<InstanceId>i-e1f7dbea</InstanceId>
<State>InService</State>
<ReasonCode>N/A</ReasonCode>
</member>
</InstanceStates>
</DescribeInstanceHealthResult>
<ResponseMetadata>
<RequestId>ca6e49db-353d-11e4-92c6-f3a5ea1ffeb7</RequestId>
</ResponseMetadata>
</DescribeInstanceHealthResponse>`
// This structs result in only 1 InstanceState being parsed out from the
// response body above.
// type InstanceState struct {
// InstanceId string `xml:"member>InstanceId"`
// Description string `xml:"member>Description"`
// State string `xml:"member>State"`
// ReasonCode string `xml:"member>ReasonCode"`
// }
// type DescribeInstanceHealthResp struct {
// InstanceStates []InstanceState `xml:"DescribeInstanceHealthResult>InstanceStates"`
// RequestId string `xml:"ResponseMetadata>RequestId"`
// }
// These structs lead to correctly parsing out multiple InstanceStates
// for the response above.
type InstanceState struct {
InstanceId string `xml:"InstanceId"`
Description string `xml:"Description"`
State string `xml:"State"`
ReasonCode string `xml:"ReasonCode"`
}
type DescribeInstanceHealthResp struct {
InstanceStates []InstanceState `xml:"DescribeInstanceHealthResult>InstanceStates>member"`
RequestId string `xml:"ResponseMetadata>RequestId"`
}
func main() {
reader := bytes.NewBufferString(ResponseExample)
decoder := xml.NewDecoder(reader)
resp := &DescribeInstanceHealthResp{}
err := decoder.Decode(resp)
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", resp)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.