Bitbucket is a code hosting site with unlimited public and private repositories. We're also free for small teams!

xmlm - An XML Marshalling/Unmarshalling package for go

xmlm uses go's struct's tag feature for precise control over how your elements are marshalled.

See xmlm_test.go for an example. Here's an overview:

type MyType struct {
	MyNumber int "my-number" //<my-number> is the tag
	MyArray []int "items|item" //will produce <items><item>1</item><item>2</item>...</items>
	MyOtherArray []float "__parent|float"	//__parent instructs us not to descend, so this 
						//generates (or gets) <float></float> tags at the current level.

To marshal, call:

HumanReadable := true
xml, err := xmlm.Marshal(MyStruct, HumanReadable)

To unmarshal, call:

MyStruct := (*MyType)(nil)
err := Unmarshal(InputXML, &MyStruct, CustomTypes)

where CustomTypes, if not nil, maps tag names to types like this: 

var CustomTypes = map[string]interface{} {
	"custom1": Custom{},
	"custom2": int(0),
	"custom3": (*int)(nil),

CustomTypes is only used by Unmarshal if MyType contains interface values.

If MyType is a root node, you should write a Name() function:

func (o MyType) Name() (string, bool) {return "my-root", true}

If it has attributes, you should write an Attributes() function:

func (o MyType) Attributes() map[string]string {return map[string]string{"xmlns": ""}}

If it should interpret attributes when being unmarshalled, write a SetAttribute member:

func (o *MyType) SetAttribute(name, value string) {xmlm.SetAttr(&o.Attrs, name, value)}

(Note this needs to be on *MyType, not MyType. xmlm.SetAttr() is provided for convienience 
if you keep your attributes in a map). 

If you would like to produce or parse your own value, you may write one or both of the functions:

func (o MyType) Value() (string, bool) {return "MyValue", true}
func (o *MyType) Parse(xml string) bool {
	//return true to stop descent, false to continue it

Known problems:
Unmarshal currently has no way of returning an error...

Recent activity


Commits by lavalamp were pushed to lavalamp/xmlm

c792441 - Fix very confusing example in the readme. And, just to clarify the last commit message, it works with release-r57. You'll have to fix one line ...

Commits by lavalamp were pushed to lavalamp/xmlm

c0c732a - Fix to run with most recent release.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.