xml

package
v0.0.0-...-f8c0f81 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2011 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package xml implements a simple XML 1.0 parser that understands XML name spaces.

Index

Constants

View Source
const (
	// A generic XML header suitable for use with the output of Marshal and
	// MarshalIndent.  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

View Source
var HTMLAutoClose = htmlAutoClose

HTMLAutoClose is the set of HTML elements that should be considered to close automatically.

View Source
var HTMLEntity = htmlEntity

HTMLEntity is an entity map containing translations for the standard HTML entity characters.

Functions

func Escape

func Escape(w io.Writer, s []byte)

Escape writes to w the properly escaped XML equivalent of the plain text data s.

func Marshal

func Marshal(w io.Writer, v interface{}) (err os.Error)

Marshal writes an XML-formatted representation of v to w.

If v implements Marshaler, then Marshal calls its MarshalXML method. Otherwise, Marshal uses the following procedure to create the XML.

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 an XMLName field, if the data is a struct
  • the value of an 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 '???'.

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 "attr" becomes an attribute 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.

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. For example:

type Result struct {
	XMLName   xml.Name `xml:"result"`
	FirstName string   `xml:"person>name>first"`
	LastName  string   `xml:"person>name>last"`
	Age       int      `xml:"person>age"`
}

xml.Marshal(w, &Result{FirstName: "John", LastName: "Doe", Age: 42})

would be marshalled as:

<result>
	<person>
		<name>
			<first>John</first>
			<last>Doe</last>
		</name>
		<age>42</age>
	</person>
</result>

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

func Unmarshal

func Unmarshal(r io.Reader, val interface{}) os.Error

Unmarshal parses an XML element from r and uses the reflect library to fill in an arbitrary struct, slice, or string pointed at by val. Well-formed data that does not fit into val is discarded.

For example, given these definitions:

type Email struct {
	Where string `xml:"attr"`
	Addr  string
}

type Result struct {
	XMLName xml.Name `xml:"result"`
	Name	string
	Phone	string
	Email	[]Email
	Groups  []string `xml:"group>value"`
}

result := Result{Name: "name", Phone: "phone", Email: nil}

unmarshalling the XML input

<result>
	<email where="home">
		<addr>gre@example.com</addr>
	</email>
	<email where='work'>
		<addr>gre@work.com</addr>
	</email>
	<name>Grace R. Emlin</name>
	<group>
		<value>Friends</value>
		<value>Squash</value>
	</group>
	<address>123 Main Street</address>
</result>

via Unmarshal(r, &result) is equivalent to assigning

r = Result{xml.Name{"", "result"},
	"Grace R. Emlin", // name
	"phone",	  // no phone given
	[]Email{
		Email{"home", "gre@example.com"},
		Email{"work", "gre@work.com"},
	},
	[]string{"Friends", "Squash"},
}

Note that the field r.Phone has not been modified and that the XML <address> element was discarded. Also, the field Groups was assigned considering the element path provided in the field tag.

Because Unmarshal uses the reflect package, it can only assign to exported (upper case) fields. Unmarshal uses a case-insensitive comparison to match XML element names to struct field names.

Unmarshal maps an XML element to a struct using the following rules. In the rules, the tag of a field refers to the value associated with the key 'xml' in the struct field's tag (see the example above).

  • If the struct has a field of type []byte or string with tag "innerxml", Unmarshal accumulates the raw XML nested inside the element in that field. The rest of the rules still apply.

  • If the struct has a field named XMLName of type xml.Name, Unmarshal records the element name in that field.

  • If the XMLName field has an associated tag of the form "name" or "namespace-URL name", the XML element must have the given name (and, optionally, name space) or else Unmarshal returns an error.

  • If the XML element has an attribute whose name matches a struct field of type string with tag "attr", Unmarshal records the attribute value in that field.

  • If the XML element contains character data, that data is accumulated in the first struct field that has tag "chardata". The struct field may have type []byte or string. If there is no such field, the character data is discarded.

  • If the XML element contains comments, they are accumulated in the first struct field that has tag "comments". The struct field may have type []byte or string. If there is no such field, the comments are discarded.

  • If the XML element contains a sub-element whose name matches the prefix of a tag formatted as "a>b>c", unmarshal will descend into the XML structure looking for elements with the given names, and will map the innermost elements to that struct field. A tag starting with ">" is equivalent to one starting with the field name followed by ">".

  • If the XML element contains a sub-element whose name matches a field whose tag is neither "attr" nor "chardata", Unmarshal maps the sub-element to that struct field. Otherwise, if the struct has a field named Any, unmarshal maps the sub-element to that struct field.

Unmarshal maps an XML element to a string or []byte by saving the concatenation of that element's character data in the string or []byte.

Unmarshal maps an attribute value to a string or []byte by saving the value in the string or slice.

Unmarshal maps an XML element to a slice by extending the length of the slice and mapping the element to the newly created value.

Unmarshal maps an XML element or attribute value to a bool by setting it to the boolean value represented by the string.

Unmarshal maps an XML element or attribute value to an integer or floating-point field by setting the field to the result of interpreting the string value in decimal. There is no check for overflow.

Unmarshal maps an XML element to an xml.Name by recording the element name.

Unmarshal maps an XML element to a pointer by setting the pointer to a freshly allocated value and then mapping the element to that value.

Types

type Attr

type Attr struct {
	Name  Name
	Value string
}

An Attr represents an attribute in an XML element (Name=Value).

type CharData

type CharData []byte

A CharData represents XML character data (raw text), in which XML escape sequences have been replaced by the characters they represent.

func (CharData) Copy

func (c CharData) Copy() CharData

type Comment

type Comment []byte

A Comment represents an XML comment of the form <!--comment-->. The bytes do not include the <!-- and --> comment markers.

func (Comment) Copy

func (c Comment) Copy() Comment

type Directive

type Directive []byte

A Directive represents an XML directive of the form <!text>. The bytes do not include the <! and > markers.

func (Directive) Copy

func (d Directive) Copy() Directive

type EndElement

type EndElement struct {
	Name Name
}

An EndElement represents an XML end element.

type Marshaler

type Marshaler interface {
	MarshalXML() ([]byte, os.Error)
}

A Marshaler can produce well-formatted XML representing its internal state. It is used by both Marshal and MarshalIndent.

type Name

type Name struct {
	Space, Local string
}

A Name represents an XML name (Local) annotated with a name space identifier (Space). In tokens returned by Parser.Token, the Space identifier is given as a canonical URL, not the short prefix used in the document being parsed.

type Parser

type Parser struct {
	// Strict defaults to true, enforcing the requirements
	// of the XML specification.
	// If set to false, the parser allows input containing common
	// mistakes:
	//	* If an element is missing an end tag, the parser invents
	//	  end tags as necessary to keep the return values from Token
	//	  properly balanced.
	//	* In attribute values and character data, unknown or malformed
	//	  character entities (sequences beginning with &) are left alone.
	//
	// Setting:
	//
	//	p.Strict = false;
	//	p.AutoClose = HTMLAutoClose;
	//	p.Entity = HTMLEntity
	//
	// creates a parser that can handle typical HTML.
	Strict bool

	// When Strict == false, AutoClose indicates a set of elements to
	// consider closed immediately after they are opened, regardless
	// of whether an end element is present.
	AutoClose []string

	// Entity can be used to map non-standard entity names to string replacements.
	// The parser behaves as if these standard mappings are present in the map,
	// regardless of the actual map content:
	//
	//	"lt": "<",
	//	"gt": ">",
	//	"amp": "&",
	//	"apos": "'",
	//	"quot": `"`,
	Entity map[string]string

	// CharsetReader, if non-nil, defines a function to generate
	// charset-conversion readers, converting from the provided
	// non-UTF-8 charset into UTF-8. If CharsetReader is nil or
	// returns an error, parsing stops with an error. One of the
	// the CharsetReader's result values must be non-nil.
	CharsetReader func(charset string, input io.Reader) (io.Reader, os.Error)
	// contains filtered or unexported fields
}

A Parser represents an XML parser reading a particular input stream. The parser assumes that its input is encoded in UTF-8.

func NewParser

func NewParser(r io.Reader) *Parser

NewParser creates a new XML parser reading from r.

func (*Parser) RawToken

func (p *Parser) RawToken() (Token, os.Error)

RawToken is like Token but does not verify that start and end elements match and does not translate name space prefixes to their corresponding URLs.

func (*Parser) Skip

func (p *Parser) Skip() os.Error

Have already read a start element. Read tokens until we find the end element. Token is taking care of making sure the end element matches the start element we saw.

func (*Parser) Token

func (p *Parser) Token() (t Token, err os.Error)

Token returns the next XML token in the input stream. At the end of the input stream, Token returns nil, os.EOF.

Slices of bytes in the returned token data refer to the parser's internal buffer and remain valid only until the next call to Token. To acquire a copy of the bytes, call CopyToken or the token's Copy method.

Token expands self-closing elements such as <br/> into separate start and end elements returned by successive calls.

Token guarantees that the StartElement and EndElement tokens it returns are properly nested and matched: if Token encounters an unexpected end element, it will return an error.

Token implements XML name spaces as described by http://www.w3.org/TR/REC-xml-names/. Each of the Name structures contained in the Token has the Space set to the URL identifying its name space when known. If Token encounters an unrecognized name space prefix, it uses the prefix as the Space rather than report an error.

func (*Parser) Unmarshal

func (p *Parser) Unmarshal(val interface{}, start *StartElement) os.Error

The Parser's Unmarshal method is like xml.Unmarshal except that it can be passed a pointer to the initial start element, useful when a client reads some raw XML tokens itself but also defers to Unmarshal for some elements. Passing a nil start element indicates that Unmarshal should read the token stream to find the start element.

type ProcInst

type ProcInst struct {
	Target string
	Inst   []byte
}

A ProcInst represents an XML processing instruction of the form <?target inst?>

func (ProcInst) Copy

func (p ProcInst) Copy() ProcInst

type StartElement

type StartElement struct {
	Name Name
	Attr []Attr
}

A StartElement represents an XML start element.

func (StartElement) Copy

func (e StartElement) Copy() StartElement

type SyntaxError

type SyntaxError struct {
	Msg  string
	Line int
}

A SyntaxError represents a syntax error in the XML input stream.

func (*SyntaxError) String

func (e *SyntaxError) String() string

type TagPathError

type TagPathError struct {
	Struct       reflect.Type
	Field1, Tag1 string
	Field2, Tag2 string
}

A TagPathError represents an error in the unmarshalling process caused by the use of field tags with conflicting paths.

func (*TagPathError) String

func (e *TagPathError) String() string

type Token

type Token interface{}

A Token is an interface holding one of the token types: StartElement, EndElement, CharData, Comment, ProcInst, or Directive.

func CopyToken

func CopyToken(t Token) Token

CopyToken returns a copy of a Token.

type UnmarshalError

type UnmarshalError string

An UnmarshalError represents an error in the unmarshalling process.

func (UnmarshalError) String

func (e UnmarshalError) String() string

type UnsupportedTypeError

type UnsupportedTypeError struct {
	Type reflect.Type
}

A MarshalXMLError is returned when Marshal or MarshalIndent encounter a type that cannot be converted into XML.

func (*UnsupportedTypeError) String

func (e *UnsupportedTypeError) String() string

Notes

Bugs

  • Mapping between XML elements and data structures is inherently flawed: an XML element is an order-dependent collection of anonymous values, while a data structure is an order-independent collection of named values. See package json for a textual representation more suitable to data structures.

Jump to

Keyboard shortcuts

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