Skip to content

Instantly share code, notes, and snippets.

@nqthqn
Created July 11, 2020 17:04
Show Gist options
  • Save nqthqn/6104762e5c2925aef1f2fd17f288915a to your computer and use it in GitHub Desktop.
Save nqthqn/6104762e5c2925aef1f2fd17f288915a to your computer and use it in GitHub Desktop.
Convert XML file to JSON file
package main
import (
"bufio"
"io/ioutil"
"os"
xj "github.com/basgys/goxml2json"
)
func main() {
// Open the file from the first argument, ignore any errors
// Open func(name string) (*File, error)
f, _ := os.Open(os.Args[1])
// Create a pointer to a "Reader" that we will give to the library
// NewReader func(rd io.Reader) *Reader
xml := bufio.NewReader(f)
// Convert the data, return a pointer to a buffer of the data, ignore any errors
// Convert func(r io.Reader, ps ...plugin) (*bytes.Buffer, error)
json, _ := xj.Convert(xml)
// Use the 2nd argument as the file path for writing
// Convert the json pointer to Bytes
// to with octal value permissions 0644 which represents
// http://www.filepermissions.com/file-permission/0644
/*
Owner Group World
Read * * *
Write * x x
Execute x x x
O G W
- rw- r-- r--
4+2+0 4+0+0 4+0+0
6 4 4
*/
// WriteFile func(filename string, data []byte, perm os.FileMode) error
ioutil.WriteFile(os.Args[2], json.Bytes(), 0644)
// https://gobyexample.com/ for helpful examples
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment