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

  • 相关阅读:
    树的直径 poj 2631
    hdu 3954 Level up(线段树)
    [Java Web]Struts2解决中文乱码问题
    怎样为virtualbox添加新的分辨率
    本人的cocos2d-x之路
    php 中利用json_encode和json_decode传递包括特殊字符的数据
    【设计模式】—— 创建者模式Builder
    【设计模式】——工厂方法FactoryMethod
    【Apache开源软件基金会项目】
    【设计模式】——抽象工厂Abstract Factory
  • 原文地址:https://www.cnblogs.com/ha666/p/6185318.html
Copyright © 2011-2022 走看看