Home > Mobile >  How to modify xml and return only content without the wrapper in golang
How to modify xml and return only content without the wrapper in golang

Time:10-01

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) ?

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

CodePudding user response:

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

  • Related