亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 228 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號