simplexml
![GoDoc](http://godoc.org/github.com/kylewolfe/simplexml?status.svg)
simplexml provides a simple API to read, create and manipulate XML documents at run time in pure Go.
Stability
simplxml underwent a major refactor for v0.1 in order to address comment support and a few other annoyances I had with the API. simplexml is now entering a more stable state as of v0.1. While trunk is not guaranteed to be clear of breaking changes, they will be well documented moving forward, and tags will be available of older releases. Please remember to vendor your dependencies :)
Usage
From Scratch
root := NewTag("root")
doc := NewDocument(root)
doc.AddBefore(NewComment("simplexml has support for comments outside of the root document"), root)
root.AddAfter(NewTag("foo"), nil)
root.AddBefore(NewTag("bar"), nil)
bat := NewTag("bat")
bat.AddAfter(NewValue("bat value"), nil)
root.AddAfter(bat, nil)
b, err := doc.Marshal()
if err != nil {
panic(err)
}
fmt.Println(string(b))
From A Reader
xmlString := `<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!-- comment above root element -->
<root>
<!-- <comment>above foo</comment> -->
<foo>
<bar>bat</bar>
<baz/>
<fizz><![CDATA[<foo>contents</foo>]]></fizz>
</foo>
</root>
<!-- comment below root element -->`
doc, err := NewDocumentFromReader(strings.NewReader(xmlString))
if err != nil {
panic(err)
}
fizz := doc.Root().Search().ByName("foo").ByName("fizz").One()
if fizz == nil {
panic("fizz is missing")
}
fv, err := fizz.Value()
if err != nil {
panic(err)
}
fmt.Println("fizz: ", fv)