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

代码:

  1. package main
  2.  
  3. import (
  4. "encoding/xml"
  5. "fmt"
  6. "os"
  7. "time"
  8. )
  9.  
  10. type xmldas struct {
  11. XMLName xml.Name `xml:"page"`
  12. Xmlns string `xml:"xmlns,attr"`
  13. XmlnsXsi string `xml:"xmlns:xsi,attr"`
  14. XsiSchemaLocation string `xml:"xsi:schemaLocation,attr"`
  15. XmlnsEditor string `xml:"xmlns:editor,attr"`
  16. Width string `xml:"width,attr"`
  17. Height string `xml:"height,attr"`
  18. Auth xmlAuth `xml:"auth"`
  19. }
  20.  
  21. type xmlAuth struct {
  22. Name string `xml:name`
  23. Age int `xml:age`
  24. }
  25.  
  26. func main() {
  27. 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:"", Height:"" }
  28. v.Auth = xmlAuth{Name:"ha666", Age:}
  29. output, err := xml.MarshalIndent(v, "", "\t")
  30. if err != nil {
  31. fmt.Printf("error: %v\n", err)
  32. }
  33. filename := time.Now().Format("2006-01-02-15-04-05")
  34. userFile := filename + ".xml"
  35. fout, err := os.Create(userFile)
  36. defer fout.Close()
  37. if err != nil {
  38. fmt.Println(userFile, err)
  39. return
  40. }
  41. fout.Write([]byte(xml.Header))
  42. fout.Write(output)
  43. fout.Close()
  44. }

生成的xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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="" height="">
  3. <auth>
  4. <Name>ha666</Name>
  5. <Age></Age>
  6. </auth>
  7. </page>

xml格式化写入文件的更多相关文章

  1. c#读取并异步写入文件,简单版,指定编码,保持原格式。

    1.同步读取和写入 StreamReader objReader = new StreamReader("E://workspace//zzz//read.txt", Encodi ...

  2. android XMl 解析神奇xstream 五: 把复杂对象转换成 xml ,并写入SD卡中的xml文件

    前言:对xstream不理解的请看: android XMl 解析神奇xstream 一: 解析android项目中 asset 文件夹 下的 aa.xml 文件 android XMl 解析神奇xs ...

  3. EFDC主控文件efdc.inp文件的xml格式化处理——转载http://blog.sina.com.cn/s/articlelist_1584892573_0_1.html

    在对EFDC模型进行系统集成时,虽然可以通过一行一行读来进行解析处理,但那将是一个繁琐的工作.我们经过xml格式化处理后,再经xsd转成Dataset类对象,那么整个主控文件就是一个数据库表集合,对其 ...

  4. python关于字典如何格式化地写入文件之中

    1.python关于字典如何式化地写入文件之中 如何写入:https://blog.csdn.net/qq_15642411/article/details/79943741 (推荐使用json包) ...

  5. 对XML和YAML文件实现I/O操作

    1.文件的打开关闭 XML\YAML文件在OpenCV中的数据结构为FileStorage,打开操作例如: string filename = "I.xml"; FileStora ...

  6. Opencv探索之路(十九):读写xml和yml文件

    有时候我们处理完图像后需要保存一下数据到文件上,以供下一步的处理.一个比较广泛的需求场景就是:我们对一幅图像进行特征提取之后,需要把特征点信息保存到文件上,以供后面的机器学习分类操作.那么如果遇到这样 ...

  7. 关于spark写入文件至文件系统并制定文件名之自定义outputFormat

    引言: spark项目中通常我们需要将我们处理之后数据保存到文件中,比如将处理之后的RDD保存到hdfs上指定的目录中,亦或是保存在本地 spark保存文件: 1.rdd.saveAsTextFile ...

  8. 安装sudo apt-get install ros-kinetic-desktop-full,报错mv: 无法获取'/var/lib/ni/licenses.xml.dpkg-old' 的文件状态(stat): 没有。。。。

    安装sudo apt-get install ros-kinetic-desktop-full,报了一堆错误 mv: 无法获取'/var/lib/ni/licenses.xml.dpkg-old' 的 ...

  9. 将datagridview数据保为xml或txt文件

    using System.IOpublic void SaveFile()        {            //实例化一个保存文件对话框            SaveFileDialog s ...

随机推荐

  1. 《31天成为IT服务达人》之精准运维

     1       第十四章精准运维服务 近年来国内IT产业的发展格局始终与我国的经济转型与创新创业的社会大潮息息相关,纵观国内国际形势,IT产业仍是创新的领头者,是解决人类各种经济危机的主要扮演者 ...

  2. 怎么进行robot检測

    服务端能够通过三种途径进行robot检測: 第一种,利用http的User-Agent header进行推断,这样的是最正常的推断,但这样的不能检測出不友好的请求,它能够伪造. 另外一种,限制请求频率 ...

  3. 什么是SEO?SEO干嘛的?怎么做SEO?

    黑帽seo分享了这么多SEO的东西,今天在群时面遇到群亲问到了什么是SEO?SEO是干嘛的?蜗牛认为,是时候让大家知道这些不为外人所知的真相了.且听蜗牛慢慢道来吧. 一.什么是SEO?SEO是干嘛的? ...

  4. MySQL 5.6初始配置调整

    原文链接: What to tune in MySQL 5.6 after installation原文日期: 2013年09月17日翻译日期: 2014年06月01日翻译人员: 铁锚 随着 大量默认 ...

  5. Java读取WEB-INF目录下的properties配置文件

    如何在Java代码中读取WEB-INF目录下的properties配置文件,下文给出了一个解决方案. 我们习惯将一些配置信息写在配置文件中,比如将数据库的配置信息URL.User和Password写在 ...

  6. excel中VBA对多个文件的操作

    添加引用 "Scripting.FileSystemObject" (Microsoft Scripting Runtime) '用于操作文件.目录 Sub 数据整理部分() ' ...

  7. datazen 修改instanceid db_encryption_key

    切换到Datazen.Enterprise.Server.3.0.3327.exe 所在的目录 运行如下命令: Datazen.Enterprise.Server.3.0.3327.exe DATAZ ...

  8. java中XMLGregorianCalendar类型和Date类型之间的相互转换

    import java.text.SimpleDateFormat;import java.util.Date;import java.util.GregorianCalendar;import ja ...

  9. mysql 安装-编码

    mysql的安装过程相对较为简单,在这里就不阐述,我想说的问题是,关于编码的安装, 在安装到达'Please select the default character set'的时候,选择'Manul ...

  10. 用于显示上个月和下个月_PHP

    /** * 用于显示上个月和下个月 * @param int $sign 1:表示上个月 0:表示下个月 * @return string */ function GetMonth($sign=&qu ...