假设,我有这样的 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")
}
- 1 回答
- 0 关注
- 182 浏览
添加回答
举报
0/150
提交
取消