anyxml

package module
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2018 License: BSD-3-Clause, MIT Imports: 6 Imported by: 2

README

anyxml - create an XML document from almost any Go type

Marshal XML from map[string]interface{}, arrays, slices, and alpha/numeric values.

This wraps encoding/xml with github.com/clbanning/mxj functionality. See mxj package documentation for caveats, etc.

XML encoding conventions

  • 'nil' Map values, which may represent 'null' JSON values, are encoded as '<tag/>' unless XmlGoEmptyElemSyntax() has been called to change the default to encoding/xml syntax, '<tag></tag>'.
  • In map[string]interface{} values keys that are prepended by a hyphen, '-', are assumed to be attributes.

Caveat

Since some values, such as arrays, may require injecting tag labels to generate the XML, unmarshaling the resultant XML is not necessarily symmetric, i.e., you cannot get the original value back without some manipulation.

Documentation

http://godoc.org/github.com/clbanning/anyxml

Example

Encode an arbitrary JSON object.


package main

import (
	"encoding/json"
	"fmt"
	"github.com/clbanning/anyxml"
)

func main() {
	jasondata := []byte(`[
		{ "somekey":"somevalue" },
		"string",
		3.14159265,
		true
	]`)
	var i interface{}
	err := json.Unmarshal(jsondata, &i)
	if err != nil {
		// do something
	}
	x, err := anyxml.XmlIndent(i, "", "  ", "mydoc")
	if err != nil {
		// do something else
	}
	fmt.Println(string(x))
}

output:
	<mydoc>
	  <somekey>somevalue</somekey>
	  <element>string</element>
	  <element>3.14159265</element>
	  <element>true</element>
	</mydoc>

Documentation

Overview

anyxml - marshal an XML document from almost any Go variable. Marshal XML from map[string]interface{}, arrays, slices, alpha/numeric, etc.

Wraps xml.Marshal with functionality in github.com/clbanning/mxj to create a more genericized XML marshaling capability. Note: unmarshaling the resultant XML may not return the original value, since tag labels may have been injected to create the XML representation of the value.

See mxj package documentation for more information. See anyxml_test.go for examples or just try Xml() or XmlIndent().

 Encode an arbitrary JSON object.
	package main

	import (
		"encoding/json"
		"fmt"
		"github.com/clbanning/anyxml"
	)

	func main() {
		jsondata := []byte(`[
			{ "somekey":"somevalue" },
			"string",
			3.14159265,
			true
		]`)
		var i interface{}
		err := json.Unmarshal(jsondata, &i)
		if err != nil {
			// do something
		}
		x, err := anyxml.XmlIndent(i, "", "  ", "mydoc")
		if err != nil {
			// do something else
		}
		fmt.Println(string(x))
	}

	output:
		<mydoc>
		  <somekey>somevalue</somekey>
		  <element>string</element>
		  <element>3.14159265</element>
		  <element>true</element>
		</mydoc>

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultRootTag          = "doc"
	UseGoEmptyElementSyntax = false // if 'true' encode empty element as "<tag></tag>" instead of "<tag/>
)

Functions

func XMLEscapeChars

func XMLEscapeChars(b bool)

XMLEscapeChars(true) forces escaping invalid characters in attribute and element values. NOTE: this is brute force with NO interrogation of '&' being escaped already; if it is then '&amp;' will be re-escaped as '&amp;amp;'.

The values are:
"   &quot;
'   &apos;
<   &lt;
>   &gt;
&   &amp;

func Xml

func Xml(v interface{}, rootTag ...string) ([]byte, error)

Encode arbitrary value as XML. Note: there are no guarantees.

func XmlIndent

func XmlIndent(v interface{}, prefix, indent string, rootTag ...string) ([]byte, error)

Encode an arbitrary value as a pretty XML string. Note: there are no guarantees.

Types

This section is empty.

Jump to

Keyboard shortcuts

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