猿问

在 Go 中解析多个 XML 标签

我正在尝试为 sslscan 解析一个 xml 文件,它具有以下输出(已缩短)。我想用 Go 解析它。


<document title="SSLScan Results">

 <ssltest host="x.x.x.x" port="443">

   <cipher status="accepted" sslversion="TLSv1.0" bits="256" cipher="DHE-RSA-CAMELLIA256-SHA" dhebits="2048" />

             .

             .

   <cipher status="accepted" sslversion="TLSv1.2" bits="112" cipher="DES-CBC3-SHA" />               .


  <certificate>

   <not-valid-before>Jun  6 00:00:00 2014 GMT</not-valid-before>

   <not-valid-after>Jul  4 23:59:59 2017 GMT</not-valid-after>

  </certificate>

 </ssltest>

</document>

代码:


type XMLStrap struct {

  Status     string `xml:"status,attr"`

  SSLversion string `xml:"sslversion,attr"`

  Bits       string `xml:"bits,attr"`

  Cipher     string `xml:"cipher,attr"`

}


type XMLStraps struct {

  XMLName xml.Name   `xml:"document"`

  Straps  []XMLStrap `xml:"ssltest>cipher"`

}


func ReadStraps(reader io.Reader) ([]XMLStrap, error) {

  var xmlStraps XMLStraps

  if err := xml.NewDecoder(reader).Decode(&xmlStraps); err != nil {

    return nil, err

  }

  return xmlStraps.Straps, nil

}



func main() {

  strapsFilePath, err := filepath.Abs("straps.xml")

  if err != nil {

    fmt.Println(err)

    os.Exit(1)

  }


  file, err := os.Open(strapsFilePath)

  if err != nil {

    fmt.Println(err)

    os.Exit(1)

  }


  defer file.Close()


  xmlStraps, err := ReadStraps(file)

  if err != nil {

    fmt.Println(err)

    os.Exit(1)

  }


  for i := 0; i < len(xmlStraps); i++ {

    fmt.Printf("%s %s %s %s\n", xmlStraps[i].Status,      xmlStraps[i].SSLversion, xmlStraps[i].Bits, xmlStraps[i].Cipher)

  }

}

一切正常,但我不知道如何在<certificate><not-valid-before>不创建新阅读器的情况下解析日期?


我试图将前两个结构更改为


type XMLStrap struct {

  Status     string `xml:"cipher>status,attr"`

  SSLversion string `xml:"cipher>sslversion,attr"`

  Bits       string `xml:"cipher>bits,attr"`

  Cipher     string `xml:"cipher>cipher,attr"`

  Cert       string `xml:"certificate>not-valid-after,chardata"`

}


type XMLStraps struct {

  XMLName xml.Name   `xml:"document"`

  Straps  []XMLStrap `xml:"ssltest"`

}

显然在旧版本的 Go 中它有效。


慕妹3146593
浏览 205回答 1
1回答

呼如林

我必须将新对象的参数分配给返回的对象。我以前试过这个,但我犯了一个错误type XMLStrap struct {&nbsp; Status&nbsp; &nbsp; &nbsp;string `xml:"status,attr"`&nbsp; SSLversion string `xml:"sslversion,attr"`&nbsp; Bits&nbsp; &nbsp; &nbsp; &nbsp;string `xml:"bits,attr"`&nbsp; Cipher&nbsp; &nbsp; &nbsp;string `xml:"cipher,attr"`&nbsp; Valid&nbsp; &nbsp; &nbsp; string ""}type Certs struct {&nbsp; Cert string `xml:"not-valid-after"`}type XMLStraps struct {&nbsp; XMLName&nbsp; &nbsp; xml.Name&nbsp; &nbsp;`xml:"document"`&nbsp; Straps&nbsp; &nbsp; &nbsp;[]XMLStrap `xml:"ssltest>cipher"`&nbsp; Validation Certs&nbsp; &nbsp; &nbsp; `xml:"ssltest>certificate"`}func ReadStraps(reader io.Reader) ([]XMLStrap, error) {&nbsp; var xmlStraps XMLStraps&nbsp; if err := xml.NewDecoder(reader).Decode(&xmlStraps); err != nil {&nbsp; &nbsp; return nil, err&nbsp; }&nbsp; xmlStraps.Straps[0].Valid = xmlStraps.Validation.Cert&nbsp; return xmlStraps.Straps, nil}
随时随地看视频慕课网APP

相关分类

Go
我要回答