goxml

package module
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: BSD-3-Clause Imports: 6 Imported by: 6

README

Go reference documentation

Go XML

Go XML is a DOM based XML representation for Go. The entire XML file is read into a set of structs and can be accessed without keeping the source file open. You can also use this library to construct and serialize an XML file.

Used in https://github.com/speedata/goxpath

Sample usage

myfile.xml:

<data attrib="hello">
    <p>hello world!</p>
</data>

main.go:

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/speedata/goxml"
)

func dothings() error {
	r, err := os.Open("myfile.xml")
	if err != nil {
		return err
	}
	doc, err := goxml.Parse(r)
	if err != nil {
		return err
	}
	rootElt, err := doc.Root()
	if err != nil {
		return err
	}
	fmt.Println(rootElt.Attributes()) // [attrib="hello"]
	_ = rootElt.Parent                // document node

	return nil
}

func main() {
	if err := dothings(); err != nil {
		log.Fatal(err)
	}
}

Constructing an XML file:

package main

import (
	"encoding/xml"
	"fmt"

	"github.com/speedata/goxml"
)

func main() {
	d := goxml.XMLDocument{}
	root := &goxml.Element{Name: "root"}
	d.Append(root)
	cd := goxml.CharData{Contents: "\n   "}
	root.Append(cd)
	elt1 := &goxml.Element{Name: "element"}
	elt1.SetAttribute(xml.Attr{Name: xml.Name{Local: "attr"}, Value: "element 1"})
	elt1.SetAttribute(xml.Attr{Name: xml.Name{Local: "attr2"}, Value: "some <value> &'"})
	root.Append(elt1)
	root.Append(cd)
	elt2 := &goxml.Element{Name: "element"}
	elt2.SetAttribute(xml.Attr{Name: xml.Name{Local: "attr"}, Value: "element 2"})
	root.Append(elt2)
	root.Append(goxml.CharData{Contents: "\n"})
	fmt.Println(d.ToXML())
}

prints

<root>
   <element attr="element 1" attr2="some &lt;value> &amp;'" />
   <element attr="element 2" />
</root>

License: BSD-3-Clause License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewID added in v1.0.8

func NewID() int

NewID returns a unique node ID. Use this when constructing XML nodes programmatically outside of the parser.

Types

type Appender

type Appender interface {
	Append(n XMLNode)
}

Appender implements the function Append(XMLNode)

type Attribute

type Attribute struct {
	ID        int
	Name      string
	Namespace string
	Prefix    string
	Value     string
	Parent    XMLNode
}

Attribute represents an attribute

func (Attribute) Children

func (a Attribute) Children() []XMLNode

Children returns the empty sequence.

func (Attribute) GetID added in v1.0.7

func (a Attribute) GetID() int

GetID returns the ID of this node.

func (Attribute) String

func (a Attribute) String() string

func (Attribute) Stringvalue

func (a Attribute) Stringvalue() string

Stringvalue returns the attribute value.

type CharData

type CharData struct {
	ID       int
	Contents string
}

CharData is a string

func (CharData) Children

func (cd CharData) Children() []XMLNode

Children is a dummy function

func (CharData) GetID added in v1.0.7

func (cd CharData) GetID() int

GetID returns the ID of this node.

type Comment

type Comment struct {
	ID       int
	Contents string
}

Comment is a string

func (Comment) Children

func (cmt Comment) Children() []XMLNode

Children is a dummy function

func (Comment) GetID added in v1.0.7

func (cmt Comment) GetID() int

GetID returns the ID of this node.

type Element

type Element struct {
	ID         int
	Name       string
	Prefix     string
	Parent     XMLNode
	Namespaces map[string]string

	Line int
	Pos  int
	// contains filtered or unexported fields
}

Element represents an XML element

func NewElement

func NewElement() *Element

NewElement returns an initialized Element.

func (*Element) Append

func (elt *Element) Append(n XMLNode)

Append appends an XML node to the element.

func (*Element) Attributes

func (elt *Element) Attributes() []*Attribute

Attributes returns all attributes for this element. Attribute objects are cached so they have stable IDs across calls.

func (*Element) Children

func (elt *Element) Children() []XMLNode

Children returns all child nodes from elt

func (*Element) GetID added in v1.0.7

func (elt *Element) GetID() int

GetID returns the ID of this node.

func (*Element) InnerXML added in v1.0.5

func (elt *Element) InnerXML() string

InnerXML returns the XML representation of the children of this element.

func (*Element) SetAttribute added in v1.0.1

func (elt *Element) SetAttribute(attr xml.Attr)

SetAttribute appends attr to the list of attributes of elt. If an attribute of this name already exists, the existing one will be discarded.

func (Element) String

func (elt Element) String() string

func (*Element) Stringvalue

func (elt *Element) Stringvalue() string

Stringvalue returns the text nodes of this elements and its children.

func (*Element) ToXML

func (elt *Element) ToXML() string

ToXML returns a valid XML document

type ProcInst

type ProcInst struct {
	ID     int
	Target string
	Inst   []byte
}

ProcInst is a string

func (ProcInst) Children

func (pi ProcInst) Children() []XMLNode

Children is a dummy function

func (ProcInst) GetID added in v1.0.7

func (pi ProcInst) GetID() int

GetID returns the ID of this node.

type SortByDocumentOrder

type SortByDocumentOrder []XMLNode

SortByDocumentOrder sorts the nodes by document order.

func (SortByDocumentOrder) Len

func (xn SortByDocumentOrder) Len() int

func (SortByDocumentOrder) Less

func (xn SortByDocumentOrder) Less(i, j int) bool

func (SortByDocumentOrder) SortAndEliminateDuplicates

func (xn SortByDocumentOrder) SortAndEliminateDuplicates() SortByDocumentOrder

SortAndEliminateDuplicates returns the nodes sorted in document order and duplicates deleted.

func (SortByDocumentOrder) Swap

func (xn SortByDocumentOrder) Swap(i, j int)

type XMLDocument

type XMLDocument struct {
	ID int
	// contains filtered or unexported fields
}

XMLDocument represents an XML file for decoding

func Parse

func Parse(r io.Reader) (*XMLDocument, error)

Parse reads the XML file from r. r is not closed.

func (*XMLDocument) Append

func (xr *XMLDocument) Append(n XMLNode)

Append appends an XML node to the document.

func (*XMLDocument) Children

func (xr *XMLDocument) Children() []XMLNode

Children returns all child nodes from elt

func (XMLDocument) GetID added in v1.0.7

func (xr XMLDocument) GetID() int

GetID returns the ID of this node.

func (*XMLDocument) Root

func (xr *XMLDocument) Root() (*Element, error)

Root returns the root node of the document

func (XMLDocument) String

func (xr XMLDocument) String() string

func (*XMLDocument) Stringvalue added in v1.0.7

func (xr *XMLDocument) Stringvalue() string

Stringvalue returns the string value of the document node (concatenation of all descendant text nodes), analogous to Element.Stringvalue().

func (*XMLDocument) ToXML

func (xr *XMLDocument) ToXML() string

ToXML returns a valid XML document

type XMLNode

type XMLNode interface {

	// GetID returns a unique integer identifier for this node.
	GetID() int
	Children() []XMLNode
	// contains filtered or unexported methods
}

XMLNode is one of Document, Element, CharData, ProcInst, Comment

Jump to

Keyboard shortcuts

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