Golang - XML 使用 attr 解码
Golang - XML decoding with attr
我无法尝试从我正在尝试读取的旧设备中解组一些 XML。
设备以 ISO-8859-1 格式生成 XML。我已经成功转码,但很难将元素和属性映射到我的输出模型。
我没有发现任何结合这些问题的问题,也不知道这是否是导致问题的原因。
问题是并非所有 XML 细节都映射到我的对象
{Error:0 Context:2 LoginState:3 DI:0 DO:0 Clock:{Date:0/0/0 Time:12:54:52 Day:3} OWbus: Tmps:{Tmp:{ID:5 low: high: value:}} AOS:{AO:0} AIS:{AI:0}}
Tmps只保留最后一次重复,AOS和AIS没有映射。
这是一个带有示例 xml 输出的独立测试包装器。
package main
// go-webbrck is a lightweight package that is used to control a variety the legacy webbrick products
import (
"bytes"
"code.google.com/p/go-charset/charset" // For XML conversion
_ "code.google.com/p/go-charset/data" // Specs for dataset conversion
"encoding/xml" // For XML work
"fmt" // For outputting stuff
)
type Clock struct {
Date string
Time string
Day string
}
type Tmps struct {
Tmp Tmp `xml:"Tmp"`
}
type Tmp struct {
ID string `xml:"id,attr"`
low string `xml:"low,attr"`
high string `xml:"high,attr"`
value string `xml:",chardata"`
}
type AOs struct {
AO string `xml:"AO"`
}
type AO struct {
id string `xml:"id,attr"`
AO string `xml:",chardata"`
}
type AIs struct {
AI string `xml:"AI"`
}
type AI struct {
id string `xml:"id,attr"`
low string `xml:"low,attr"`
high string `xml:"high,attr"`
AI string `xml:",chardata"`
}
type WebbrickStatus struct {
Error string
Context string
LoginState string
DI string
DO string
Clock Clock `xml:"Clock"`
OWbus string
Tmps Tmps `xml:"Tmps"`
AOS AOs `xml:"AOs"`
AIS AIs `xml:"AIs"`
}
func main() {
fmt.Println("Hello, playground")
GetWBStatus()
}
// Get WB Status on Initilisation
func GetWBStatus() bool {
//var msg string
var strMsg string
strMsg = `<?xml version="1.0" encoding="ISO-8859-1"?>
<WebbrickStatus Ver="6.1.614">
<Error>0</Error>
<Context>2</Context>
<LoginState>3</LoginState>
<DI>0</DI>
<DO>0</DO>
<Clock>
<Date>0/0/0</Date>
<Time>12:54:52</Time>
<Day>3</Day>
</Clock>
<OWBus>1</OWBus>
<Tmps>
<Tmp id="1" lo="-800" hi="384">283</Tmp>
<Tmp id="2" lo="-800" hi="1600">0</Tmp>
<Tmp id="3" lo="-800" hi="1600">0</Tmp>
<Tmp id="4" lo="-800" hi="1600">0</Tmp>
<Tmp id="5" lo="-800" hi="1600">0</Tmp>
</Tmps>
<AOs>
<AO id="0">0</AO>
<AO id="1">0</AO>
<AO id="2">0</AO>
<AO id="3">0</AO>
</AOs>
<AIs>
<AI id="0" lo="0" hi="100">1</AI>
<AI id="1" lo="0" hi="100">0</AI>
<AI id="2" lo="0" hi="100">0</AI>
<AI id="3" lo="0" hi="100">0</AI>
</AIs>
</WebbrickStatus>`
fmt.Println("\n\n*** Setting up\n==============\n\n")
fmt.Printf("%v", strMsg)
msg := []byte(strMsg)
// // Decode XML encoding
var _wbs WebbrickStatus // create container to load the xml
reader := bytes.NewReader(msg) // create a new reader for transcoding to utf-8
decoder := xml.NewDecoder(reader) // create a new xml decoder
decoder.CharsetReader = charset.NewReader // bind the reader to the decoder
fmt.Println("*** Decoding\n")
xmlerr := decoder.Decode(&_wbs) // unmarshall the xml
if xmlerr != nil {
fmt.Printf("error: %v", xmlerr)
return false
}
fmt.Println("*** Result\n")
fmt.Printf("%+v\n\n\n", _wbs)
return true
}
谢谢
您的建模非常接近,您只需修复 "array holder" 结构元素(Tmps、AOs、AIs)的类型。试试这个:
type Tmps struct {
Tmp []Tmp
}
type AOs struct {
AO []AO
}
type AIs struct {
AI []AI
}
另请注意,除非另有说明,否则 XML 包需要 XML element name to match the struct field name,因此您可以省略一些 xml:"..."
标签。
我无法尝试从我正在尝试读取的旧设备中解组一些 XML。
设备以 ISO-8859-1 格式生成 XML。我已经成功转码,但很难将元素和属性映射到我的输出模型。
我没有发现任何结合这些问题的问题,也不知道这是否是导致问题的原因。
问题是并非所有 XML 细节都映射到我的对象
{Error:0 Context:2 LoginState:3 DI:0 DO:0 Clock:{Date:0/0/0 Time:12:54:52 Day:3} OWbus: Tmps:{Tmp:{ID:5 low: high: value:}} AOS:{AO:0} AIS:{AI:0}}
Tmps只保留最后一次重复,AOS和AIS没有映射。
这是一个带有示例 xml 输出的独立测试包装器。
package main
// go-webbrck is a lightweight package that is used to control a variety the legacy webbrick products
import (
"bytes"
"code.google.com/p/go-charset/charset" // For XML conversion
_ "code.google.com/p/go-charset/data" // Specs for dataset conversion
"encoding/xml" // For XML work
"fmt" // For outputting stuff
)
type Clock struct {
Date string
Time string
Day string
}
type Tmps struct {
Tmp Tmp `xml:"Tmp"`
}
type Tmp struct {
ID string `xml:"id,attr"`
low string `xml:"low,attr"`
high string `xml:"high,attr"`
value string `xml:",chardata"`
}
type AOs struct {
AO string `xml:"AO"`
}
type AO struct {
id string `xml:"id,attr"`
AO string `xml:",chardata"`
}
type AIs struct {
AI string `xml:"AI"`
}
type AI struct {
id string `xml:"id,attr"`
low string `xml:"low,attr"`
high string `xml:"high,attr"`
AI string `xml:",chardata"`
}
type WebbrickStatus struct {
Error string
Context string
LoginState string
DI string
DO string
Clock Clock `xml:"Clock"`
OWbus string
Tmps Tmps `xml:"Tmps"`
AOS AOs `xml:"AOs"`
AIS AIs `xml:"AIs"`
}
func main() {
fmt.Println("Hello, playground")
GetWBStatus()
}
// Get WB Status on Initilisation
func GetWBStatus() bool {
//var msg string
var strMsg string
strMsg = `<?xml version="1.0" encoding="ISO-8859-1"?>
<WebbrickStatus Ver="6.1.614">
<Error>0</Error>
<Context>2</Context>
<LoginState>3</LoginState>
<DI>0</DI>
<DO>0</DO>
<Clock>
<Date>0/0/0</Date>
<Time>12:54:52</Time>
<Day>3</Day>
</Clock>
<OWBus>1</OWBus>
<Tmps>
<Tmp id="1" lo="-800" hi="384">283</Tmp>
<Tmp id="2" lo="-800" hi="1600">0</Tmp>
<Tmp id="3" lo="-800" hi="1600">0</Tmp>
<Tmp id="4" lo="-800" hi="1600">0</Tmp>
<Tmp id="5" lo="-800" hi="1600">0</Tmp>
</Tmps>
<AOs>
<AO id="0">0</AO>
<AO id="1">0</AO>
<AO id="2">0</AO>
<AO id="3">0</AO>
</AOs>
<AIs>
<AI id="0" lo="0" hi="100">1</AI>
<AI id="1" lo="0" hi="100">0</AI>
<AI id="2" lo="0" hi="100">0</AI>
<AI id="3" lo="0" hi="100">0</AI>
</AIs>
</WebbrickStatus>`
fmt.Println("\n\n*** Setting up\n==============\n\n")
fmt.Printf("%v", strMsg)
msg := []byte(strMsg)
// // Decode XML encoding
var _wbs WebbrickStatus // create container to load the xml
reader := bytes.NewReader(msg) // create a new reader for transcoding to utf-8
decoder := xml.NewDecoder(reader) // create a new xml decoder
decoder.CharsetReader = charset.NewReader // bind the reader to the decoder
fmt.Println("*** Decoding\n")
xmlerr := decoder.Decode(&_wbs) // unmarshall the xml
if xmlerr != nil {
fmt.Printf("error: %v", xmlerr)
return false
}
fmt.Println("*** Result\n")
fmt.Printf("%+v\n\n\n", _wbs)
return true
}
谢谢
您的建模非常接近,您只需修复 "array holder" 结构元素(Tmps、AOs、AIs)的类型。试试这个:
type Tmps struct {
Tmp []Tmp
}
type AOs struct {
AO []AO
}
type AIs struct {
AI []AI
}
另请注意,除非另有说明,否则 XML 包需要 XML element name to match the struct field name,因此您可以省略一些 xml:"..."
标签。