Golang : Search and extract certain XML data example
There are times when you want to just search for certain element in huge XML data set and extract the element name and associated value(chardata) or attribute value.
In this tutorial, we will learn how to extract the "normal" type of XML data. Just by the element name and get the chardata value(text in between the element name). Then, followed by extraction example of XML attribute data.
Here you 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>`
var XMLdata2 = `<ALEXA VER="0.9" URL="socketloop.com/" HOME="0" AID="=" IDN="socketloop.com/">
<SD>
<POPULARITY URL="socketloop.com/" TEXT="291466" SOURCE="panel"/>
<REACH RANK="237320"/>
<RANK DELTA="+31735"/>
<COUNTRY CODE="US" NAME="United States" RANK="191742"/>
</SD>
</ALEXA>`
// 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)
}
}
}
fmt.Println("------------------------------------")
// example on handling XML attribute
decoder = xml.NewDecoder(strings.NewReader(string(XMLdata2)))
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 == "REACH" {
fmt.Println("Element name is : ", Element.Name.Local)
attrName := Element.Attr[0].Name.Local
attrValue := Element.Attr[0].Value
fmt.Printf("Attribute name is [%s] and value is [%s] \n", attrName, attrValue)
}
}
}
}
http://play.golang.org/p/b5RpnKGwsV
References :
See also : Golang : delete and modify XML file content
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
+8.2k Golang : Implementing class(object-oriented programming style)
+10.8k Golang : Sieve of Eratosthenes algorithm
+15.6k Golang : Get current time from the Internet time server(ntp) example
+8.6k Golang : Executing and evaluating nested loop in html template
+9.5k Golang : Copy map(hash table) example
+9.3k Golang : Web(Javascript) to server-side websocket example
+8.7k Golang : Accept any number of function arguments with three dots(...)
+9.5k Golang : Validate IPv6 example
+6.7k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?
+11.2k Golang : How to flush a channel before the end of program?
+10k Golang : Print how to use flag for your application example
+16.3k Golang : Get IP addresses of a domain name