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

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

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

问题

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

package main

import (
	"bytes"
	"encoding/xml"
	"fmt"
)

type Stuff struct {
	Name string `xml:"name"`
}

func main() {
	w := &bytes.Buffer{}
	enc := xml.NewEncoder(w)
	enc.Indent("", "  ")
	procInst := xml.ProcInst{
		Target: "xml",
		Inst:   []byte("version=\"1.0\" encoding=\"UTF-8\""),
	}
	if err := enc.EncodeToken(procInst); err != nil {
		panic(err)
	}

	if err := enc.Encode(Stuff{"My stuff"}); err != nil {
		panic(err)
	}
	fmt.Println(w.String())
}

它输出:

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

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

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

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

英文:

I have the following code that uses xml.Encode.

package main

import (
	&quot;bytes&quot;
	&quot;encoding/xml&quot;
	&quot;fmt&quot;
)

type Stuff struct {
	Name string `xml:&quot;name&quot;`
}

func main() {
	w := &amp;bytes.Buffer{}
	enc := xml.NewEncoder(w)
	enc.Indent(&quot;&quot;, &quot;  &quot;)
	procInst := xml.ProcInst{
		Target: &quot;xml&quot;,
		Inst:   []byte(&quot;version=\&quot;1.0\&quot; encoding=\&quot;UTF-8\&quot;&quot;),
	}
	if err := enc.EncodeToken(procInst); err != nil {
		panic(err)
	}

	if err := enc.Encode(Stuff{&quot;My stuff&quot;}); err != nil {
		panic(err)
	}
	fmt.Println(w.String())
}

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

It prints:

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

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;Stuff&gt;
  &lt;name&gt;My stuff&lt;/name&gt;
&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

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

w := &bytes.Buffer{}
w.Write([]byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"))

enc := xml.NewEncoder(w)
enc.Indent("", "  ")
if err := enc.Encode(Stuff{"My stuff"}); err != nil {
    panic(err)
}
fmt.Println(w.String())

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

w := &bytes.Buffer{}
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

w := &amp;bytes.Buffer{}
w.Write([]byte(&quot;&lt;?xml version=\&quot;1.0\&quot; encoding=\&quot;UTF-8\&quot;?&gt;\n&quot;))

enc := xml.NewEncoder(w)
enc.Indent(&quot;&quot;, &quot;  &quot;)
if err := enc.Encode(Stuff{&quot;My stuff&quot;}); err != nil {
	panic(err)
}
fmt.Println(w.String())

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

w := &amp;bytes.Buffer{}
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:

确定