使用encoding/xml.Encoder,我如何将XML头放在单独的一行上?

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

Using encoding/xml.Encoder how do I place the xml header on its own line?

问题

我有以下使用xml.Encode的代码。

  1. package main
  2. import (
  3. "bytes"
  4. "encoding/xml"
  5. "fmt"
  6. )
  7. type Stuff struct {
  8. Name string `xml:"name"`
  9. }
  10. func main() {
  11. w := &bytes.Buffer{}
  12. enc := xml.NewEncoder(w)
  13. enc.Indent("", " ")
  14. procInst := xml.ProcInst{
  15. Target: "xml",
  16. Inst: []byte("version=\"1.0\" encoding=\"UTF-8\""),
  17. }
  18. if err := enc.EncodeToken(procInst); err != nil {
  19. panic(err)
  20. }
  21. if err := enc.Encode(Stuff{"My stuff"}); err != nil {
  22. panic(err)
  23. }
  24. fmt.Println(w.String())
  25. }

它输出:

  1. <?xml version="1.0" encoding="UTF-8"?><Stuff>
  2. <name>My stuff</name>
  3. </Stuff>

我该如何使它在新的一行上打印<Stuff>开始标签:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Stuff>
  3. <name>My stuff</name>
  4. </Stuff>

很不幸,我需要在这里写更多的内容才能提交问题。不太确定要写什么,因为我认为上面的解释已经足够了。

英文:

I have the following code that uses xml.Encode.

  1. package main
  2. import (
  3. &quot;bytes&quot;
  4. &quot;encoding/xml&quot;
  5. &quot;fmt&quot;
  6. )
  7. type Stuff struct {
  8. Name string `xml:&quot;name&quot;`
  9. }
  10. func main() {
  11. w := &amp;bytes.Buffer{}
  12. enc := xml.NewEncoder(w)
  13. enc.Indent(&quot;&quot;, &quot; &quot;)
  14. procInst := xml.ProcInst{
  15. Target: &quot;xml&quot;,
  16. Inst: []byte(&quot;version=\&quot;1.0\&quot; encoding=\&quot;UTF-8\&quot;&quot;),
  17. }
  18. if err := enc.EncodeToken(procInst); err != nil {
  19. panic(err)
  20. }
  21. if err := enc.Encode(Stuff{&quot;My stuff&quot;}); err != nil {
  22. panic(err)
  23. }
  24. fmt.Println(w.String())
  25. }

http://play.golang.org/p/ZtZ5FGABmj

It prints:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&lt;Stuff&gt;
  2. &lt;name&gt;My stuff&lt;/name&gt;
  3. &lt;/Stuff&gt;

How do I make it print with the &lt;Stuff&gt; start tag on a new line:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;Stuff&gt;
  3. &lt;name&gt;My stuff&lt;/name&gt;
  4. &lt;/Stuff&gt;

Unfortunately I need to write more here so that I can submit the question. Not really sure what to write though because I believe my question is summarized adequately with the above explanation.

答案1

得分: 8

你可以自己写一行带有换行符的静态代码。这是一个示例:

  1. w := &bytes.Buffer{}
  2. w.Write([]byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"))
  3. enc := xml.NewEncoder(w)
  4. enc.Indent("", " ")
  5. if err := enc.Encode(Stuff{"My stuff"}); err != nil {
  6. panic(err)
  7. }
  8. fmt.Println(w.String())

这一行甚至在xml包中被定义为一个常量,所以你可以直接写:

  1. w := &bytes.Buffer{}
  2. w.WriteString(xml.Header)

你可以在以下链接中查看示例代码:

Go Playground 示例1

Go Playground 示例2

英文:

How about just write that one static line with a newline character yourself? Go Playground

  1. w := &amp;bytes.Buffer{}
  2. w.Write([]byte(&quot;&lt;?xml version=\&quot;1.0\&quot; encoding=\&quot;UTF-8\&quot;?&gt;\n&quot;))
  3. enc := xml.NewEncoder(w)
  4. enc.Indent(&quot;&quot;, &quot; &quot;)
  5. if err := enc.Encode(Stuff{&quot;My stuff&quot;}); err != nil {
  6. panic(err)
  7. }
  8. fmt.Println(w.String())

This one line is even defined as a constant in the xml package, so you can simply write: Go Playground

  1. w := &amp;bytes.Buffer{}
  2. w.WriteString(xml.Header)

huangapple
  • 本文由 发表于 2015年6月8日 20:45:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/30709562.html
匿名

发表评论

匿名网友

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

确定