2022-09-28 encoding/xml包的簡(jiǎn)單使用

encoding/xml的使用

Marshal序列化/Unmarshal反序列化

1.Marshal序列化:將結(jié)構(gòu)體序列化成的[]byte

type Person struct {
   XMLName xml.Name `xml:"person"`   //這個(gè)代表xml的根元素名為 person
   Name string     `xml:"name"`       //名為name的子節(jié)點(diǎn)
   Age int32         `xml:"age"`       //名為age的子節(jié)點(diǎn)
}
//marshal:將結(jié)構(gòu)體編碼成xml格式的[]byte
func testXmlMarsher(){
   p :=Person{
      Name: "huige",
      Age: 20,
   }
   //編碼成xml無(wú)縮進(jìn)
   b1 ,_ := xml.Marshal(p)
   fmt.Println("b1:=",string(b1))

   //編碼成xml有縮進(jìn)
   b2,_ := xml.MarshalIndent(p,"    ","    ")
   fmt.Println("b2:=",string(b2))
}

2.Unmarshal反序列化:一般用于將xml文件的數(shù)據(jù)反序列化到某個(gè)struct中

//Unmarshal:將xml形式的[]byte解碼到一個(gè)結(jié)構(gòu)體中
func testXmlUnmarsher(){
   b := ` <person>
         <name>huige</name>
         <age>20</age>
         </person>`
   data :=[]byte(b)
   var person Person
   xml.Unmarshal(data,&person)
   fmt.Printf("person: %v\n", person)
}
//使用ioutil快速讀取xml文件data,使用Unmarshal反序列化data到struct中,
func ReadXmlFile(){
   data,_:=ioutil.ReadFile("a.xml")
   var person Person
   xml.Unmarshal(data,&person)
   fmt.Println("person:=",person)
}

Decode編碼/Encode解碼

寫xml文件。

func WriteXml(){
   p :=Person{
      Name: "huige",
      Age: 20,
   }
   fd,_:=os.OpenFile("my.xml",os.O_CREATE|os.O_RDWR,0777)
   encoder := xml.NewEncoder(fd) //創(chuàng)建一個(gè)encoder
   encoder.Encode(p)            //將結(jié)體數(shù)據(jù)以xml形式保存到文件中
}

讀取層級(jí)比較多的xml的栗子

假如我們有一個(gè)如下xml:

<Config>
    <account>root</account>
    <password>root</password>
    <mysql>game</mysql>
    <address>127.0.0.1</address>
    <cmd>
        <data>
            <seq>0</seq>
            <creator>test</creator>
            <type>2</type>
            <cmdstr>CmdToForbidRegisterRole 0</cmdstr>
        </data>
    </cmd>
</Config>

我們應(yīng)該如何定義結(jié)構(gòu)體呢?正確應(yīng)該是一個(gè)一個(gè)父標(biāo)簽一個(gè)結(jié)構(gòu)體。
根據(jù)上面xml我們定義結(jié)構(gòu)體如下:

type Route struct {
    Cmdlist []Cmd `xml:"data"`
}

type Cmd struct {
    Index   int32  `xml:"seq"`
    Type    int32  `xml:"type"`
    Creator string `xml:"creator"`
    CmdStr  string `xml:"cmdstr"`
}
type Command struct {
    XMLName  xml.Name `xml:"Config"`
    Account  string   `xml:"account"`
    Password string   `xml:"password"`
    DBName   string   `xml:"mysql"`
    Address  string   `xml:"address"`
    CmdRoute Route    `xml:"cmd"`
}

第一層父標(biāo)簽Config我們對(duì)應(yīng)Command結(jié)構(gòu)體,第二層父標(biāo)簽cmd我們定義Route結(jié)構(gòu)體,第三個(gè)層ata父標(biāo)簽我們定義Cmd結(jié)構(gòu)體。這樣我們只要層層嵌套,就能把xml反序列化到Comand對(duì)象中。
注意
1.xml根節(jié)點(diǎn)需要使用XMLName xml.Name xml:"Config"指明
2.結(jié)構(gòu)體字段需要大寫,否則反序列化報(bào)錯(cuò)。
3.如果是整數(shù)請(qǐng)確定使用int32或者int64,不要使用int.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容