dongteng0748 2015-10-20 07:49
浏览 108
已采纳

在Go中解析多个XML标签

I'm trying to parse an xml file for sslscan which has the following output (shortened). I want to parse it with 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>

Code:

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
", xmlStraps[i].Status,      xmlStraps[i].SSLversion, xmlStraps[i].Bits, xmlStraps[i].Cipher)
  }
}

Everything works fine but I have no idea how to parse the date in <certificate><not-valid-before> without creating a new reader?

I tried to change the first two structs to

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"`
}

but this doesn't work.

xml: cipher>status chain not valid with attr flag exit status 1

Apparently in older versions of Go it worked.

  • 写回答

1条回答 默认 最新

  • douxiegan6468 2015-10-20 08:41
    关注

    I had to allocate the parameter of a new object to the object which was returned. I tried this before but I did a mistake

    type XMLStrap struct {
      Status     string `xml:"status,attr"`
      SSLversion string `xml:"sslversion,attr"`
      Bits       string `xml:"bits,attr"`
      Cipher     string `xml:"cipher,attr"`
      Valid      string ""
    }
    
    type Certs struct {
      Cert string `xml:"not-valid-after"`
    }
    
    type XMLStraps struct {
      XMLName    xml.Name   `xml:"document"`
      Straps     []XMLStrap `xml:"ssltest>cipher"`
      Validation Certs      `xml:"ssltest>certificate"`
    }
    
    func ReadStraps(reader io.Reader) ([]XMLStrap, error) {
      var xmlStraps XMLStraps
      if err := xml.NewDecoder(reader).Decode(&xmlStraps); err != nil {
        return nil, err
      }
      xmlStraps.Straps[0].Valid = xmlStraps.Validation.Cert
      return xmlStraps.Straps, nil
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器