Golang : Read XML elements data with xml.CharData example
For this post, we will learn how to read inner XML elements data with xml.CharData
type. This tutorial will show you how to handle xml.CharData
variable as an array of bytes and then to convert to a string for display.
Here we go!
package main
import (
"encoding/xml"
"fmt"
"strings"
)
var XMLdata = `<urlset>
<url>
<loc>http://www.example.com/xml-element-golang</loc>
<lastmod>2015-06-14</lastmod>
<changefreq>daily</changefreq>
<priority>0.5</priority>
</url>
<url>
<loc>http://www.example.com/extract-element-by-for-loop</loc>
<lastmod>2015-06-14</lastmod>
<changefreq>daily</changefreq>
<priority>0.5</priority>
</url>
<urlset>`
// ignore <loc>, only use chardata because DecodeElement will work on <loc>
type XMLQuery struct {
Loc string `xml:",chardata"`
}
var l XMLQuery
func main() {
// example on handling XML chardata(string)
decoder := xml.NewDecoder(strings.NewReader(string(XMLdata)))
for {
// err is ignore here. IF you are reading from a XML file
// do not ignore err and also check for io.EOF
token, _ := decoder.Token()
if token == nil {
break
}
switch Element := token.(type) {
case xml.StartElement:
if Element.Name.Local == "loc" {
fmt.Println("Element name is : ", Element.Name.Local)
err := decoder.DecodeElement(&l, &Element)
if err != nil {
fmt.Println(err)
}
fmt.Println("Element value is : ", l.Loc)
}
// print out the element data
// convert to []byte slice and cast to string type
case xml.CharData:
str := string([]byte(Element))
fmt.Println(str)
}
}
}
Sample output :
Element name is : loc
Element value is : http://www.example.com/xml-element-golang
2015-06-14
daily
0.5
Element name is : loc
Element value is : http://www.example.com/extract-element-by-for-loop
2015-06-14
daily
0.5
Happy coding!
References :
https://www.socketloop.com/references/golang-encoding-xml-chardata-type-examples
https://www.socketloop.com/tutorials/golang-search-and-extract-certain-xml-data-example
See also : Golang : Search and extract certain XML data example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.9k Golang : Channels and buffered channels examples
+9.3k Golang : Play .WAV file from command line
+3.3k Golang : Fix go-cron set time not working issue
+5.2k Golang : Get FX sentiment from website example
+10.2k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+9.2k Golang : Apply Histogram Equalization to color images
+15.5k Golang : Convert date format and separator yyyy-mm-dd to dd-mm-yyyy
+8.1k Golang : Get final or effective URL with Request.URL example
+16.7k Golang : Set up source IP address before making HTTP request
+21.3k Golang : How to read float value from standard input ?
+10.2k Golang : Embed secret text string into binary(executable) file
+5.9k PHP : How to check if an array is empty ?