go 处理xml

package main

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

type BcpData struct {
    XMLName     xml.Name      `xml:"MESSAGE"`
    Items       []Item2       `xml:"ITEM"`
    Bcpdatainfo []BcpDatainfo `xml:"DATASET"`
}

type BcpDatainfo struct {
    Name               string              `xml:"name,attr"`
    Rmk                string              `xml:"rmk,attr"`
    BcpDatainfodetails []BcpDatainfodetail `xml:"DATA"`
}

type BcpDatainfodetail struct {
    Items []Item `xml:"ITEM"`
}

type Item2 struct {
    Key string `xml:"key,attr"`
    Val string `xml:"val,attr"`
    Rmk string `xml:"rmk,attr"`
}

type Item struct {
    Attributes []xml.Attr `xml:",any,attr"`
}

var data = `
                
                
                
                
                
                
                
                
                    
                        
                        
                        
                        
                        
                        
                        
                        
                        
                    
                
                
                    
                        
                        
                        
                    
                
            `

func main() {
    v := new(BcpData)
    err := xml.Unmarshal([]byte(data), v)
    if err != nil {
        fmt.Printf("error: %v", err)
        return
    }
    xmlOutPut, _ := xml.MarshalIndent(v, " ", " ")
    headerBytes := []byte(xml.Header)
    xmlOutPutData := append(headerBytes, xmlOutPut...)
    ioutil.WriteFile("test11.xml", xmlOutPutData, os.ModeAppend)
}

 

你可能感兴趣的:(golang)