Documentation ¶
Overview ¶
Package xmlvalue is for XML parsing. It is used in situations those Go structures cannot achieve well. Most of the usages are quite simular as jsonvalue. (https://github.com/Andrew-M-C/go.jsonvalue)
This package is particularly useful in following situations:
1. Rapidly create an XML document with mutiple level.
2. Parsing XML documents with volatile structures.
Index ¶
- type Opt
- type S
- type V
- func (x *V) AddInt(i int) *S
- func (x *V) AddString(s string) *S
- func (x *V) Get(params ...interface{}) (*V, error)
- func (x *V) GetAttr(a string, defaultValue ...string) (attr string, exist bool)
- func (x *V) GetInt(params ...interface{}) (int, error)
- func (x *V) GetString(params ...interface{}) (string, error)
- func (x *V) Marshal(opt ...Opt) ([]byte, error)
- func (x *V) MarshalString(opt ...Opt) (string, error)
- func (x *V) MustMarshalString(opt ...Opt) string
- func (x *V) Name() string
- func (x *V) SetAttr(a, v string)
- func (x *V) SetInt(i int) *S
- func (x *V) SetString(s string) *S
- func (x *V) SetText(t interface{}) error
- func (x *V) Text() string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Opt ¶
type Opt struct { // Indent defines the indent string when marshaling. If indent string is empty, new line will not be added when marshaling. Indent string }
Opt provide optional preferances when marshaling the value
type S ¶
type S struct {
// contains filtered or unexported fields
}
S structures is a internal structure for SetXxx functions
type V ¶
type V struct {
// contains filtered or unexported fields
}
V represents an XML value
func UnmarshalString ¶
UnmarshalString is the same as Unmarshal(), but takes string as parameter
func (*V) AddString ¶
AddString starts adding a string to specified path
Example ¶
package main import ( "fmt" xmlvalue "github.com/Andrew-M-C/go.xmlvalue" ) func main() { x := xmlvalue.New("xml") x.AddString("Hello, world!").At("data", "message") x.AddString("Hello, world again!").At("data", "message") fmt.Println(x.MustMarshalString(xmlvalue.Opt{Indent: " "})) }
Output: <xml> <data> <message>Hello, world!</message> <message>Hello, world again!</message> </data> </xml>
func (*V) GetAttr ¶
GetAttr read attribute. If a default value given, the default attribute will be set and returned when the attribute does not exist.
func (*V) Marshal ¶
Marshal convert xml values into data in UTF-8 encoding. It only returns error when this XML value is nil or not generated by New() function.
func (*V) MarshalString ¶
MarshalString is same as Marshal(), but returns string instead
func (*V) MustMarshalString ¶
MustMarshalString is same as MarshalString(), but panics if error occured.
func (*V) SetString ¶
SetString starts setting a string to specified path
Example ¶
package main import ( "fmt" xmlvalue "github.com/Andrew-M-C/go.xmlvalue" ) func main() { x := xmlvalue.New("xml") x.SetString("Hello, world!").At("data", "message") fmt.Println(x.MustMarshalString(xmlvalue.Opt{Indent: " "})) }
Output: <xml> <data> <message>Hello, world!</message> </data> </xml>
func (*V) SetText ¶
SetText set raw text of this XML element. Both string and []byte type are accepted
Example ¶
package main import ( "fmt" xmlvalue "github.com/Andrew-M-C/go.xmlvalue" ) func main() { x := xmlvalue.New("xml") x.SetText("Hello, XML!") fmt.Println(x.MustMarshalString()) }
Output: <xml>Hello, XML!</xml>