Golang XML:如何在字符串映射的标头中获取 xml 属性

假设,我有这样的 XML


<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.opentravel.org/OTA/2003/05">

    <soap:Header/>

    <soap:Body>

        <contents>

            <article>

                <category>Server</category>

                <title>Connect to Oracle Server using Golang and Go-OCI8 on Ubuntu</title>

                <url>/go-oci8-oracle-linux/</url>

            </article>

            <article>

                <category>Server</category>

                <title>Easy Setup OpenVPN Using Docker DockVPN</title>

                <url>/easy-setup-openvpn-docker/</url>

            </article>

            <article info="popular article">

                <category>Server</category>

                <title>Setup Ghost v0.11-LTS, Ubuntu, Nginx, Custom Domain, and SSL</title>

                <url>/ghost-v011-lts-ubuntu-nginx-custom-domain-ssl/</url>

            </article>

        </contents>

    </soap:Body>

</soap:Envelope>

如何获取返回根元素处属性的映射(键值是动态的,不总是xmlns:soapand xmlns:ns)


{ "xmlns:soap": "http://schemas.xmlsoap.org/soap/envelope/", "xmlns:ns": "http://www.opentravel.org/OTA/2003/05" }


以及如何获取此字符串soap:Envelope?


假设结构并不总是soap:Envelope,所以它可以是soap:Foo


大话西游666
浏览 183回答 1
1回答

摇曳的蔷薇

这似乎是这样做的:package mainimport "encoding/xml"var input = []byte(`<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&nbsp; &nbsp;xmlns:ns="http://www.opentravel.org/OTA/2003/05"></soap:Envelope>`)func main() {&nbsp; &nbsp;var soap struct {&nbsp; &nbsp; &nbsp; Attrs&nbsp; &nbsp;[]xml.Attr `xml:",any,attr"`&nbsp; &nbsp; &nbsp; XMLName xml.Name&nbsp; &nbsp;}&nbsp;&nbsp; &nbsp;err := xml.Unmarshal(input, &soap)&nbsp; &nbsp;if err != nil {&nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp;}&nbsp; &nbsp;println(soap.Attrs[1].Name.Local == "ns")&nbsp; &nbsp;println(soap.Attrs[1].Value == "http://www.opentravel.org/OTA/2003/05")&nbsp; &nbsp;println(soap.XMLName.Local == "Envelope")}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go