使用Ruby生成XML文件

上一篇 / 下一篇  2013-03-05 18:40:16 / 个人分类:测试

下文为一个ruby生成多层xml文档的实例:

require 'win32ole'

#创建xml文件
xmlDoc=WIN32OLE.new("MSXML2.DOMDocument")

#设置Root
Root=xmlDoc.createElement("Root")
xmlDoc.appendChild Root

#创建各层子元素

#层1
bc1=xmlDoc.createElement("child_1")
  bc1Attribute=xmlDoc.createAttribute("Level-1")
  bc1Attribute.text="1"
  bc1.text="1"
  bc1.setAttributeNode bc1Attribute
#层2
bc2=xmlDoc.createElement("child_2")
  bc2Attribute=xmlDoc.createAttribute("Leve-2")
  bc2.setAttributeNode bc2Attribute
  bc2Attribute.text="2"
  bc2.text="2"
#层3
bc3=xmlDoc.createElement("child_3")
  bc3.text="3"
#层4-1
bc41=xmlDoc.createElement("child_4_1")
  bc41.text="4-1"
#层4-2
bc42=xmlDoc.createElement("child_4_2")
  bc42.text="4-2"
  
#加入各自父节点下
  bc3.appendChild bc41
  bc3.appendChild bc42
  bc2.appendChild bc3
  bc1.appendChild bc2
  Root.appendChild bc1


#创建 XML processing instruction 并把它加到根元素之前
header=xmlDoc.createProcessingInstruction("xml","version='1.0'")
xmlDoc.insertBefore header,xmlDoc.childNodes(0)
#文件保存
xmlDoc.Save "c:\\test.xml"

执行结果如下:

TAG:

 

评分:0

我来说两句

日历

« 2024-04-23  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 150805
  • 日志数: 185
  • 文件数: 6
  • 建立时间: 2007-08-06
  • 更新时间: 2015-01-06

RSS订阅

Open Toolbar