2016-05-06 5 views
0

こんにちはStackOverFLowers!私は、次からXMLを抽出しようとしているGOLang:XMLを抽出する問題

..... enter image description here

コード:それは基本的に私を与えたファイルからデータを抽出していないいくつかの理由について

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()) 
    } 


} 

空白のレポート。私はこのようなプログラムを文字通り2日前に作って同じフォーマットに従っていますが、どこが間違っているのか分かりません。

OUTPUT:

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

任意およびすべてのヘルプははるかに高く評価され、私は数時間でテストを持っていると私はそれまでにいくつかのガイダンスのために願っています!ありがとう!

答えて

0

reportTypeのxml属性を変更してルート要素を削除します。xml:"course>crn"xml:"crn"である必要があります。 CharDataを使用する必要がない場合は、後で変換しないようにデータ型を文字列に単純化します。実際の例については、私の例の遊び場を参照してください。

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

関連する問題