dom

package
v0.0.0-...-0bff936 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2018 License: Apache-2.0 Imports: 8 Imported by: 19

Documentation

Overview

Package dom implements a simple XML DOM that is a light wrapper on top of encoding/xml. It is oriented towards processing XML used as an RPC encoding mechanism (XMLRPC, SOAP, etc.), and not for general XML document processing. Specifically:

1. We ignore comments and document processing directives. They are stripped out as part of document processing.

2. We do not have seperate Text fields. Instead, each Element has a single Contents field which holds the contents of the last enclosed text in a tag.

Index

Constants

View Source
const NS_XS = "http://www.w3.org/2001/XMLSchema"
View Source
const NS_XSD = "http://www.w3.org/2001/XMLSchema-datatypes"
View Source
const NS_XSI = "http://www.w3.org/2001/XMLSchema-instance"
View Source
const TooManyRootElements = "More than one root Element not allowed!"

Variables

This section is empty.

Functions

func Attr

func Attr(name, space, value string) xml.Attr

Attr creates a new xml.Attr. It is exactly equivalent to creating a new xml.Attr with:

xml.Attr{
    Name: xml.Name{
        Local: name,
        Space: space,
    },
    Value: value,
}

Types

type Document

type Document struct {
	// contains filtered or unexported fields
}

A Document represents an entire XML document. Documents hold the root Element.

func CreateDocument

func CreateDocument() *Document

CreateDocument creates a new XML document.

func Parse

func Parse(r io.Reader) (doc *Document, err error)

Parse parses the XML document from the passed io.Reader and returns either a Document or an error if the io.Reader stream could not be parsed as a well-formed XML document.

func (*Document) Bytes

func (doc *Document) Bytes() []byte

Bytes encodes a Document into a byte array. The document will be pretty-printed.

func (*Document) Encode

func (doc *Document) Encode(e *Encoder) (err error)

Encode encodes the entire Document using the passed-in Encoder. The output is a well-formed XML document.

func (*Document) Reader

func (doc *Document) Reader() *bytes.Reader

Reader returns a bytes.Reader that can be used wherever something wants to consume this document via io.Reader This would be implemented using io.Pipe() for some nice streaming reads, but that does not play nice for some reason when using the returned Reader as an http.Request.Body

func (*Document) Root

func (doc *Document) Root() (node *Element)

Root returns the root element of the document.

func (*Document) SetRoot

func (doc *Document) SetRoot(node *Element)

SetRoot sets the new root element of the document.

func (*Document) String

func (doc *Document) String() string

String returns the result of stringifying the byte array that Bytes returns.

type Element

type Element struct {
	Name xml.Name

	// Unlike a full-fledged XML DOM, we only have a single Content field
	// instead of representing Text nodes seperately.
	Content    []byte
	Attributes []xml.Attr
	// contains filtered or unexported fields
}

Element represents a node in an XML document. Elements are arranged in a tree which corresponds to the structure of the XML documents.

func CreateElement

func CreateElement(n xml.Name) *Element

CreateElement creates a new element with the passed-in xml.Name. The created Element has no parent, no children, no content, and no attributes.

func Elem

func Elem(name, space string) *Element

Elem creates a new Element. It is equivalent to creating a new Element with:

CreateElement(xml.Name{Local: name, Space: space})

func ElemC

func ElemC(name, space, content string) *Element

ElemC creates a new Element with Content. It is equivalent to creating a new Element with:

e := Elem(name,space)
e.Content = []byte(content)

func ParseElements

func ParseElements(r io.Reader) (elements []*Element, err error)

ParseElements parses the XML elements in the passed io.Reader and returns an array of parsed Elements and an error. If error is not nil, then all the elements in the Reader were parsed corrently.

This assumes our input is always UTF-8, no matter what lies the <?xml?> header says.

func (*Element) AddAttr

func (node *Element) AddAttr(attr xml.Attr) *Element

AddAttr adds attr to node. Duplicates are ignored. If attr has the same name as a preexisting attribute, then it will replace the preexsting attribute. Return is node.

func (*Element) AddChild

func (node *Element) AddChild(child *Element) *Element

AddChild adds child to node. child will be reparented if needed. The return value is node.

func (*Element) AddChildren

func (node *Element) AddChildren(children ...*Element) *Element

AddChildren adds children to node. The children will be reparented as needed. The return value is node.

func (*Element) All

func (node *Element) All() []*Element

All returns node + node.Descendants()

func (*Element) Ancestors

func (node *Element) Ancestors() (res []*Element)

Ancestors returns all the ancestors of this node with the most distant ancestor last.

func (*Element) Attr

func (node *Element) Attr(name, space, value string) *Element

Attr creates a new xml.Attr and adds it to node. It is equivalent to:

node.AddAttr(xml.Attr{
    Name: xml.Name{
        Space: space,
        Local: name,
    },
    Value: value,
})

func (*Element) Bytes

func (node *Element) Bytes() []byte

Bytes returns a pretty-printed XML encoding of this part of the tree. The return is a byte array.

func (*Element) Children

func (node *Element) Children() (res []*Element)

Children returns all the children of node.

func (*Element) Descendants

func (node *Element) Descendants() (res []*Element)

Descendants returns all descendants of node in breadth order.

func (*Element) Encode

func (node *Element) Encode(e *Encoder) (err error)

Encode encodes an element using the passed-in Encoder. If an error occurs during encoding, that error is returned.

func (*Element) GetAttr

func (node *Element) GetAttr(name, space, val string) []xml.Attr

GetAttr returns all the matching Attrs on the node.

func (*Element) Parent

func (node *Element) Parent() *Element

Parent returns the parent of this node. If there is no parent, returns nil.

func (*Element) RemoveChild

func (node *Element) RemoveChild(child *Element) *Element

RemoveChild removes child from node. The removed child will be returned if it was actually a child of node, otherwise nil will be returned.

func (*Element) Replace

func (node *Element) Replace(other *Element) *Element

Replace performs an in-place replacement of node with other. other should not be used after this functions returns. node will be returned.

func (*Element) SetParent

func (node *Element) SetParent(parent *Element) *Element

SetParent makes parent the new parent of node, and returns node.

func (*Element) String

func (node *Element) String() string

String returns a pretty-printed XML encoding of this part of the tree.

The return is a string.

type Encoder

type Encoder struct {
	*bufio.Writer
	// contains filtered or unexported fields
}

Encoder holds the state needed to encode the DOM into a well-formed XML document.

func NewEncoder

func NewEncoder(writer io.Writer) *Encoder

NewEncoder returns a new Encoder that will output to the passed-in io.Writer.

The encoded docuemnt will have all namespace declarations lifted to the root element of the document.

func (*Encoder) Pretty

func (e *Encoder) Pretty()

Pretty puts the passed Encoder into pretty-print mode.

Jump to

Keyboard shortcuts

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