Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
Problem :
There are times when we want to produce XML files that are easier to read by human eyes and you happen to have a Golang program that produce XML data that look like this :
<person>
<id>13</id>
<name>
<first>John</first>
<last>Doe</last>
</name>
<age>42</age>
<Married>false</Married>
<City>Hanga Roa</City>
<State>Easter Island</State>
<!-- Need more details. -->
</person>
To make it more readable, you want to make the field ID as a XML attribute of the Person instead of separate tag. Or you want to make the XML data to become more readable such as :
<HostProperties>
<tag name="HOST_END">Thu Feb 20 12:38:24 2014</tag>
<tag name="patch-summary-total-cves">4</tag>
<tag name="host-fqdn">foobar.com</tag>
<tag name="HOST_START">Thu Feb 20 12:30:14 2014</tag>
</HostProperties>
with the XML attribute name
to differentiate each tags
How to do that ?
Solution :
Add the attr
tag into Person structure. For example :
type Person struct {
XMLName xml.Name `xml:"person"`
Id int `xml:"id,attr"` //<--- add attr and see what will happen
FirstName string `xml:"name>first"`
LastName string `xml:"name>last"`
Age int `xml:"age"`
Height float32 `xml:"height,omitempty"`
Married bool
Address
Comment string `xml:",comment"`
}
the output will become :
<person id="13">
<name>
<first>John</first>
<last>Doe</last>
</name>
<age>42</age>
<Married>false</Married>
<City>Hanga Roa</City>
<State>Easter Island</State>
<!-- Need more details. -->
</person>
The complete code :
package main
import (
"encoding/xml"
"fmt"
)
type Address struct {
City, State string
}
type Person struct {
XMLName xml.Name `xml:"person"`
Id int `xml:"id,attr"` //<--- remove attr and see what will happen
FirstName string `xml:"name>first"`
LastName string `xml:"name>last"`
Age int `xml:"age"`
Height float32 `xml:"height,omitempty"`
Married bool
Address
Comment string `xml:",comment"`
}
func main() {
v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}
v.Comment = " Need more details. "
v.Address = Address{"Hanga Roa", "Easter Island"}
output, err := xml.MarshalIndent(v, " ", " ")
if err != nil {
fmt.Printf("error: %v\n", err)
}
fmt.Println(string(output))
}
Play at : http://play.golang.org/p/mioNj3kht-
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
+11.6k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter
+28.3k Golang : Record voice(audio) from microphone to .WAV file
+8.6k Golang : Get curl -I or head data from URL example
+8.5k Golang : HTTP Routing with Goji example
+20.2k Golang : Underscore or snake_case to camel case example
+9.8k Golang : How to profile or log time spend on execution?
+14k Golang : Parsing or breaking down URL
+42.5k Golang : Get hardware information such as disk, memory and CPU usage
+15k Golang : rune literal not terminated error
+20k Golang : Pipe output from one os.Exec(shell command) to another command
+13.3k Golang : How to determine if a year is leap year?
+5.4k Golang : Fix opencv.LoadHaarClassifierCascade The node does not represent a user object error