1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
// https://groups.google.com/forum/?fromgroups#!topic/golang-nuts/2A6_YRYXCjA
package main
import (
"fmt"
"github.com/clbanning/mxj/v2"
)
var data = []byte(`<root>
<!-- first child1 element -->
<child1>
<option1 />
</child1>
<!-- follows first child2 element -->
<child2>
<option2 />
</child2>
<!-- there can be multiple child1 elements -->
<child1>
<option1 />
</child1>
<child1>
<option1 />
</child1>
<!-- followed by child2 element -->
<child2>
<option2 />
</child2>
<!-- followed by child1 element -->
<child1>
<option1 />
</child1>
<!-- there can be multiple child2 elements -->
<child2>
<option2 />
</child2>
<child2>
<option2 />
</child2>
</root>`)
func main() {
m, err := mxj.NewMapXml(data)
if err != nil {
fmt.Println("err:", err)
}
fmt.Println(m.StringIndentNoTypeInfo())
doc, err := m.XmlIndent("", " ")
if err != nil {
fmt.Println("err:", err)
}
fmt.Println(string(doc))
val, err := m.ValuesForKey("child1")
if err != nil {
fmt.Println("err:", err)
}
fmt.Println("val:", val)
mxj.XmlGoEmptyElemSyntax()
doc, err = mxj.AnyXmlIndent(val, "", " ", "child1")
if err != nil {
fmt.Println("err:", err)
}
fmt.Println(string(doc))
}
|