如何在Golang中修改XML并仅返回内容而不包含包装器。

huangapple go评论74阅读模式
英文:

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:&quot;,innerxml&quot; for the Body element to consume arbitrary XML inside. Then you can just spit it back out.

package main

import (
	&quot;bytes&quot;
	&quot;encoding/xml&quot;
	&quot;io&quot;
	&quot;log&quot;
	&quot;os&quot;
)

var src = []byte(`
&lt;soap:Envelope xmlns:soap=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot; xmlns:ns=&quot;http://www.opentravel.org/OTA/2003/05&quot;&gt;
    &lt;soap:Header/&gt;
    &lt;soap:Body&gt;
        &lt;contents&gt;
            &lt;article&gt;
                &lt;category&gt;Server&lt;/category&gt;
                &lt;title&gt;Connect to Oracle Server using Golang and Go-OCI8 on Ubuntu&lt;/title&gt;
                &lt;url&gt;/go-oci8-oracle-linux/&lt;/url&gt;
            &lt;/article&gt;
            &lt;!-- ... --&gt;
        &lt;/contents&gt;
    &lt;/soap:Body&gt;
&lt;/soap:Envelope&gt;
`)

type envelope struct {
	XMLName xml.Name `xml:&quot;Envelope&quot;`
	Body    struct {
		InnerXML []byte `xml:&quot;,innerxml&quot;`
	}
}

func main() {
	var e envelope
	if err := xml.Unmarshal(src, &amp;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

huangapple
  • 本文由 发表于 2022年9月30日 15:37:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/73905443.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定