dom

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2017 License: BSD-3-Clause Imports: 7 Imported by: 5

README

dom

License GoDoc Go Report Card Build Status codecov.io

Package dom provides document object model for xml.

It does not strictly follow DOM interfaces, but has everything needed for xml processing library.

Example

str := `
<developer xmlns="www.jroller.com/santhosh/">
<name>Santhosh Kumar Tekuri</name>
<email>santhosh.tekuri@gmail.com</email>
</developer>
`

doc, err := dom.Unmarshal(xml.NewDecoder(strings.NewReader(str)))
if err != nil {
    fmt.Println(err)
    return
}

root := doc.RootElement()
fmt.Printf("rootElement: {%s}%s\n", root.URI, root.Local)
buf := new(bytes.Buffer)
if err = dom.Marshal(doc, buf); err != nil {
    fmt.Println(err)
    return
}
fmt.Printf("xml:\n%s", buf.String())

Output:

rootElement: {www.jroller.com/santhosh/}developer
xml:
<developer xmlns="www.jroller.com/santhosh/">
    <name>Santhosh Kumar Tekuri</name>
    <email>santhosh.tekuri@gmail.com</email>
</developer>

Documentation

Overview

Package dom provides document object model for xml.

It does not strictly follow DOM interfaces, but has everything needed for xml processing library.

Example
package main

import (
	"bytes"
	"encoding/xml"
	"fmt"
	"strings"

	"github.com/santhosh-tekuri/dom"
)

func main() {
	str := `
<developer xmlns="www.jroller.com/santhosh/">
    <name>Santhosh Kumar Tekuri</name>
    <email>santhosh.tekuri@gmail.com</email>
</developer>
`

	doc, err := dom.Unmarshal(xml.NewDecoder(strings.NewReader(str)))
	if err != nil {
		fmt.Println(err)
		return
	}

	root := doc.RootElement()
	fmt.Printf("rootElement: {%s}%s\n", root.URI, root.Local)
	buf := new(bytes.Buffer)
	if err = dom.Marshal(doc, buf); err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("xml:\n%s", buf.String())
}
Output:

rootElement: {www.jroller.com/santhosh/}developer
xml:
<developer xmlns="www.jroller.com/santhosh/">
    <name>Santhosh Kumar Tekuri</name>
    <email>santhosh.tekuri@gmail.com</email>
</developer>

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(d *Document, w io.Writer) error

Marshal writes the XML encoding of d into w.

Types

type Attr

type Attr struct {
	Owner *Element
	*Name
	Value string
	Type  string
}

An Attr represents an attribute in an XML element (Name=Value).

func (*Attr) Parent

func (*Attr) Parent() Parent

func (*Attr) SetParent

func (*Attr) SetParent(Parent)

type Comment

type Comment struct {
	ParentNode Parent
	Data       string
}

A Comment represents an XML comment of the form <!--comment-->. The bytes do not include the <!-- and --> comment markers.

func (*Comment) Parent

func (c *Comment) Parent() Parent

func (*Comment) SetParent

func (c *Comment) SetParent(p Parent)

type Document

type Document struct {
	ChildNodes []Node
}

A Document represents XML Document.

func OwnerDocument

func OwnerDocument(n Node) *Document

OwnerDocument returns The Document object associated with given node.

func Unmarshal

func Unmarshal(decoder *xml.Decoder) (*Document, error)

Unmarshal reads tokens from decoder and constructs Document object.

func (*Document) Append

func (d *Document) Append(child Node) error

func (*Document) Children

func (d *Document) Children() []Node

func (*Document) Parent

func (*Document) Parent() Parent

func (*Document) RootElement

func (d *Document) RootElement() *Element

RootElement returns the root element of the document

func (*Document) SetParent

func (*Document) SetParent(Parent)

type Element

type Element struct {
	ParentNode Parent
	*Name
	NSDecl     map[string]string
	Attrs      []*Attr
	ChildNodes []Node
}

An Element represents an XML element.

func (*Element) Append

func (e *Element) Append(child Node) error

func (*Element) Children

func (e *Element) Children() []Node

func (*Element) GetAttr

func (e *Element) GetAttr(uri, local string) *Attr

GetAttr returns the attribute with given uri and local. It returns null, if attribute is not found.

func (*Element) Parent

func (e *Element) Parent() Parent

func (*Element) ResolvePrefix

func (e *Element) ResolvePrefix(prefix string) (string, bool)

ResolvePrefix returns the URI bound to given prefix. The second return value tells whether prefix is bound or not.

func (*Element) SetParent

func (e *Element) SetParent(p Parent)

type Name

type Name struct {
	URI    string
	Prefix string
	Local  string
}

A Name represents an XML name.

func (*Name) String

func (n *Name) String() string

String returns qualified name

type NameSpace

type NameSpace struct {
	Owner  *Element
	Prefix string
	URI    string
}

A NameSpace represents namespace node. This is only used by xpath engines.

func (*NameSpace) Parent

func (*NameSpace) Parent() Parent

func (*NameSpace) SetParent

func (n *NameSpace) SetParent(Parent)

type Node

type Node interface {
	Parent() Parent
	SetParent(p Parent)
}

A Node is an interface holding one of the types: *Document, *Element, *Text, *Comment, *ProcInst, *Attr or *Namespace.

func Owner

func Owner(n Node) Node

Owner returns the node who owns the node

for *Attr and *Namespace it returns Owner field, for others it returns their parent Node

type Parent

type Parent interface {
	Node
	Append(child Node) error
	Children() []Node
}

A Parent is an interface holding one of the types: *Document or *Element.

type ProcInst

type ProcInst struct {
	ParentNode Parent
	Target     string
	Data       string
}

A ProcInst represents an XML processing instruction of the form <?target data?>

func (*ProcInst) Parent

func (pi *ProcInst) Parent() Parent

func (*ProcInst) SetParent

func (pi *ProcInst) SetParent(p Parent)

type Text

type Text struct {
	ParentNode Parent
	Data       string
}

A Text represents XML character data (raw text), in which XML escape sequences have been replaced by the characters they represent.

func (*Text) Parent

func (t *Text) Parent() Parent

func (*Text) SetParent

func (t *Text) SetParent(p Parent)

Jump to

Keyboard shortcuts

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