xmldom

package module
Version: v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2018 License: Apache-2.0 Imports: 7 Imported by: 7

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

Examples

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 {
	Name  string
	Value string
}

type Document

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

func Must

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

func NewDocument

func NewDocument(name string) *Document
Example
doc := xmldom.NewDocument("testsuites")

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

fmt.Println(doc.XMLPretty())
Output:

<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
  <testsuite name="github.com/subchen/go-xmldom">
    <testcase name="case 1">PASS</testcase>
    <testcase name="case 2">FAIL</testcase>
  </testsuite>
</testsuites>

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)
Example
node := xmldom.Must(xmldom.ParseXML(ExampleXml)).Root
fmt.Printf("name = %v\n", node.Name)
fmt.Printf("attributes.len = %v\n", len(node.Attributes))
fmt.Printf("children.len = %v\n", len(node.Children))
fmt.Printf("root = %v", node == node.Root())
Output:

name = testsuites
attributes.len = 0
children.len = 1
root = true

func (*Document) XML

func (d *Document) XML() string
Example
doc := xmldom.Must(xmldom.ParseXML(ExampleXml))
fmt.Println(doc.XML())
Output:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE junit SYSTEM "junit-result.dtd"><testsuites><testsuite tests="2" failures="0" time="0.009" name="github.com/subchen/go-xmldom"><properties><property name="go.version">go1.8.1</property></properties><testcase classname="go-xmldom" id="ExampleParseXML" time="0.004" /><testcase classname="go-xmldom" id="ExampleParse" time="0.005" /></testsuite></testsuites>

func (*Document) XMLPretty

func (d *Document) XMLPretty() string
Example
doc := xmldom.Must(xmldom.ParseXML(ExampleXml))
fmt.Println(doc.XMLPretty())
Output:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE junit SYSTEM "junit-result.dtd">
<testsuites>
  <testsuite tests="2" failures="0" time="0.009" name="github.com/subchen/go-xmldom">
    <properties>
      <property name="go.version">go1.8.1</property>
    </properties>
    <testcase classname="go-xmldom" id="ExampleParseXML" time="0.004" />
    <testcase classname="go-xmldom" id="ExampleParse" time="0.005" />
  </testsuite>
</testsuites>

func (*Document) XMLPrettyEx added in v1.1.2

func (d *Document) XMLPrettyEx(indent string) string

type Node

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

func (*Node) AppendChild

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

func (*Node) CreateNode added in v1.1.0

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

func (*Node) FindByID

func (n *Node) FindByID(id string) *Node
Example
root := xmldom.Must(xmldom.ParseXML(ExampleXml)).Root
node := root.FindByID("ExampleParseXML")
fmt.Println(node.XML())
Output:

<testcase classname="go-xmldom" id="ExampleParseXML" time="0.004" />

func (*Node) FindByName added in v1.1.0

func (n *Node) FindByName(name string) []*Node
Example
root := xmldom.Must(xmldom.ParseXML(ExampleXml)).Root
nodes := root.FindByName("testcase")
for _, node := range nodes {
	fmt.Println(node.XML())
}
Output:

<testcase classname="go-xmldom" id="ExampleParseXML" time="0.004" />
<testcase classname="go-xmldom" id="ExampleParse" time="0.005" />

func (*Node) FindOneByName added in v1.1.0

func (n *Node) FindOneByName(name string) *Node
Example
root := xmldom.Must(xmldom.ParseXML(ExampleXml)).Root
node := root.FindOneByName("property")
fmt.Println(node.XML())
Output:

<property name="go.version">go1.8.1</property>

func (*Node) FirstChild

func (n *Node) FirstChild() *Node

func (*Node) GetAttribute

func (n *Node) GetAttribute(name string) *Attribute
Example
node := xmldom.Must(xmldom.ParseXML(ExampleXml)).Root
attr := node.FirstChild().GetAttribute("name")
fmt.Printf("%v = %v\n", attr.Name, attr.Value)
Output:

name = github.com/subchen/go-xmldom

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
Example
node := xmldom.Must(xmldom.ParseXML(ExampleXml)).Root
children := node.FirstChild().GetChildren("testcase")
for _, c := range children {
	fmt.Printf("%v: id = %v\n", c.Name, c.GetAttributeValue("id"))
}
Output:

testcase: id = ExampleParseXML
testcase: id = ExampleParse

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) Query

func (n *Node) Query(xpath string) []*Node
Example
node := xmldom.Must(xmldom.ParseXML(ExampleXml)).Root
// xpath expr: https://github.com/antchfx/xpath

// 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: id = %v\n", c.Name, c.GetAttributeValue("id"))
}
Output:

children = 5
testcase: id = ExampleParseXML
testcase: id = ExampleParse

func (*Node) QueryEach

func (n *Node) QueryEach(xpath string, cb func(int, *Node))

func (*Node) QueryOne

func (n *Node) QueryOne(xpath string) *Node
Example
node := xmldom.Must(xmldom.ParseXML(ExampleXml)).Root
// xpath expr: https://github.com/antchfx/xpath

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

testcase: id = ExampleParseXML

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

func (*Node) XMLPrettyEx added in v1.1.2

func (n *Node) XMLPrettyEx(indent string) string

Jump to

Keyboard shortcuts

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