douzhanjia0773 2015-03-27 17:35
浏览 288
已采纳

Golang-使用attr进行XML解码

I've got stuck with trying to unmarshall some XML from an old device I'm trying to read from.

The device produces XML in ISO-8859-1 format. I've managed to transcode, but am struggling to map the elements and attributes to my output model.

I've not found any questions that combine these, and don't know if this is causing the issue.

The issue is that not all of the XML details are being mapped to my object

{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}}

The Tmps only holds the last repeat, and the AOS and AIS are not being mapped.

Here's a standalone test wrapper with an example xml output.

    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("

*** Setting up
==============

")
    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
")
    xmlerr := decoder.Decode(&_wbs) // unmarshall the xml
    if xmlerr != nil {
        fmt.Printf("error: %v", xmlerr)
        return false
    }

    fmt.Println("*** Result
")

    fmt.Printf("%+v


", _wbs)

    return true

}

Thanks

  • 写回答

1条回答 默认 最新

  • duannuochi3549 2015-03-27 18:24
    关注

    Your modeling is very close, you just need to fix the type of the "array holder" struct elements (Tmps, AOs, AIs). Try this:

    type Tmps struct {
      Tmp []Tmp
    }
    type AOs struct {
      AO []AO
    }
    type AIs struct {
      AI []AI
    }
    

    Note also that the XML package will expect the XML element name to match the struct field name unless otherwise specified, so you can omit some xml:"..." tags.

    Here is a simplified example on the go playground.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程