xmldom

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2017 License: Apache-2.0 Imports: 7 Imported by: 6

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

testsuiteNode := xmldom.NewNode("testsuite").SetAttributeValue("name", "github.com/subchen/go-xmldom")
doc.Root.AppendChild(testsuiteNode)

caseNode1 := xmldom.NewTextNode("testcase", "PASS")
caseNode2 := xmldom.NewTextNode("testcase", "PASS")
testsuiteNode.AppendChild(caseNode1).AppendChild(caseNode2)

fmt.Println(doc.XML())

License

Apache 2

Documentation

Overview

XML DOM processing for Golang, supports xpath query

Index

Examples

Constants

This section is empty.

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
	Root     *Node
}

func Must

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

func NewDocument

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

testsuiteNode := xmldom.NewNode("testsuite").SetAttributeValue("name", "github.com/subchen/go-xmldom")
doc.Root.AppendChild(testsuiteNode)

caseNode1 := xmldom.NewTextNode("testcase", "PASS")
caseNode2 := xmldom.NewTextNode("testcase", "PASS")
testsuiteNode.AppendChild(caseNode1).AppendChild(caseNode2)

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

<?xml version="1.0" encoding="UTF-8"?><testsuites><testsuite name="github.com/subchen/go-xmldom"><testcase>PASS</testcase><testcase>PASS</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"?><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" name="ExampleParseXML" time="0.004" /><testcase classname="go-xmldom" name="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"?>
<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" name="ExampleParseXML" time="0.004" />
    <testcase classname="go-xmldom" name="ExampleParse" time="0.005" />
  </testsuite>
</testsuites>

type Node

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

func NewNode

func NewNode(nodeName string) *Node

func NewTextNode

func NewTextNode(nodeName string, text string) *Node

func (*Node) AppendChild

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

func (*Node) FindByID

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

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

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

children = 5
testcase: name = ExampleParseXML
testcase: name = 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[@name='ExampleParseXML']")
fmt.Printf("%v: name = %v\n", c.Name, c.GetAttributeValue("name"))
Output:

testcase: name = 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

Jump to

Keyboard shortcuts

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