go语言解析xml文件

1、xml示例


    
      
        
          
            18
            0
          
          
            19
            1
          
        
      
    
  

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

你可能感兴趣的:(go语言)