亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在golang中修改xml并僅返回沒有包裝器的內容

如何在golang中修改xml并僅返回沒有包裝器的內容

Go
開心每一天1111 2023-02-21 16:09:06
我有一個這樣的 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)?===========================================================
查看完整描述

1 回答

?
小唯快跑啊

TA貢獻1863條經驗 獲得超2個贊

標準庫足以完成這項任務。簡單地解析 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))

}

在操場上試試:https://go.dev/play/p/CUEpuPfh_Xl


查看完整回答
反對 回復 2023-02-21
  • 1 回答
  • 0 關注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號