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

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

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

问题

我有一个像这样的XML文件:

  1. <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.opentravel.org/OTA/2003/05">
  2. <soap:Header/>
  3. <soap:Body>
  4. <contents>
  5. <article>
  6. <category>Server</category>
  7. <title>Connect to Oracle Server using Golang and Go-OCI8 on Ubuntu</title>
  8. <url>/go-oci8-oracle-linux/</url>
  9. </article>
  10. <article>
  11. <category>Server</category>
  12. <title>Easy Setup OpenVPN Using Docker DockVPN</title>
  13. <url>/easy-setup-openvpn-docker/</url>
  14. </article>
  15. <article info="popular article">
  16. <category>Server</category>
  17. <title>Setup Ghost v0.11-LTS, Ubuntu, Nginx, Custom Domain, and SSL</title>
  18. <url>/ghost-v011-lts-ubuntu-nginx-custom-domain-ssl/</url>
  19. </article>
  20. </contents>
  21. </soap:Body>
  22. </soap:Envelope>

我想将其修改为只返回如下内容:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <contents>
  3. <article>
  4. <category>Server</category>
  5. <title>Connect to Oracle Server using Golang and Go-OCI8 on Ubuntu</title>
  6. <url>/go-oci8-oracle-linux/</url>
  7. </article>
  8. <article>
  9. <category>Server</category>
  10. <title>Easy Setup OpenVPN Using Docker DockVPN</title>
  11. <url>/easy-setup-openvpn-docker/</url>
  12. </article>
  13. <article info="popular article">
  14. <category>Server</category>
  15. <title>Setup Ghost v0.11-LTS, Ubuntu, Nginx, Custom Domain, and SSL</title>
  16. <url>/ghost-v011-lts-ubuntu-nginx-custom-domain-ssl/</url>
  17. </article>
  18. </contents>

所以我想移除包装器,只选择 soap:Body,也许可以使用 etree(https://pkg.go.dev/github.com/beevik/etree#Element)来解决问题?

英文:

I have an XML file like this

  1. <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.opentravel.org/OTA/2003/05">
  2. <soap:Header/>
  3. <soap:Body>
  4. <contents>
  5. <article>
  6. <category>Server</category>
  7. <title>Connect to Oracle Server using Golang and Go-OCI8 on Ubuntu</title>
  8. <url>/go-oci8-oracle-linux/</url>
  9. </article>
  10. <article>
  11. <category>Server</category>
  12. <title>Easy Setup OpenVPN Using Docker DockVPN</title>
  13. <url>/easy-setup-openvpn-docker/</url>
  14. </article>
  15. <article info="popular article">
  16. <category>Server</category>
  17. <title>Setup Ghost v0.11-LTS, Ubuntu, Nginx, Custom Domain, and SSL</title>
  18. <url>/ghost-v011-lts-ubuntu-nginx-custom-domain-ssl/</url>
  19. </article>
  20. </contents>
  21. </soap:Body>
  22. </soap:Envelope>

I want to modify it to only return like this

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <contents>
  3. <article>
  4. <category>Server</category>
  5. <title>Connect to Oracle Server using Golang and Go-OCI8 on Ubuntu</title>
  6. <url>/go-oci8-oracle-linux/</url>
  7. </article>
  8. <article>
  9. <category>Server</category>
  10. <title>Easy Setup OpenVPN Using Docker DockVPN</title>
  11. <url>/easy-setup-openvpn-docker/</url>
  12. </article>
  13. <article info="popular article">
  14. <category>Server</category>
  15. <title>Setup Ghost v0.11-LTS, Ubuntu, Nginx, Custom Domain, and SSL</title>
  16. <url>/ghost-v011-lts-ubuntu-nginx-custom-domain-ssl/</url>
  17. </article>
  18. </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内容消耗掉。然后你可以将其原样输出。

  1. package main
  2. import (
  3. "bytes"
  4. "encoding/xml"
  5. "io"
  6. "log"
  7. "os"
  8. )
  9. var src = []byte(`
  10. <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.opentravel.org/OTA/2003/05">
  11. <soap:Header/>
  12. <soap:Body>
  13. <contents>
  14. <article>
  15. <category>Server</category>
  16. <title>Connect to Oracle Server using Golang and Go-OCI8 on Ubuntu</title>
  17. <url>/go-oci8-oracle-linux/</url>
  18. </article>
  19. <!-- ... -->
  20. </contents>
  21. </soap:Body>
  22. </soap:Envelope>
  23. `)
  24. type envelope struct {
  25. XMLName xml.Name `xml:"Envelope"`
  26. Body struct {
  27. InnerXML []byte `xml:",innerxml"`
  28. }
  29. }
  30. func main() {
  31. var e envelope
  32. if err := xml.Unmarshal(src, &e); err != nil {
  33. log.Fatal(err)
  34. }
  35. io.WriteString(os.Stdout, xml.Header)
  36. os.Stdout.Write(bytes.TrimSpace(e.Body.InnerXML))
  37. }

在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.

  1. package main
  2. import (
  3. &quot;bytes&quot;
  4. &quot;encoding/xml&quot;
  5. &quot;io&quot;
  6. &quot;log&quot;
  7. &quot;os&quot;
  8. )
  9. var src = []byte(`
  10. &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;
  11. &lt;soap:Header/&gt;
  12. &lt;soap:Body&gt;
  13. &lt;contents&gt;
  14. &lt;article&gt;
  15. &lt;category&gt;Server&lt;/category&gt;
  16. &lt;title&gt;Connect to Oracle Server using Golang and Go-OCI8 on Ubuntu&lt;/title&gt;
  17. &lt;url&gt;/go-oci8-oracle-linux/&lt;/url&gt;
  18. &lt;/article&gt;
  19. &lt;!-- ... --&gt;
  20. &lt;/contents&gt;
  21. &lt;/soap:Body&gt;
  22. &lt;/soap:Envelope&gt;
  23. `)
  24. type envelope struct {
  25. XMLName xml.Name `xml:&quot;Envelope&quot;`
  26. Body struct {
  27. InnerXML []byte `xml:&quot;,innerxml&quot;`
  28. }
  29. }
  30. func main() {
  31. var e envelope
  32. if err := xml.Unmarshal(src, &amp;e); err != nil {
  33. log.Fatal(err)
  34. }
  35. io.WriteString(os.Stdout, xml.Header)
  36. os.Stdout.Write(bytes.TrimSpace(e.Body.InnerXML))
  37. }

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:

确定