【问题标题】:GOLang: Extracting XML IssueGOlang:提取 XML 问题
【发布时间】:2016-09-01 19:36:57
【问题描述】:

你好 StackOverFLowers!

我正在尝试从以下内容中提取 xml .....

代码:

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\n",stringOfBody)

    //Unmarshalling
    var report reportType
    xml.Unmarshal(body, &report)
    fmt.Printf("The Report: %s\n", 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: \n%s\n",data)

    fmt.Println("CRN: %v\n",report.Crn)
    fmt.Println("ID: %v\n",report.Id)
    fmt.Println("Section: %v\n",report.Section)
    fmt.Println("Title: %v\n",report.Title)
    fmt.Println("Course: %v\n",report.Course)
    fmt.Println("ID: %v\n",report.Id)


}
func doErr( err error, message string){
    if err != nil{
        log.Panicf("ERROR: %s %s \n", message, err.Error())
    }


}

由于某种原因,它没有从文件中提取数据,基本上给我一个空白报告。两天前我做了一个这样的程序,并遵循相同的格式,但我不知道我哪里出错了。

输出:

The Report: {    }
JSON output nicely formatted: 
{
      "course": "",
      "crn": "",
      "id": "",
      "Section": "",
      "Title": ""
}

非常感谢任何和所有帮助,我将在几个小时后进行测试,我希望届时能得到一些指导!谢谢!

【问题讨论】:

    标签: jquery json xml go extract


    【解决方案1】:

    更改您在reportType 上的xml 属性以删除根元素:xml:"course>crn" 应该是xml:"crn"。如果您不需要使用 CharData,我还会将您的数据类型简化为字符串,这样您以后就不会再转换它们了。有关工作示例,请参阅我的示例 Playground。

    https://play.golang.org/p/xysSV-7oCA

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-14
      • 2017-01-08
      相关资源
      最近更新 更多