Go parsing XML

Source: Internet
Author: User
XML is widely used as a data exchange and information transmission format. With the increasingly wide application of Web Services, XML plays an increasingly important role in daily development. This section describes the XML-related processing packages in the go language standard package. This section does not cover content related to XML specifications (for more information, see other documents), but describes how to use the go language to codec XML files. If you are an O & M engineer, you have generated the following xml configuration file for all the servers you manage: <? XML version = "1.0" encoding = "UTF-8"?>
 
  
   
    
Shanghai_vpn
   
   
    
127.0.0.1
   
  
  
   
    
Beijing_vpn
   
   
    
127.0.0.2
   
  
 // Gotest Project main. gopackage mainimport ("encoding/XML" "FMT" "IO/ioutil" "OS") type recurlyservers struct {xmlname XML. name 'xml: "servers" 'version string' XML: "version, ATTR" 'svs [] server 'xml: "server" 'description string' XML :", innerxml "'} type server struct {xmlname XML. name 'xml: "server" 'servername string' XML: "servername" 'serverip string' XML: "serverip" '} func main () {file, err: = OS. open ("servers . Xml ") if Err! = Nil {FMT. println ("error: & V", err) return} data, err: = ioutil. readall (File) If Err! = Nil {FMT. println ("error & V", err) return} V: = recurlyservers {} err = xml. unmarshal (data, & V) If Err! = Nil {FMT. println ("error & V", err) return} FMT. println (v. xmlname) FMT. println (v. version) FMT. println ("--------------------") FMT. println (v. SVS [0]. servername) FMT. println (v. SVS [0]. serverip) FMT. println ("--------------------") FMT. println (v. SVS [1]. servername) FMT. println (v. SVS [1]. serverip )}

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.