IM正在尝试解析xml:
附加xml,因为它对于此处而言太长https://drive.google.com/file/d/1CPleC1gBAR6n7lcyiR_zVMEjkowo3gUU/view?usp=sharing
type 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 + "º")
println(users.N1GeneralInfo.SpectralInformationList.SpectralInformation[1].BandId)
// we iterate through every user within our users array and
// print out the user Type, their name, and their facebook url
// as just an example
// for i := 0; i < len(users.N1GeneralInfo.SpectralInformationList.SpectralInformation); i++ {
// for i := 0; i < 10; i++ {
// fmt.Println("Band Id: " + users.N1GeneralInfo.SpectralInformationList.SpectralInformation[i].BandId)
// fmt.Println("physicalBand: " + users.N1GeneralInfo.SpectralInformationList.SpectralInformation[i].PhysicalBand)
// }
}
但是当文件被正确读取时,miXML结构是空的
fmt.Println(users.N1GeneralInfo.ProductInfo.ProductStartTime + "º")
println(users.N1GeneralInfo.SpectralInformationList.SpectralInformation[1].BandId)
fmt.Println(users.N1GeneralInfo.ProductInfo.ProductStartTime + "º")
println(users.N1GeneralInfo.SpectralInformationList.SpectralInformation[1].BandId)
就像结构体是空的:输出:**
运行时错误:索引超出范围goroutine 1 [正在运行]:应用程序/服务/带宽/服务.go:53 + 0x 20**
退出状态2
1条答案
按热度按时间lf5gs5x21#
如果您需要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"
请参阅:Parse Xml in GO for atttribute with ":" in tag
以下是一个演示:
输出如下:
2022-11-09T16:55:19.024Zº