Documentation ¶
Overview ¶
XML DOM processing for Golang, supports xpath query
Index ¶
- Constants
- type Attribute
- type Document
- type Node
- func (n *Node) AppendChild(c *Node) *Node
- func (n *Node) CreateNode(name string) *Node
- func (n *Node) FindByID(id string) *Node
- func (n *Node) FindByName(name string) []*Node
- func (n *Node) FindOneByName(name string) *Node
- func (n *Node) FirstChild() *Node
- func (n *Node) GetAttribute(name string) *Attribute
- func (n *Node) GetAttributeValue(name string) string
- func (n *Node) GetChild(name string) *Node
- func (n *Node) GetChildren(name string) []*Node
- func (n *Node) LastChild() *Node
- func (n *Node) NextSibling() *Node
- func (n *Node) PrevSibling() *Node
- func (n *Node) Query(xpath string) []*Node
- func (n *Node) QueryEach(xpath string, cb func(int, *Node))
- func (n *Node) QueryOne(xpath string) *Node
- func (n *Node) RemoveAttribute(name string) *Node
- func (n *Node) RemoveChild(c *Node) *Node
- func (n *Node) Root() *Node
- func (n *Node) SetAttributeValue(name string, value string) *Node
- func (n *Node) XML() string
- func (n *Node) XMLPretty() string
- func (n *Node) XMLPrettyEx(indent string) string
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 Document ¶
func NewDocument ¶
Example ¶
doc := xmldom.NewDocument("testsuites") testsuiteNode := doc.Root.CreateNode("testsuite").SetAttributeValue("name", "github.com/MrSplidge/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/MrSplidge/go-xmldom"> <testcase name="case 1">PASS</testcase> <testcase name="case 2">FAIL</testcase> </testsuite> </testsuites>
func ParseXML ¶
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 ¶
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/MrSplidge/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" /><testcase xmlns:test="mock" id="AttrNamespace" /></testsuite></testsuites>
func (*Document) XMLPretty ¶
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/MrSplidge/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" /> <testcase xmlns:test="mock" id="AttrNamespace" /> </testsuite> </testsuites>
func (*Document) XMLPrettyEx ¶ added in v1.1.3
type Node ¶
type Node struct { Document *Document Parent *Node Name string Attributes []*Attribute Children []*Node Text string }
func (*Node) AppendChild ¶
func (*Node) CreateNode ¶ added in v1.1.0
func (*Node) FindByID ¶
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
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" /> <testcase xmlns:test="mock" id="AttrNamespace" />
func (*Node) FindOneByName ¶ added in v1.1.0
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 (*Node) GetAttribute ¶
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/MrSplidge/go-xmldom
func (*Node) GetAttributeValue ¶
func (*Node) GetChildren ¶
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) NextSibling ¶
func (*Node) PrevSibling ¶
func (*Node) Query ¶
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) QueryOne ¶
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 (*Node) RemoveChild ¶
func (*Node) XMLPrettyEx ¶ added in v1.1.3
Click to show internal directories.
Click to hide internal directories.