Go parsing xml

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
XML is widely used as a format for data exchange and information transmission. With the increasing application of Web services, XML is now playing an increasingly important role in the daily development work. In this section, we will describe the packages of XML related processing in the Go Language standard pack. This section does not cover XML specification-related content (refer to other literature for more information), but rather describes how to encode XML file-related knowledge in the Go language. If you are an OPS person, you generate the following XML configuration file for all the servers you administer: <?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 xm L.name ' xml: "Servers" ' Version string ' xml: "version,attr" ' Svs []server ' xml: ' Server ' ' Description string ' XM L: ", 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)}

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.