zoukankan      html  css  js  c++  java
  • xml格式化写入文件

    参考xml文件地址:http://cloudprint.cainiao.com/template/standard/101

    代码:

    package main
    
    import (
        "encoding/xml"
        "fmt"
        "os"
        "time"
    )
    
    type xmldas struct {
        XMLName           xml.Name       `xml:"page"`
        Xmlns             string         `xml:"xmlns,attr"`
        XmlnsXsi          string         `xml:"xmlns:xsi,attr"`
        XsiSchemaLocation string      `xml:"xsi:schemaLocation,attr"`
        XmlnsEditor       string        `xml:"xmlns:editor,attr"`
        Width             string         `xml:"width,attr"`
        Height            string        `xml:"height,attr"`
        Auth              xmlAuth        `xml:"auth"`
    }
    
    type xmlAuth struct {
        Name string `xml:name`
        Age  int `xml:age`
    }
    
    func main() {
        v := xmldas{Xmlns:"http://cloudprint.cainiao.com/print", XmlnsXsi:"http://www.w3.org/2001/XMLSchema-instance", XsiSchemaLocation:"http://cloudprint.cainiao.com/print http://cloudprint-docs-resource.oss-cn-shanghai.aliyuncs.com/lpml_schema.xsd", XmlnsEditor:"http://cloudprint.cainiao.com/schema/editor", Width:"100", Height:"180" }
        v.Auth = xmlAuth{Name:"ha666", Age:32}
        output, err := xml.MarshalIndent(v, "", "	")
        if err != nil {
            fmt.Printf("error: %v
    ", err)
        }
        filename := time.Now().Format("2006-01-02-15-04-05")
        userFile := filename + ".xml"
        fout, err := os.Create(userFile)
        defer fout.Close()
        if err != nil {
            fmt.Println(userFile, err)
            return
        }
        fout.Write([]byte(xml.Header))
        fout.Write(output)
        fout.Close()
    }

    生成的xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <page xmlns="http://cloudprint.cainiao.com/print" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://cloudprint.cainiao.com/print http://cloudprint-docs-resource.oss-cn-shanghai.aliyuncs.com/lpml_schema.xsd" xmlns:editor="http://cloudprint.cainiao.com/schema/editor" width="100" height="180">
        <auth>
            <Name>ha666</Name>
            <Age>32</Age>
        </auth>
    </page>

    ha666@ha666.com

    http://www.ha666.com

  • 相关阅读:
    前端js部分面试题
    前端css部分面试笔试题
    javascript 面向对象
    string 对象
    JavaScript 正则表达式
    JavaScirpt 位运算
    JavaScript冒泡循环排序案例
    JavaScript 练习题
    浏览器缓存机制
    浏览器对象模型(BOM)
  • 原文地址:https://www.cnblogs.com/ha666/p/6185318.html
Copyright © 2011-2022 走看看