123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- package main
- import (
- "encoding/xml"
- "flag"
- "fmt"
- "io/ioutil"
- "os"
- )
- type Questionnaire struct {
- XMLName xml.Name `xml:"questionnaire"`
- Section []struct {
- Question []struct {
- Text string `xml:"text"`
- Response struct {
- Fixed struct {
- Category []struct {
- Label string `xml:"label"`
- } `xml:"category"`
- } `xml:"fixed"`
- } `xml:"response"`
- } `xml:"question"`
- } `xml:"section"`
- }
- func main() {
- flag.Parse()
- if len(flag.Args()) == 0 {
- panic("A filename should be provided")
- }
- xmlFile, err := os.Open(flag.Arg(0))
- if err != nil {
- fmt.Println(err)
- return
- }
- defer xmlFile.Close()
- byteValue, _ := ioutil.ReadAll(xmlFile)
- var questionnaire Questionnaire
- xml.Unmarshal(byteValue, &questionnaire)
- for i, section := range questionnaire.Section {
- for j, question := range section.Question {
- mdContent := question.Text + "\n\n"
- for _, answer := range question.Response.Fixed.Category {
- mdContent += "* " + answer.Label + "\n"
- }
- mdFileName := fmt.Sprintf("question_%d_%d.md", i+1, j+1)
- ioutil.WriteFile(mdFileName, []byte(mdContent), 0644)
- }
- }
- }
|