xmldom

package module
v0.0.0-...-5258907 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

README

go-xmldom

Go Report Card GoDoc

XML DOM processing for Golang, supports xpath query

  • Parse XML into dom
  • Query node using xpath
  • Update XML using dom

Installation

$ go get github.com/subchen/go-xmldom

Basic Usage

xml := `<testsuite tests="2" failures="0" time="0.009" name="github.com/subchen/go-xmldom">
    <testcase classname="go-xmldom" name="ExampleParseXML" time="0.004"></testcase>
    <testcase classname="go-xmldom" name="ExampleParse" time="0.005"></testcase>
</testsuite>`

doc := xmldom.Must(xmldom.ParseXML(xml))
root := doc.Root

name := root.GetAttributeValue("name")
time := root.GetAttributeValue("time")
fmt.Printf("testsuite: name=%v, time=%v\n", name, time)

for _, node := range root.GetChildren("testcase") {
    name := node.GetAttributeValue("name")
    time := node.GetAttributeValue("time")
    fmt.Printf("testcase: name=%v, time=%v\n", name, time)
}

Xpath Query

// find all children
fmt.Printf("children = %v\n", len(node.Query("//*")))

// find node matched tag name
nodeList := node.Query("//testcase")
for _, c := range nodeList {
    fmt.Printf("%v: name = %v\n", c.Name, c.GetAttributeValue("name"))
}

// find node matched attr name
c := node.QueryOne("//testcase[@name='ExampleParseXML']")
fmt.Printf("%v: name = %v\n", c.Name, c.GetAttributeValue("name"))

Create XML

doc := xmldom.NewDocument("testsuites")

suiteNode := doc.Root.CreateNode("testsuite").SetAttributeValue("name", "github.com/subchen/go-xmldom")
suiteNode.CreateNode("testcase").SetAttributeValue("name", "case 1")
suiteNode.CreateNode("testcase").SetAttributeValue("name", "case 2")

fmt.Println(doc.XML())

License

go-xmldom is released under the Apache 2.0 license. See LICENSE

Documentation

Overview

XML DOM processing for Golang, supports xpath query

Index

Constants

View Source
const (
	DEFAULT_XML_HEADER = `<?xml version="1.0" encoding="UTF-8"?>`
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Attribute

type Attribute struct {
	NS    *Namespace
	Name  string
	Value string
}

type Document

type Document struct {
	ProcInst      string
	Directives    []string
	NamespaceList []*Namespace
	Root          *Node
}

func Must

func Must(doc *Document, err error) *Document

func NewDocument

func NewDocument(name string) *Document

func Parse

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

func ParseFile

func ParseFile(filename string) (*Document, error)

func ParseXML

func ParseXML(s string) (*Document, error)

func (*Document) XML

func (d *Document) XML() string

func (*Document) XMLPretty

func (d *Document) XMLPretty() string

type Namespace

type Namespace struct {
	Name string
	URI  string
}

func (*Namespace) IsDefault

func (ns *Namespace) IsDefault() bool

type Node

type Node struct {
	Document   *Document
	Parent     *Node
	NS         *Namespace
	Name       string
	Attributes []*Attribute
	Children   []*Node
	Text       string
}

func (*Node) AppendChild

func (n *Node) AppendChild(c *Node) *Node

func (*Node) CreateNode

func (n *Node) CreateNode(name string) *Node

func (*Node) CreateNodeAt

func (n *Node) CreateNodeAt(index int, name string) *Node

func (*Node) FindByID

func (n *Node) FindByID(id string) *Node

func (*Node) FindByName

func (n *Node) FindByName(name string) []*Node

func (*Node) FindOneByName

func (n *Node) FindOneByName(name string) *Node

func (*Node) FirstChild

func (n *Node) FirstChild() *Node

func (*Node) GetAttribute

func (n *Node) GetAttribute(name string) *Attribute

func (*Node) GetAttributeValue

func (n *Node) GetAttributeValue(name string) string

func (*Node) GetChild

func (n *Node) GetChild(name string) *Node

func (*Node) GetChildren

func (n *Node) GetChildren(name string) []*Node

func (*Node) IndexNode

func (n *Node) IndexNode(c *Node) int

func (*Node) LastChild

func (n *Node) LastChild() *Node

func (*Node) NextSibling

func (n *Node) NextSibling() *Node

func (*Node) PrevSibling

func (n *Node) PrevSibling() *Node

func (*Node) RemoveAttribute

func (n *Node) RemoveAttribute(name string) *Node

func (*Node) RemoveChild

func (n *Node) RemoveChild(c *Node) *Node

func (*Node) Root

func (n *Node) Root() *Node

func (*Node) SetAttributeValue

func (n *Node) SetAttributeValue(name string, value string) *Node

func (*Node) XML

func (n *Node) XML() string

func (*Node) XMLPretty

func (n *Node) XMLPretty() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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