英文:
Unmarshal Group with Nested XML-RPC in Go
问题
我正在使用Webfaction API,并尝试解析它们的响应以获取会话ID(下面的12345abcde...)。
当特定值嵌套在多个字段下(即params>param)时,我如何访问它?
以下是代码,位于http://play.golang.org/p/krG4_fXVka
package main
import (
"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>
</methodResponse>`
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)
}
英文:
I'm using Webfaction API and trying to unmarshal their response to obtain the session ID (12345abcde... below).
How do I access the specific value when its nested under several fields (namely, params>param)?
Code below is at http://play.golang.org/p/krG4_fXVka
package main
import ("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>
</methodResponse>`
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
得分: 5
type Member struct {
Name string xml:"name"
Value string xml:"value>string"
}
type Result struct {
XMLName xml.Name xml:"methodResponse"
FirstValue string xml:"params>param>value>array>data>value>string"
Members []Member xml:"params>param>value>array>data>value>struct>member"
}
请注意,您解析的结构具有相当深的层次。建议您运行XML通过一个漂亮的打印机,以便以缩进的方式格式化它。
英文:
Not 100% clear on what you're trying to do, but this seems to break it down better?
type Member struct {
Name string `xml:"name"`
Value string `xml:"value>string"`
}
type Result struct {
XMLName xml.Name `xml:"methodResponse"`
FirstValue string `xml:"params>param>value>array>data>value>string"`
Members []Member `xml:"params>param>value>array>data>value>struct>member"`
}
Note that the structure your parsing has a fairly insane level of depth. Might help to run the XML through a pretty printer that formats it for you with indent to see.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论