英文:
How to modify xml and return only content without the wrapper in golang
问题
我有一个像这样的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>
我想将其修改为只返回如下内容:
<?xml version="1.0" encoding="UTF-8"?>
<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
,也许可以使用 etree(https://pkg.go.dev/github.com/beevik/etree#Element)来解决问题?
英文:
I have an XML file 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>
I want to modify it to only return like this
<?xml version="1.0" encoding="UTF-8"?>
<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>
So I want to remove the wrapper and only select soap:Body
maybe some solution using etree (https://pkg.go.dev/github.com/beevik/etree#Element) ?
===========================================================
答案1
得分: 1
标准库足够胜任这个任务。只需解析XML文档,使用xml:"innerxml"
将Body元素中的任意XML内容消耗掉。然后你可以将其原样输出。
package main
import (
"bytes"
"encoding/xml"
"io"
"log"
"os"
)
var src = []byte(`
<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>
<!-- ... -->
</contents>
</soap:Body>
</soap:Envelope>
`)
type envelope struct {
XMLName xml.Name `xml:"Envelope"`
Body struct {
InnerXML []byte `xml:",innerxml"`
}
}
func main() {
var e envelope
if err := xml.Unmarshal(src, &e); err != nil {
log.Fatal(err)
}
io.WriteString(os.Stdout, xml.Header)
os.Stdout.Write(bytes.TrimSpace(e.Body.InnerXML))
}
在playground上尝试一下:https://go.dev/play/p/CUEpuPfh_Xl
英文:
The standard library is capable enough for this task. Simply parse the XML document, using xml:",innerxml"
for the Body element to consume arbitrary XML inside. Then you can just spit it back out.
package main
import (
"bytes"
"encoding/xml"
"io"
"log"
"os"
)
var src = []byte(`
<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>
<!-- ... -->
</contents>
</soap:Body>
</soap:Envelope>
`)
type envelope struct {
XMLName xml.Name `xml:"Envelope"`
Body struct {
InnerXML []byte `xml:",innerxml"`
}
}
func main() {
var e envelope
if err := xml.Unmarshal(src, &e); err != nil {
log.Fatal(err)
}
io.WriteString(os.Stdout, xml.Header)
os.Stdout.Write(bytes.TrimSpace(e.Body.InnerXML))
}
Try it on the playground: https://go.dev/play/p/CUEpuPfh_Xl
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论