为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Go
大话西游666 2023-02-21 16:34:01
假设,我有这样的 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
查看完整描述

1 回答

?
摇曳的蔷薇

TA贡献1793条经验 获得超6个赞

这似乎是这样做的:


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

}


查看完整回答
反对 回复 2023-02-21
  • 1 回答
  • 0 关注
  • 140 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信