main.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package main
  2. import (
  3. "encoding/xml"
  4. "flag"
  5. "fmt"
  6. "io/ioutil"
  7. "os"
  8. )
  9. type Questionnaire struct {
  10. XMLName xml.Name `xml:"questionnaire"`
  11. Section []struct {
  12. Question []struct {
  13. Text string `xml:"text"`
  14. Response struct {
  15. Fixed struct {
  16. Category []struct {
  17. Label string `xml:"label"`
  18. } `xml:"category"`
  19. } `xml:"fixed"`
  20. } `xml:"response"`
  21. } `xml:"question"`
  22. } `xml:"section"`
  23. }
  24. func main() {
  25. flag.Parse()
  26. if len(flag.Args()) == 0 {
  27. panic("A filename should be provided")
  28. }
  29. xmlFile, err := os.Open(flag.Arg(0))
  30. if err != nil {
  31. fmt.Println(err)
  32. return
  33. }
  34. defer xmlFile.Close()
  35. byteValue, _ := ioutil.ReadAll(xmlFile)
  36. var questionnaire Questionnaire
  37. xml.Unmarshal(byteValue, &questionnaire)
  38. for i, section := range questionnaire.Section {
  39. for j, question := range section.Question {
  40. mdContent := question.Text + "\n\n"
  41. for _, answer := range question.Response.Fixed.Category {
  42. mdContent += "* " + answer.Label + "\n"
  43. }
  44. mdFileName := fmt.Sprintf("question_%d_%d.md", i+1, j+1)
  45. ioutil.WriteFile(mdFileName, []byte(mdContent), 0644)
  46. }
  47. }
  48. }