xml转json
package main
import (
"encoding/json"
"encoding/xml"
"fmt"
)
type DataFormat struct {
ProductList []struct {
Sku string `xml:"sku" json:"sku"`
Quantity int `xml:"quantity" json:"quantity"`
} `xml:"Product" json:"products"`
}
func main() {
xmlData := []byte(`<?xml version="1.0" encoding="UTF-8" ?>
<ProductList>
<Product>
<sku>ABC123</sku>
<quantity>2</quantity>
</Product>
<Product>
<sku>ABC123</sku>
<quantity>2</quantity>
</Product>
</ProductList>`)
data := &DataFormat{}
err := xml.Unmarshal(xmlData, data)
if nil != err {
fmt.Println("Error unmarshalling from XML", err)
return
}
result, err := json.Marshal(data)
if nil != err {
fmt.Println("Error marshalling to JSON", err)
return
}
fmt.Printf("%s\n", result)
}
xml文件转json文件
Employees.xml
<?xml version="1.0"?>
<company>
<staff>
<id>101</id>
<firstname>Derek</firstname>
<lastname>Young</lastname>
<username>derekyoung</username>
</staff>
<staff>
<id>102</id>
<firstname>John</firstname>
<lastname>Smith</lastname>
<username>johnsmith</username>
</staff>
</company>
代码:
package main
import (
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"os"
)
type jsonStaff struct {
ID int
FirstName string
LastName string
UserName string
}
type Staff struct {
XMLName xml.Name `xml:"staff"`
ID int `xml:"id"`
FirstName string `xml:"firstname"`
LastName string `xml:"lastname"`
UserName string `xml:"username"`
}
type Company struct {
XMLName xml.Name `xml:"company"`
Staffs []Staff `xml:"staff"`
}
func (s Staff) String() string {
return fmt.Sprintf("\t ID : %d - FirstName : %s - LastName : %s - UserName : %s \n", s.ID, s.FirstName, s.LastName, s.UserName)
}
func main() {
xmlFile, err := os.Open("Employees.xml")
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer xmlFile.Close()
XMLdata, _ := ioutil.ReadAll(xmlFile)
var c Company
xml.Unmarshal(XMLdata, &c)
// sanity check - XML level
fmt.Println(c.Staffs)
// convert to JSON
var oneStaff jsonStaff
var allStaffs []jsonStaff
for _, value := range c.Staffs {
oneStaff.ID = value.ID
oneStaff.FirstName = value.FirstName
oneStaff.LastName = value.LastName
oneStaff.UserName = value.UserName
allStaffs = append(allStaffs, oneStaff)
}
jsonData, err := json.Marshal(allStaffs)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// sanity check - JSON level
fmt.Println(string(jsonData))
// now write to JSON file
jsonFile, err := os.Create("./Employees.json")
if err != nil {
fmt.Println(err)
}
defer jsonFile.Close()
jsonFile.Write(jsonData)
jsonFile.Close()
}
生成了文件Employees.json :
[
{
"ID": 101,
"FirstName": "Derek",
"LastName": "Young",
"UserName": "derekyoung"
},
{
"ID": 102,
"FirstName": "John",
"LastName": "Smith",
"UserName": "johnsmith"
}
]
json转xml
type Person struct {
Id int `xml:"id,attr"`
FirstName string `xml:"name>first" json:"first"`
LastName string `xml:"name>last" json:"last"`
}
func main() {
j := `{"id": 10, "first": "firstname", "last":"lastname"}`
var p Person
fmt.Println(json.Unmarshal([]byte(j), &p), p)
out, _ := xml.MarshalIndent(p, "\t", "\t")
fmt.Println(string(out))
}