]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/xml: add (*Encoder).Indent
authorRuss Cox <rsc@golang.org>
Wed, 30 Jan 2013 15:57:20 +0000 (07:57 -0800)
committerRuss Cox <rsc@golang.org>
Wed, 30 Jan 2013 15:57:20 +0000 (07:57 -0800)
Exposing this on the Encoder allows streaming generation of indented XML.

R=golang-dev, rogpeppe
CC=golang-dev
https://golang.org/cl/7221075

src/pkg/encoding/xml/example_test.go
src/pkg/encoding/xml/marshal.go

index 97c8c0b0dc0224a8dc80d8b97f2876ad9d8671db..becedd58398693818b21ad1fb94feeb3a5c9b6d1 100644 (file)
@@ -50,6 +50,46 @@ func ExampleMarshalIndent() {
        //   </person>
 }
 
+func ExampleEncoder() {
+       type Address struct {
+               City, State string
+       }
+       type Person struct {
+               XMLName   xml.Name `xml:"person"`
+               Id        int      `xml:"id,attr"`
+               FirstName string   `xml:"name>first"`
+               LastName  string   `xml:"name>last"`
+               Age       int      `xml:"age"`
+               Height    float32  `xml:"height,omitempty"`
+               Married   bool
+               Address
+               Comment string `xml:",comment"`
+       }
+
+       v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}
+       v.Comment = " Need more details. "
+       v.Address = Address{"Hanga Roa", "Easter Island"}
+
+       enc := xml.NewEncoder(os.Stdout)
+       enc.Indent("  ", "    ")
+       if err := enc.Encode(v); err != nil {
+               fmt.Printf("error: %v\n", err)
+       }
+
+       // Output:
+       //   <person id="13">
+       //       <name>
+       //           <first>John</first>
+       //           <last>Doe</last>
+       //       </name>
+       //       <age>42</age>
+       //       <Married>false</Married>
+       //       <City>Hanga Roa</City>
+       //       <State>Easter Island</State>
+       //       <!-- Need more details. -->
+       //   </person>
+}
+
 // This example demonstrates unmarshaling an XML excerpt into a value with
 // some preset fields. Note that the Phone field isn't modified and that
 // the XML <Company> element is ignored. Also, the Groups field is assigned
index aacb50c9cfcb3eefb3c38c7f13378b86e3bb9661..803805fed30f3474723ef76a10d7fd200c936180 100644 (file)
@@ -81,8 +81,7 @@ func Marshal(v interface{}) ([]byte, error) {
 func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
        var b bytes.Buffer
        enc := NewEncoder(&b)
-       enc.prefix = prefix
-       enc.indent = indent
+       enc.Indent(prefix, indent)
        if err := enc.Encode(v); err != nil {
                return nil, err
        }
@@ -99,6 +98,14 @@ func NewEncoder(w io.Writer) *Encoder {
        return &Encoder{printer{Writer: bufio.NewWriter(w)}}
 }
 
+// Indent sets the encoder to generate XML in which each element
+// begins on a new indented line that starts with prefix and is followed by
+// one or more copies of indent according to the nesting depth.
+func (enc *Encoder) Indent(prefix, indent string) {
+       enc.prefix = prefix
+       enc.indent = indent
+}
+
 // Encode writes the XML encoding of v to the stream.
 //
 // See the documentation for Marshal for details about the conversion