在Go中使用嵌套XML-RPC解组组。

huangapple go评论86阅读模式
英文:

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 (&quot;encoding/xml&quot;
        &quot;fmt&quot;
        )

type Result struct {
        XMLName xml.Name `xml:&quot;methodResponse&quot;`
        Values []string `xml:&quot;params&gt;param&gt;value&quot;`
}

func main() {
data := `
    &lt;methodResponse&gt;
&lt;params&gt;
    &lt;param&gt;
        &lt;value&gt;&lt;array&gt;&lt;data&gt;
            &lt;value&gt;&lt;string&gt;12345abcde12345abcde12345&lt;/string&gt;&lt;/value&gt;
            &lt;value&gt;&lt;struct&gt;
                &lt;member&gt;
                    &lt;name&gt;username&lt;/name&gt;
                    &lt;value&gt;&lt;string&gt;trex&lt;/string&gt;&lt;/value&gt;
                &lt;/member&gt;
                &lt;member&gt;
                    &lt;name&gt;home&lt;/name&gt;
                    &lt;value&gt;&lt;string&gt;/home&lt;/string&gt;&lt;/value&gt;
                &lt;/member&gt;
                &lt;member&gt;
                    &lt;name&gt;mail_server&lt;/name&gt;
                    &lt;value&gt;&lt;string&gt;Mailbox1&lt;/string&gt;&lt;/value&gt;
                &lt;/member&gt;
                &lt;member&gt;
                    &lt;name&gt;web_server&lt;/name&gt;
                    &lt;value&gt;&lt;string&gt;Web12&lt;/string&gt;&lt;/value&gt;
                &lt;/member&gt;
                &lt;member&gt;
                    &lt;name&gt;id&lt;/name&gt;
                    &lt;value&gt;&lt;int&gt;1234&lt;/int&gt;&lt;/value&gt;
                &lt;/member&gt;
            &lt;/struct&gt;&lt;/value&gt;
        &lt;/data&gt;&lt;/array&gt;&lt;/value&gt;
    &lt;/param&gt;
&lt;/params&gt;

</methodResponse>`

v := Result{}
err := xml.Unmarshal([]byte(data), &amp;v)
if err != nil {
fmt.Printf(&quot;error: %v&quot;, err)
        return
}
fmt.Printf(&quot;XMLName: %#v\n&quot;, v.XMLName)
fmt.Printf(&quot;Values: %#v\n&quot;, 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:&quot;name&quot;`
    Value string `xml:&quot;value&gt;string&quot;`
}

type Result struct {
        XMLName xml.Name `xml:&quot;methodResponse&quot;`
        FirstValue string `xml:&quot;params&gt;param&gt;value&gt;array&gt;data&gt;value&gt;string&quot;`
        Members []Member `xml:&quot;params&gt;param&gt;value&gt;array&gt;data&gt;value&gt;struct&gt;member&quot;`
}

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.

huangapple
  • 本文由 发表于 2012年9月17日 08:33:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/12451898.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定