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

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

Golang XML: how to get xml attribute in header in string map

问题

假设我有一个像这样的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:soapxmlns:ns)?

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

还有如何获取字符串soap:Envelope

假设结构不总是soap:Envelope,可能是soap:Foo

英文:

Suppose, I have an XML like this

<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>

How to get a map that return the attribute at the root element (the key value is dynamic, not always xmlns:soap and xmlns:ns)

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

and also how to get this string soap:Envelope?

assuming structure is not always soap:Envelope, so it can be soap:Foo

答案1

得分: 2

这似乎可以实现:

package main
import "encoding/xml"

var input = []byte(`
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
   xmlns:ns="http://www.opentravel.org/OTA/2003/05">
</soap:Envelope>
`)

func main() {
   var soap struct {
      Attrs   []xml.Attr `xml:",any,attr"`
      XMLName xml.Name
   } 
   err := xml.Unmarshal(input, &soap)
   if err != nil {
      panic(err)
   }
   println(soap.Attrs[1].Name.Local == "ns")
   println(soap.Attrs[1].Value == "http://www.opentravel.org/OTA/2003/05")
   println(soap.XMLName.Local == "Envelope")
}
英文:

This seems to do it:

package main
import &quot;encoding/xml&quot;

var input = []byte(`
&lt;soap:Envelope xmlns:soap=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;
   xmlns:ns=&quot;http://www.opentravel.org/OTA/2003/05&quot;&gt;
&lt;/soap:Envelope&gt;
`)

func main() {
   var soap struct {
      Attrs   []xml.Attr `xml:&quot;,any,attr&quot;`
      XMLName xml.Name
   } 
   err := xml.Unmarshal(input, &amp;soap)
   if err != nil {
      panic(err)
   }
   println(soap.Attrs[1].Name.Local == &quot;ns&quot;)
   println(soap.Attrs[1].Value == &quot;http://www.opentravel.org/OTA/2003/05&quot;)
   println(soap.XMLName.Local == &quot;Envelope&quot;)
}

huangapple
  • 本文由 发表于 2022年10月3日 01:18:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/73927837.html
匿名

发表评论

匿名网友

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

确定