Read and generate XML in Go language

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

The XML to read is as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><resources><string name= "videoloading" >loading video...</ String><string name= "ApplicationName" >what</string></resources>

This structure of XML, its stub point is the property and text coexist (most of the tutorials on the Web are attribute nodes are attribute nodes, text nodes are text nodes)

Based on this XML, the corresponding struct is generated

Type stringresources struct {xmlname        XML. Name         ' xml: ' Resources ' ' resourcestring []resourcestring ' xml: ' string ' '}type resourcestring struct {xmlname    Xml. Name ' xml: ' String ' ' Stringname string '   xml: ' name,attr ' ' InnerText  string   ' xml: ', InnerXml ' '}

(internal attributes are capitalized, the name of the attribute node is fixed to XMLName, and the internal text is called InnerXml)

The following is a complete reading of this XML code

Package Mainimport ("Encoding/xml" "Io/ioutil" "Log") type stringresources struct {xmlname        XML. Name         ' xml: ' Resources ' ' resourcestring []resourcestring ' xml: ' string ' '}type resourcestring struct {xmlname    Xml. Name ' xml: ' String ' ' Stringname string '   xml: ' name,attr ' ' InnerText  string   ' xml: ', InnerXml ' '}func main () { Content, Err: = Ioutil. ReadFile ("Studygolang.xml") if err! = Nil {log. Fatal (ERR)}var result stringresourceserr = XML. Unmarshal (content, &result) if err! = Nil {log. Fatal (Err)}log. PRINTLN (Result) log. Println (result. Resourcestring) For _, O: = range result. resourcestring {log. Println (o.stringname + "= = =" + O.innertext)}}
Output results

Next, the attribute value in this XML is changed to the text content of ApplicationName, and then saved to the file, the code is as follows:

Package Mainimport ("Encoding/xml" "FMT" "Io/ioutil" "Log" "OS" "strings") type stringresources struct {xmlname XML. Name ' xml: ' Resources ' ' resourcestring []resourcestring ' xml: ' string ' '}type resourcestring struct {xmlname XML.  Name ' xml: ' String ' ' Stringname string ' xml: ' name,attr ' ' InnerText string ' xml: ', InnerXml ' '}func main () {content, err: = Ioutil. ReadFile ("Studygolang.xml") if err! = Nil {log. Fatal (ERR)}var result stringresourceserr = XML. Unmarshal (content, &result) if err! = Nil {log. Fatal (Err)}log. PRINTLN (Result) log. Println (result. resourcestring) for I, line: = range result. resourcestring {log. Println (line. Stringname + "= = =" + line. InnerText)//Modify the internal text of the applicationname node innertextif strings. Equalfold (line. Stringname, "ApplicationName") {FMT. Println ("Change InnerText")//Pay attention to modifying not the line object, but directly using the real object result in result. Resourcestring[i]. InnerText = "This is the new applicationname"}}//Save the modified content XMLOutput, Outputerr: = XML. Marshalindent (Result, "", "") if Outputerr = = Nil {//Join XML header Headerbytes: =[]byte (XML. header)//splicing XML header and actual XML content Xmloutputdata: = Append (Headerbytes, xmloutput ...) Write to file Ioutil. WriteFile ("Studygolang_test.xml", Xmloutputdata, OS. Modeappend) fmt. Println ("ok~")} else {fmt. Println (Outputerr)}}

Compile run


The resulting XML is as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><resources><string name= "videoloading" >loading video...</ String><string name= "ApplicationName" > This is the new applicationname</string></resources>

More complex XML structure, not much to say here. The structure of a struct is mostly complex.

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.