我正在使用Webfaction API并尝试解组其响应以获得会话ID(下面的12345abcde ...)。当特定值嵌套在多个字段(即params> param)下时,如何访问该特定值?以下代码位于http://play.golang.org/p/krG4_fXVkapackage mainimport ("encoding/xml" "fmt" )type Result struct { XMLName xml.Name `xml:"methodResponse"` Values []string `xml:"params>param>value"`}func main() {data := ` <methodResponse><params> <param> <value><array><data> <value><string>12345abcde12345abcde12345</string></value> <value><struct> <member> <name>username</name> <value><string>trex</string></value> </member> <member> <name>home</name> <value><string>/home</string></value> </member> <member> <name>mail_server</name> <value><string>Mailbox1</string></value> </member> <member> <name>web_server</name> <value><string>Web12</string></value> </member> <member> <name>id</name> <value><int>1234</int></value> </member> </struct></value> </data></array></value> </param></params>`v := Result{}err := xml.Unmarshal([]byte(data), &v)if err != nil {fmt.Printf("error: %v", err) return}fmt.Printf("XMLName: %#v\n", v.XMLName)fmt.Printf("Values: %#v\n", v.Values)}
1 回答
- 1 回答
- 0 关注
- 186 浏览
添加回答
举报
0/150
提交
取消