如何在golang中修改xml并仅返回没有包装器的内容

我有一个这样的 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)?


===========================================================


开心每一天1111
浏览 142回答 1
1回答

小唯快跑啊

标准库足以完成这项任务。简单地解析 XML 文档,使用xml:",innerxml"Body 元素在其中使用任意 XML。然后你可以把它吐出来。package mainimport (&nbsp; &nbsp; "bytes"&nbsp; &nbsp; "encoding/xml"&nbsp; &nbsp; "io"&nbsp; &nbsp; "log"&nbsp; &nbsp; "os")var src = []byte(`<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.opentravel.org/OTA/2003/05">&nbsp; &nbsp; <soap:Header/>&nbsp; &nbsp; <soap:Body>&nbsp; &nbsp; &nbsp; &nbsp; <contents>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <article>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <category>Server</category>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <title>Connect to Oracle Server using Golang and Go-OCI8 on Ubuntu</title>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <url>/go-oci8-oracle-linux/</url>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </article>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- ... -->&nbsp; &nbsp; &nbsp; &nbsp; </contents>&nbsp; &nbsp; </soap:Body></soap:Envelope>`)type envelope struct {&nbsp; &nbsp; XMLName xml.Name `xml:"Envelope"`&nbsp; &nbsp; Body&nbsp; &nbsp; struct {&nbsp; &nbsp; &nbsp; &nbsp; InnerXML []byte `xml:",innerxml"`&nbsp; &nbsp; }}func main() {&nbsp; &nbsp; var e envelope&nbsp; &nbsp; if err := xml.Unmarshal(src, &e); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; io.WriteString(os.Stdout, xml.Header)&nbsp; &nbsp; os.Stdout.Write(bytes.TrimSpace(e.Body.InnerXML))}在操场上试试:https://go.dev/play/p/CUEpuPfh_Xl
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go