计算机一级

3773考试网计算机等级考试计算机一级正文

用vb实现word文档生成xml文件并互相转换

来源:青年人 2010-8-2 17:26:16

 

1. 建立一个新的vb工程。
  2. 引用 Microsoft XML,版本 2.0 或以上。
  3. 在窗体form1上建立按钮 cmdCreateXML 和 cmdGetBinary
  代码:
  Option Explicit
  Dim oDoc As DOMDocument
  Dim DOCINPATH As String
  Dim XMLOUTPATH As String
  Dim DOCOUTPATH As String
  Private Sub cmdCreateXML_Click()
  Dim oEle As IXMLDOMElement
  Dim oRoot As IXMLDOMElement
  Dim oNode As IXMLDOMNode
  DOCINPATH = App.Path & "DocInput.doc"
  XMLOUTPATH = App.Path & "XmlOuput.xml"
  Call ReleaseObjects
  Set oDoc = New DOMDocument
  oDoc.resolveExternals = True
  注释: Create processing instruction and document root
  Set oNode = oDoc.createProcessingInstruction("xml", "version=注释:1.0注释:")
  Set oNode = oDoc.insertBefore(oNode, oDoc.childNodes.Item(0))
  注释: Create document root
  Set oRoot = oDoc.createElement("Root")
  Set oDoc.documentElement = oRoot
  oRoot.setAttribute "xmlns:dt", "urn:schemas-microsoft-com:datatypes"
  注释: Add a few simple nodes with different datatypes
  Set oNode = oDoc.createElement("Document")
  oNode.Text = "Demo"
  oRoot.appendChild oNode
  Set oNode = oDoc.createElement("CreateDate")
  oRoot.appendChild oNode
  Set oEle = oNode
  注释: Use DataType so MSXML will validate the data type
  oEle.dataType = "date"
  oEle.nodeTypedValue = Now
  Set oNode = oDoc.createElement("bgColor")
  oRoot.appendChild oNode
  Set oEle = oNode
  注释: Use DataType so MSXML will validate the data type
  oEle.dataType = "bin.hex"
  oEle.Text = &HFFCCCC
  Set oNode = oDoc.createElement("Data")
  oRoot.appendChild oNode
  Set oEle = oNode
  注释: Use DataType so MSXML will validate the data type
  oEle.dataType = "bin.base64"
  注释: Read in the data
  oEle.nodeTypedValue = ReadBinData(DOCINPATH)
  注释: Save xml file

 

[1] [2] 下一页

触屏版 电脑版
3773考试网 琼ICP备12003406号-1