xml

package module
v0.0.0-...-c04bcd0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2015 License: BSD-3-Clause Imports: 8 Imported by: 0

README

Usage:
   Use xml.SetUseNullEndTag(true) to get the empty element in xml(like <LastName/> when LastName is nil or empty)

Documentation

Index

Examples

Constants

View Source
const (
	// A generic XML header suitable for use with the output of Marshal.
	// This is not automatically added to any output of this package,
	// it is provided as a convenience.
	Header = `<?xml version="1.0" encoding="UTF-8"?>` + "\n"
)

Variables

This section is empty.

Functions

func Marshal

func Marshal(v interface{}) ([]byte, error)

Marshal returns the XML encoding of v.

Marshal handles an array or slice by marshalling each of the elements. Marshal handles a pointer by marshalling the value it points at or, if the pointer is nil, by writing nothing. Marshal handles an interface value by marshalling the value it contains or, if the interface value is nil, by writing nothing. Marshal handles all other data by writing one or more XML elements containing the data.

The name for the XML elements is taken from, in order of preference:

  • the tag on the XMLName field, if the data is a struct
  • the value of the XMLName field of type xml.Name
  • the tag of the struct field used to obtain the data
  • the name of the struct field used to obtain the data
  • the name of the marshalled type

The XML element for a struct contains marshalled elements for each of the exported fields of the struct, with these exceptions:

  • the XMLName field, described above, is omitted.
  • a field with tag "-" is omitted.
  • a field with tag "name,attr" becomes an attribute with the given name in the XML element.
  • a field with tag ",attr" becomes an attribute with the field name in the XML element.
  • a field with tag ",chardata" is written as character data, not as an XML element.
  • a field with tag ",innerxml" is written verbatim, not subject to the usual marshalling procedure.
  • a field with tag ",comment" is written as an XML comment, not subject to the usual marshalling procedure. It must not contain the "--" string within it.
  • a field with a tag including the "omitempty" option is omitted if the field value is empty. The empty values are false, 0, any nil pointer or interface value, and any array, slice, map, or string of length zero.
  • an anonymous struct field is handled as if the fields of its value were part of the outer struct.

If a field uses a tag "a>b>c", then the element c will be nested inside parent elements a and b. Fields that appear next to each other that name the same parent will be enclosed in one XML element.

See MarshalIndent for an example.

Marshal will return an error if asked to marshal a channel, function, or map.

func MarshalIndent

func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error)

MarshalIndent works like Marshal, but each XML 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.

Example
package main

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

func main() {
	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"}

	output, err := xml.MarshalIndent(v, "  ", "    ")
	if err != nil {
		fmt.Printf("error: %v\n", err)
	}

	os.Stdout.Write(output)
}
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>

func SetUseNullEndTag

func SetUseNullEndTag(b bool)

SetUseNullEndTag - 'true': have Marshal/MarshalIndent use <tag .../> in place of <tag ...></tag> for empty element.

Example

This example demonstrates how SetUseNullEndTag changes the end tag syntax for Marshal.

package main

import (
	"encoding/xml"
	"fmt"
)

func main() {
	type NullValStruct struct {
		I int
		B []byte
		S string
	}
	s := new(NullValStruct)

	v, err := xml.Marshal(s)
	if err != nil {
		fmt.Println("err:", err.Error())
	} else {
		fmt.Println("s:", string(v))
	}

	xml.SetUseNullEndTag(true)
	v, err = xml.Marshal(s)
	if err != nil {
		fmt.Println("err:", err.Error())
	} else {
		fmt.Println("s:", string(v))
	}

	type NewStruct struct {
		NVS NullValStruct
		S   string
		F   float64
	}
	ss := new(NewStruct)

	v, err = xml.Marshal(ss)
	if err != nil {
		fmt.Println("err:", err.Error())
	} else {
		fmt.Println("ss:", string(v))
	}

	v, err = xml.MarshalIndent(ss, "  ", "   ")
	if err != nil {
		fmt.Println("err:", err.Error())
	} else {
		fmt.Println("ss indent:\n", string(v))
	}
}
Output:

s: <NullValStruct><I>0</I><B></B><S></S></NullValStruct>
s: <NullValStruct><I>0</I><B/><S/></NullValStruct>
ss: <NewStruct><NVS><I>0</I><B/><S/></NVS><S/><F>0</F></NewStruct>
ss indent:
   <NewStruct>
     <NVS>
        <I>0</I>
        <B/>
        <S/>
     </NVS>
     <S/>
     <F>0</F>
  </NewStruct>

Types

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

An Encoder writes XML data to an output stream.

Example
package main

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

func main() {
	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>

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new encoder that writes to w.

func (*Encoder) Encode

func (enc *Encoder) Encode(v interface{}) error

Encode writes the XML encoding of v to the stream.

See the documentation for Marshal for details about the conversion of Go values to XML.

Encode calls Flush before returning.

func (*Encoder) EncodeElement

func (enc *Encoder) EncodeElement(v interface{}, start StartElement) error

EncodeElement writes the XML encoding of v to the stream, using start as the outermost tag in the encoding.

See the documentation for Marshal for details about the conversion of Go values to XML.

EncodeElement calls Flush before returning.

func (*Encoder) EncodeToken

func (enc *Encoder) EncodeToken(t Token) error

EncodeToken writes the given XML token to the stream. It returns an error if StartElement and EndElement tokens are not properly matched.

EncodeToken does not call Flush, because usually it is part of a larger operation such as Encode or EncodeElement (or a custom Marshaler's MarshalXML invoked during those), and those will call Flush when finished.

Callers that create an Encoder and then invoke EncodeToken directly, without using Encode or EncodeElement, need to call Flush when finished to ensure that the XML is written to the underlying writer.

func (*Encoder) Flush

func (enc *Encoder) Flush() error

Flush flushes any buffered XML to the underlying writer. See the EncodeToken documentation for details about when it is necessary.

func (*Encoder) Indent

func (enc *Encoder) Indent(prefix, indent string)

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.

type Marshaler

type Marshaler interface {
	MarshalXML(e *Encoder, start StartElement) error
}

Marshaler is the interface implemented by objects that can marshal themselves into valid XML elements.

MarshalXML encodes the receiver as zero or more XML elements. By convention, arrays or slices are typically encoded as a sequence of elements, one per entry. Using start as the element tag is not required, but doing so will enable Unmarshal to match the XML elements to the correct struct field. One common implementation strategy is to construct a separate value with a layout corresponding to the desired XML and then to encode it using e.EncodeElement. Another common strategy is to use repeated calls to e.EncodeToken to generate the XML output one token at a time. The sequence of encoded tokens must make up zero or more valid XML elements.

type MarshalerAttr

type MarshalerAttr interface {
	MarshalXMLAttr(name Name) (Attr, error)
}

MarshalerAttr is the interface implemented by objects that can marshal themselves into valid XML attributes.

MarshalXMLAttr returns an XML attribute with the encoded value of the receiver. Using name as the attribute name is not required, but doing so will enable Unmarshal to match the attribute to the correct struct field. If MarshalXMLAttr returns the zero attribute Attr{}, no attribute will be generated in the output. MarshalXMLAttr is used only for struct fields with the "attr" option in the field tag.

type UnsupportedTypeError

type UnsupportedTypeError struct {
	Type reflect.Type
}

A MarshalXMLError is returned when Marshal encounters a type that cannot be converted into XML.

func (*UnsupportedTypeError) Error

func (e *UnsupportedTypeError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL