...

Source file src/github.com/clbanning/mxj/v2/examples/gonuts9.go

Documentation: github.com/clbanning/mxj/v2/examples

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

View as plain text