猿问

如何使用 Unmarshal 解析 XML

我在 Go 中编写了一个非常简单的 XML 解析器,但是我的 xml.Unmarshal 函数不能正常工作。这是示例代码。


我不确定我的结构是否正确。我读了一些页面,我认为这种格式更有条理。


package main


import (

   "fmt"

   "encoding/xml"

)


//this a xml sample of my document. 


var str = `<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>

    <mensagem>

        <cabecalho>

            <identificacaoTransacao>

                <tipoTransacao>DESCRICAO</tipoTransacao>

                <sequencialTransacao>5443811</sequencialTransacao>

                <dataRegistroTransacao>2020-02-27</dataRegistroTransacao>

                <horaRegistroTransacao>17:35:11</horaRegistroTransacao>

            </identificacaoTransacao>

            <origem>

                <registroANS>005711</registroANS>

            </origem>

            <destino>

                <identificacaoPrestador>

                    <codigoPrestadorNaOperadora>687146</codigoPrestadorNaOperadora>

                </identificacaoPrestador>

    </destino>

    <Padrao>3.03.03</Padrao>

    </cabecalho>

</mensagem>`


    type XmlTISSIndex struct {

            XMLCabecalho struct {

                XMLAnsIdentificacaoTransacao struct {

                    XMLAnstipoTransacao         string `xml:"tipoTransacao"`

                    XMLAnsSequenciaTransacao    string `xml:"sequencialTransacao"`

                    XMLAnsDataRegistroTransacao string `xml:"dataRegistroTransacao"`

                    XMLAnsHoraRegistroTransacao string `xml:"horaRegistroTransacao"`

                }

                XMLAnsOrigem struct {

                    XMLAnsRegistro string `xml:"registroANS"`

                }

                XMLAnsDestino struct {

                    XMLAnsIdentificacaoPrestador struct {

                        XMLAnsCodigoPrestadorNaOperadora string `xml:"codigoPrestadorNaOperadora"`

                }

          }

       }

    }



叮当猫咪
浏览 114回答 1
1回答

拉风的咖菲猫

您构建结构的方式不正确。此外,因为您使用的是 ISO-8859-1 编码,这让事情变得更加棘手......我使用这篇文章来帮助解决这个问题。像这样的东西应该工作:package mainimport (&nbsp; &nbsp; "bytes"&nbsp; &nbsp; "encoding/xml"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "golang.org/x/net/html/charset" // <-- Had to use this due to ISO-8859-1 encoding)// Sample documentvar str = `<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>&nbsp; &nbsp; <mensagem>&nbsp; &nbsp; &nbsp; &nbsp; <cabecalho>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <identificacaoTransacao>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tipoTransacao>DESCRICAO</tipoTransacao>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <sequencialTransacao>5443811</sequencialTransacao>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <dataRegistroTransacao>2020-02-27</dataRegistroTransacao>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <horaRegistroTransacao>17:35:11</horaRegistroTransacao>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </identificacaoTransacao>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <origem>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <registroANS>005711</registroANS>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </origem>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <destino>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <identificacaoPrestador>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <codigoPrestadorNaOperadora>687146</codigoPrestadorNaOperadora>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </identificacaoPrestador>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </destino>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Padrao>3.03.03</Padrao>&nbsp; &nbsp; &nbsp; &nbsp; </cabecalho>&nbsp; &nbsp; </mensagem>`type IdentificacaoTransacao struct {&nbsp; &nbsp; XMLName&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xml.Name `xml:"identificacaoTransacao"`&nbsp; &nbsp; TipoTransacao&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;string&nbsp; &nbsp;`xml:"tipoTransacao"`&nbsp; &nbsp; SequencialTransacao&nbsp; &nbsp;string&nbsp; &nbsp;`xml:"sequencialTransacao"`&nbsp; &nbsp; DataRegistroTransacao string&nbsp; &nbsp;`xml:"dataRegistroTransacao"`&nbsp; &nbsp; HoraRegistroTransacao string&nbsp; &nbsp;`xml:"horaRegistroTransacao"`}type Origem struct {&nbsp; &nbsp; XMLName&nbsp; &nbsp; &nbsp;xml.Name `xml:"origem"`&nbsp; &nbsp; RegistroANS string&nbsp; &nbsp;`xml:"registroANS"`}type IdentificacaoPrestador struct {&nbsp; &nbsp; XMLName&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xml.Name `xml:"identificacaoPrestador"`&nbsp; &nbsp; CodigoPrestadorNaOperadora string&nbsp; &nbsp;`xml:"codigoPrestadorNaOperadora"`}type Destino struct {&nbsp; &nbsp; XMLName&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xml.Name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;`xml:"destino"`&nbsp; &nbsp; IdentificacaoPrestador IdentificacaoPrestador `xml:"identificacaoPrestador"`}type Cabecalho struct {&nbsp; &nbsp; XMLName&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xml.Name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;`xml:"cabecalho"`&nbsp; &nbsp; IdentificacaoTransacao IdentificacaoTransacao `xml:"identificacaoTransacao"`&nbsp; &nbsp; Origem&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Origem&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;`xml:"origem"`&nbsp; &nbsp; Destino&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Destino&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `xml:"destino"`&nbsp; &nbsp; Padrao&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;string&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;`xml:"Padrao"`}type Mensagem struct {&nbsp; &nbsp; XMLName&nbsp; &nbsp;xml.Name&nbsp; `xml:"mensagem"`&nbsp; &nbsp; Cabecalho Cabecalho `xml:"cabecalho"`}func main() {&nbsp; &nbsp; var mensagem Mensagem&nbsp; &nbsp; var err error&nbsp; &nbsp; byteStr := []byte(str)&nbsp; &nbsp; reader := bytes.NewReader(byteStr)&nbsp; &nbsp; decoder := xml.NewDecoder(reader)&nbsp; &nbsp; decoder.CharsetReader = charset.NewReaderLabel&nbsp; &nbsp; err = decoder.Decode(&mensagem)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Decoder error:", err)&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(mensagem)}这给了我以下输出:{{ mensagem} {{ cabecalho} {{ identificacaoTransacao} DESCRICAO 5443811 2020-02-27 17:35:11} {{ origem} 005711} {{ destino} {{ identificacaoPrestador} 687146}} 3.03.03}}
随时随地看视频慕课网APP

相关分类

Go
我要回答