Answer the question
In order to leave comments, you need to log in
How to parse XML to JSON?
Good afternoon, there is an XML file:
<profile>
<profile_name>Profile 1</profile_name>
<field>
<name>role</name>
<type>string</type>
<value>user</value>
</field>
<field>
<name>age</name>
<type>int</type>
<value>30</value>
</field>
</profile>
<profile>
<profile_name>Profile 2</profile_name>
<field>
<name>role</name>
<type>string</type>
<value>user</value>
</field>
<field>
<name>age</name>
<type>int</type>
<value>30</value>
</field>
</profile>
<profile>
<profile_name>Profile 3</profile_name>
<field>
<name>role</name>
<type>string</type>
<value>admin</value>
</field>
</profile>
{
"Profile 1": {
"role": "user",
"age": 30
},
"Profile 2": {
"role": "user",
"age": 30
},
"Profile 3": {
"role": "admin"
}
}
type XMLProfile struct {
XMLName xml.Name `xml:"profile,omitempty"`
ProfileName string `xml:"profile_name",omitempty`
Fields []*XMLField `xml:"field,omitempty"`
}
type XMLField struct {
Name string `xml:"name,omitempty"`
Type string `xml:"type,omitempty"`
Value string `xml:"value,omitempty"`
}
func main() {
file, err := os.Open("example.xml")
if err != nil {
log.Println(err)
}
defer file.Close()
decoder := xml.NewDecoder(file)
for {
t, _ := decoder.Token()
if t == nil {
break
}
switch et := t.(type) {
case xml.StartElement:
if et.Name.Local == "profile" {
var object XMLProfile
decoder.DecodeElement(&object, &et)
resultData := map[string]map[string]string{
object.ProfileName: map[string]string{},
}
for _, val := range object.Fields {
resultData[object.ProfileName][val.Name] = val.Value
}
if out, err := json.MarshalIndent(resultData, "", " "); err != nil {
panic(err)
} else {
_ = ioutil.WriteFile("test.json", out, 0644)
}
}
}
}
}
{
"Profile 3": {
"role": "admin"
}
}
Answer the question
In order to leave comments, you need to log in
Although a necropost, I will still give an answer
There is a typo in the type description
type XMLProfile struct {
XMLName xml.Name `xml:"profile,omitempty"`
ProfileName string `xml:"profile_name",omitempty`
Fields []*XMLField `xml:"field,omitempty"`
}
package main
import (
"encoding/json"
"encoding/xml"
"io/ioutil"
"log"
"os"
)
type XMLProfile struct {
XMLName xml.Name `xml:"profile,omitempty"`
ProfileName string `xml:"profile_name,omitempty"`
Fields []*XMLField `xml:"field,omitempty"`
}
type XMLField struct {
Name string `xml:"name,omitempty"`
Type string `xml:"type,omitempty"`
Value string `xml:"value,omitempty"`
}
func main() {
file, err := os.Open("example.xml")
if err != nil {
log.Println(err)
}
defer file.Close()
decoder := xml.NewDecoder(file)
for {
t, _ := decoder.Token()
if t == nil {
break
}
switch et := t.(type) {
case xml.StartElement:
if et.Name.Local == "profile" {
var object XMLProfile
decoder.DecodeElement(&object, &et)
resultData := map[string]map[string]string{
object.ProfileName: map[string]string{},
}
for _, val := range object.Fields {
resultData[object.ProfileName][val.Name] = val.Value
}
if out, err := json.MarshalIndent(resultData, "", " "); err != nil {
panic(err)
} else {
_ = ioutil.WriteFile("test.json", out, 0644)
}
}
}
}
}
Intrigued...
www.cihanozhan.com/converting-xml-data-to-json-wit...
or
How to parse XML in Go?
finish a little and it will be the most
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question