Go Learning notes: The use of XML libraries

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Package Mainimport ("OS" "IO" "FMT" "Encoding/xml" "StrConv") type Config Map[string]map[string]stringvar C Onfig = Make (Config) type Class struct {xmlname XML.    Name ' xml: ' class ' '//xml element name Id int ' xml: ' id,attr ' '///Pers []persion ' xml: ' persion ' '// Desc string ' xml: ', InnerXml '//}type persion struct {xmlname XML. Name ' xml: ' persion ' ' Id int ' xml: ' id,attr ' ' name string ' xml: ' name ' ' Age int ' xml: ' Age "'}func (self *config) loadxmlfile (filename, node string) error{fd, err: = OS. Open (filename) If err! = nil{return err} defer FD. Close () return self. Load (FD, node)}func (self *config) load (r io. Reader, node String) error {Mynode: = False If _, OK: = (*self) [node];!ok {(*self) [node] = make (map[string ]string)} Decoder: = XML. Newdecoder (r) for {token, err: = decoder. Token () if err! = Nil {if Err.   Error () = = "EOF" {             Break} return Err} switch Value: = token. (type) {Case XML.} Startelement:switch {case value. Name.local = = Node:mynode = True case Mynode = = TRUE:TB , err: = decoder.                        Token () if err! = Nil {Continue} Switch TV: = TB. (type) {Case XML.} Chardata: (*self) [Node][value. Name.local] = string (TV)}} case XML. Endelement:if value. name.local = = node{Mynode = False}}} return Nil}func main () {var ps = Make ([]persion, 0) for I: = 0; I < 5; i++ {var TP persion TP. Id = i tp. Name = "Persion" + StrConv. Itoa (i) TP. Age = + I PS = AppEnd (PS,TP)} var c = class{id:1, pers:ps} var res, _ = XML. Marshalindent (C, "", "") fmt. Println (c) fmt. Println (String (res)) config. Loadxmlfile ("config +", "global") fmt. Println (config)}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.