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.