xmldom

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

README

xmldom-parser

It is a fork from subchen/go-xmldom.

Motivation for fork

Since the original repo seems to be dead, this repo is made to add the updates needed for my needs. Many thanks for subchen's work! Original Repo

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/Rodion-Bozhenko/xmldom-parser

Basic Usage

xml := `<testsuite tests="2" failures="0" time="0.009" name="github.com/Rodion-Bozhenko/xmldom-parser">
    <testcase classname="xmldom-parser" name="ExampleParseXML" time="0.004"></testcase>
    <testcase classname="xmldom-parser" 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/Rodion-Bozhenko/xmldom-parser")
suiteNode.CreateNode("testcase").SetAttributeValue("name", "case 1")
suiteNode.CreateNode("testcase").SetAttributeValue("name", "case 2")

fmt.Println(doc.XML())

License

xmldom-parser 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/xmldom-parser")
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/xmldom-parser">
    <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/xmldom-parser"><properties><property name="go.version">go1.8.1</property></properties><testcase classname="xmldom-parser" id="ExampleParseXML" time="0.004" /><testcase classname="xmldom-parser" id="ExampleParse" time="0.005" /><testcase xmlns:test="mock" id="AttrNamespace" /></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/xmldom-parser">
    <properties>
      <property name="go.version">go1.8.1</property>
    </properties>
    <testcase classname="xmldom-parser" id="ExampleParseXML" time="0.004" />
    <testcase classname="xmldom-parser" id="ExampleParse" time="0.005" />
    <testcase xmlns:test="mock" id="AttrNamespace" />
  </testsuite>
</testsuites>

func (*Document) XMLPrettyEx

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

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="xmldom-parser" id="ExampleParseXML" time="0.004" />

func (*Node) FindByName

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="xmldom-parser" id="ExampleParseXML" time="0.004" />
<testcase classname="xmldom-parser" id="ExampleParse" time="0.005" />
<testcase xmlns:test="mock" id="AttrNamespace" />

func (*Node) FindOneByName

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/xmldom-parser

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
testcase: id = AttrNamespace

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 = 6
testcase: id = ExampleParseXML
testcase: id = ExampleParse
testcase: id = AttrNamespace

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

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