Parsing XML using the Go language

Source: Internet
Author: User
Tags xml attribute

Operating system: CentOS 6.9_x64

Go language version: 1.8.3

Problem description

An existing automatic reporting program, if the service error will automatically send the message to the designated person, the configuration file content is as follows (Default.xml):

<?XML version= "1.0" encoding= "UTF-8"?><Config>    <SmtpServer>Smtp.163.com</SmtpServer>    <Smtpport>25</Smtpport>    <Sender>[Email protected]</Sender>    <senderpasswd>123456</senderpasswd>    <ReceiversFlag= "true">        <User>[Email protected]</User>        <User>[Email protected]</User>    </Receivers></Config>

The configuration is configured with Config as the root tag, with the XML text part (such as the smtpserver tag), the nested XML (receivers tag), the XML attribute section (flag of the receivers tag), Array-like multi-line configuration (user tag) with data types in both string and number types.

Solution Solutions
Package Mainimport ("Encoding/xml"    "FMT"    "Io/ioutil"    "OS") Type Sconfigstruct{xmlname XML. Name ' xml:"Config"`//specifies that the outermost label is configSmtpServerstring' XML:"SmtpServer"`//reads the SmtpServer configuration item and saves the result to the SmtpServer variableSmtpportint' XML:"Smtpport"' Senderstring' XML:"Sender"' senderpasswdstring' XML:"senderpasswd"' receivers sreceivers ' xml:"Receivers"`//read the contents of the receivers tag and get it in a structured way}type sreceiversstruct{Flagstring' XML:"flag,attr"`//Read Flag PropertyUser []string' XML:"User"`//reading the user array}func Main () {file, err:= OS. Open ("Default.xml")//For Read access.    ifErr! =Nil {fmt. Printf ("Error:%v", Err)return} defer file. Close () data, err:=Ioutil. ReadAll (file)ifErr! =Nil {fmt. Printf ("Error:%v", Err)return} V:=sconfig{} Err= XML. Unmarshal (Data, &v)ifErr! =Nil {fmt. Printf ("Error:%v", Err)return} fmt. Println (v) fmt. Println ("SmtpServer:", V.smtpserver) fmt. Println ("Smtpport:", V.smtpport) fmt. Println ("Sender:", V.sender) fmt. Println ("senderpasswd:", V.SENDERPASSWD) fmt. Println ("Receivers.flag:", V.receivers.flag) forI,element: =range V.receivers.user {fmt. Println (I,element)}}

Operating effect:

[Email protected] t1]#lsdefault.xml xmlcnftest1.go[[email protected] t1]# go run xmlcnftest1.go{{config} smtp.163. com -[Email protected]163. com123456{true[ Email protected] [email protected]}} Smtpserver:smtp.163. Comsmtpport: -Sender: [email protected]163. COMSENDERPASSWD:123456Receivers.flag:true0[email protected]1[email protected][[email protected] t1]#
Discuss

If you need to parse the XML configuration directly from a string, you can replace data in the following statement:

Err = XML. Unmarshal (data, &v)

Like what:

Err = XML. Unmarshal ([]byte//  configcontent as XML string

All right, that's it, I hope it helps you.

This article GitHub address:

https://github.com/mike-zhang/mikeBlogEssays/blob/master/2017/20170610_ using go parsing xml.rst

Welcome to Supplement

Parsing XML using the Go language

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.