Hello StackOverFLowers!
I'm trying to extract the xml from the following.....
Code:
package main
import (
"fmt"
"encoding/xml"
"net/http"
"log"
"io/ioutil"
"encoding/json"
)
type reportType struct{
Course xml.CharData `xml:"course"`
Crn xml.CharData `xml:"crn"`
Id xml.CharData `xml:"course>id"`
Section xml.CharData `xml:"course>section`
Title xml.CharData `xml:"course>title`
}
type myErrorType struct{
TypeOfError xml.CharData `xml:"type"`
Desciption xml.CharData `xml:"description"`
}
type reportTypeJson struct{
Course string `json:"course"`
Crn string `json:"crn"`
Id string `json:"id"`
Section string `json:"section`
Title string `json:"title`
course map[string]string `json:"course"`;
}
func main() {
baseURL := "http://turing.cs.missouriwestern.edu/classes/csc346/final/?crn=13398";
var query string
query = baseURL
fmt.Println("The escaped query: "+query)
response, err := http.Get(query)
doErr(err, "After the GET")
var body []byte
body, err = ioutil.ReadAll(response.Body)
fmt.Println(body);
doErr(err, "After Readall")
stringOfBody := string(body[:500])
fmt.Printf("TASK 1: The body(as text): %s
",stringOfBody)
//Unmarshalling
var report reportType
xml.Unmarshal(body, &report)
fmt.Printf("The Report: %s
", report)
//Now marshal the data out in JSON
var data []byte
var output reportTypeJson
output.Course=string(report.Course)
output.Crn=string(report.Crn)
output.Id=string(report.Id)
output.Section=string(report.Section)
output.Title=string(report.Title)
//var output reportType
//output.Version = string(report.Version);
//report.Version -> output.Version
//output.TermsOfService = string(report.TermsOfService)
data,err = json.MarshalIndent(output,""," ")
doErr(err, "From marshalIndent")
fmt.Printf("JSON output nicely formatted:
%s
",data)
fmt.Println("CRN: %v
",report.Crn)
fmt.Println("ID: %v
",report.Id)
fmt.Println("Section: %v
",report.Section)
fmt.Println("Title: %v
",report.Title)
fmt.Println("Course: %v
",report.Course)
fmt.Println("ID: %v
",report.Id)
}
func doErr( err error, message string){
if err != nil{
log.Panicf("ERROR: %s %s
", message, err.Error())
}
}
For Some reason it's not extracting the data from the file giving me basically a blank report. I made a program like this literally two days ago and am following the same format, but I can't figure out where I'm going wrong.
OUTPUT:
The Report: { }
JSON output nicely formatted:
{
"course": "",
"crn": "",
"id": "",
"Section": "",
"Title": ""
}
Any and all help is much appreciated, I have a test in a few hours and I'm hoping for some guidance by then!! Thanks!
Change your xml attributes on reportType to remove the root element: xml:"course>crn"
should be xml:"crn"
. If you aren't required to use CharData, I would also simplify your data types to strings so you aren't converting them later on. See my example playground for a working example.