go语言解析xml文件

1、xml示例

<Managed xmlns="aa">
    <Function xmlns="bb">
      <Cell>
        <Block xmlns="cc">
          <Info>
            <beamIndex>18</beamIndex>
            <ssbIndex>0</ssbIndex>
          </Info>
          <Info>
            <beamIndex>19</beamIndex>
            <ssbIndex>1</ssbIndex>
          </Info>
        </Block>
      </Cell>
    </Function>
  </Managed>

2、Go语言代码

package main

import (
	"encoding/xml"
	"fmt"
	"io/ioutil"
	"os"
)

type Managed struct {
	Function Function `xml:"Function"`
}
type Function struct {
	Cell Cell `xml:"Cell"`
}
type Cell struct {
	Block Block `xml:"Block"`
}
type Block struct {
	Info []Info `xml:"Info"`
}
type Info struct {
	BeamIndex int `xml:"beamIndex"`  //一定要注意xml文件中小写,而结构体参数要大写
	SsbIndex  int `xml:"ssbIndex"`
}

func main() {
	//将文件读取成字节数组
	content, err := ioutil.ReadFile("E:\\test.xml")
	if err != nil {
		fmt.Fprintln(os.Stderr, err.Error())
		os.Exit(9)
	}
	var ps Managed
	//反序列化xml
	xml.Unmarshal(content, &ps)
	beamLen := len(ps.Function.Cell.Block.Info)
	ssbIndex2Beam := make(map[int]int)
	for i := 0; i < beamLen; i++ {
		ssbIndex := ps.Function.Cell.Block.Info[i].SsbIndex
		ssbIndex2Beam[ssbIndex] = ps.Function.Cell.Block.Info[i].BeamIndex
	}
	fmt.Println(ssbIndex2Beam)
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值