亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嘗試在 golang 中解析 xml 但得到空結構

嘗試在 golang 中解析 xml 但得到空結構

Go
至尊寶的傳說 2023-03-15 14:21:55
我正在嘗試解析一個 xml:附上 xml,因為這里太長了https://drive.google.com/file/d/1CPleC1gBAR6n7lcyiR_zVMEjkowo3gUU/view?usp=sharingtype L1CProduct struct {    XMLName xml.Name `xml:"n1:Level-1C_User_Product"`    N1GeneralInfo N1GeneralInfo `xml:"n1:General_Info"`}type N1GeneralInfo struct {    XMLName xml.Name `xml:"n1:General_Info"`    ProductInfo ProductInfo `xml:"Product_Info"`    SpectralInformationList SpectralInformationList `xml:"Product_Image_Characteristic>Spectral_Information_List"`}type SpectralInformationList struct {    XMLName xml.Name `xml:"Spectral_Information_List"`    SpectralInformation []SpectralInformation `xml:"Spectral_Information"`}type SpectralInformation struct {    XMLName xml.Name `xml:"Spectral_Information"`    BandId string `xml:"bandId,attr"`    PhysicalBand string `xml:"physicalBand,attr"`}type ProductInfo struct{    XMLName xml.Name `xml:"Product_Info"`    ProductStartTime string `xml:"PRODUCT_START_TIME"`    GenerationTime string `xml:"GENERATION_TIME"`    ProductUri string `xml:"PRODUCT_URI"`}func parseXml() {    // Open our xmlFile    // xmlPath := inProcessPath + "/MTD_MSIL1C.xml"    xmlPath := "/home/htc/Lizer/backend/InProcessResults/MTD_MSIL1C.xml"    xmlFile, err := os.Open(xmlPath)    // if we os.Open returns an error then handle it    if err != nil {        fmt.Println(err)    }    fmt.Println("Successfully Opened " + xmlPath)    // defer the closing of our xmlFile so that we can parse it later on    defer xmlFile.Close()    // read our opened xmlFile as a byte array.    byteValue, _ := ioutil.ReadAll(xmlFile)    fmt.Printf("\nData: %s", byteValue)    // we initialize our Users array    var users L1CProduct    // we unmarshal our byteArray which contains our    // xmlFiles content into 'users' which we defined above    xml.Unmarshal(byteValue, &users)    fmt.Println(users.N1GeneralInfo.ProductInfo.ProductStartTime + "o")    println(users.N1GeneralInfo.SpectralInformationList.SpectralInformation[1].BandId)}但是當文件被正確讀取時,mi xml 結構是空的它就像結構是空的:輸出:**
查看完整描述

1 回答

?
犯罪嫌疑人X

TA貢獻2080條經驗 獲得超4個贊

如果你想要:xml 中的解析器

喜歡

xml:"n1:Level-1C_User_Product"

應該:

xml:"https://psd-14.sentinel2.eo.esa.int/PSD/User_Product_Level-1C.xsd Level-1C_User_Product"

請參閱:在 GO 中解析 Xml 以獲取標簽中帶有“:”的屬性

下面是一個演示:

package main


import (

    "encoding/xml"

    "fmt"

    "os"

)


type L1CProduct struct {

    XMLName       xml.Name `xml:"https://psd-14.sentinel2.eo.esa.int/PSD/User_Product_Level-1C.xsd Level-1C_User_Product"`

    N1GeneralInfo N1GeneralInfo

}


type N1GeneralInfo struct {

    XMLName     xml.Name    `xml:"https://psd-14.sentinel2.eo.esa.int/PSD/User_Product_Level-1C.xsd General_Info"`

    ProductInfo ProductInfo `xml:"Product_Info"`

}

type ProductInfo struct {

    XMLName          xml.Name `xml:"Product_Info"`

    ProductStartTime string   `xml:"PRODUCT_START_TIME"`

    GenerationTime   string   `xml:"GENERATION_TIME"`

    ProductUri       string   `xml:"PRODUCT_URI"`

}


func parseXml() {

    // Open our xmlFile


    // xmlPath := inProcessPath + "/MTD_MSIL1C.xml"

    xmlPath := "./MTD_MSIL1C.xml"

    // read our opened xmlFile as a byte array.

    byteValue, _ := os.ReadFile(xmlPath)

    // we initialize our Users array

    var users L1CProduct


    err := xml.Unmarshal(byteValue, &users)

    if err != nil {

        fmt.Printf("%v\n", err)

    }

    fmt.Println(users.N1GeneralInfo.ProductInfo.ProductStartTime + "o")

}


func main() {

    parseXml()

}

以下是輸出:


2022-11-09T16:55:19.024Zo


查看完整回答
反對 回復 2023-03-15
  • 1 回答
  • 0 關注
  • 175 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號