用vb將word文檔生成xml文件并互相轉(zhuǎn)換
作者:佚名 來源:本站 時間:2018-07-26 點擊:154次
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
oDoc.save XMLOUTPATH
MsgBox XMLOUTPATH & “ is created for you.”
End Sub
Function ReadBinData(ByVal strFileName As String) As Variant
Dim lLen As Long
Dim iFile As Integer
Dim arrBytes() As Byte
Dim lCount As Long
Dim strOut As String
注釋:Read from disk
iFile = FreeFile()
Open strFileName For Binary Access Read As iFile
lLen = FileLen(strFileName)
ReDim arrBytes(lLen - 1)
Get iFile, , arrBytes
Close iFile
ReadBinData = arrBytes
End Function
Private Sub WriteBinData(ByVal strFileName As String)
Dim iFile As Integer
Dim arrBuffer() As Byte
Dim oNode As IXMLDOMNode
If Not (oDoc Is Nothing) Then
注釋: Get the data
Set oNode = oDoc.documentElement.selectSingleNode(“/Root/Data”)
注釋: Make sure you use a byte array instead of variant
arrBuffer = oNode.nodeTypedValue
注釋: Write to disk
iFile = FreeFile()
Open strFileName For Binary Access Write As iFile
Put iFile, , arrBuffer
Close iFile
End If
End Sub
Private Sub cmdGetBinary_Click()
DOCOUTPATH = App.Path & “DocOutput.doc”
Set oDoc = New DOMDocument
If oDoc.Load(XMLOUTPATH) = True Then
注釋: Save the Doc as another file
WriteBinData DOCOUTPATH
MsgBox DOCOUTPATH & “ is created for you.”
Else
MsgBox oDoc.parseError.reason
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
ReleaseObjects
End Sub
Private Sub ReleaseObjects()
Set oDoc = Nothing
End Sub
4. 建立word文檔DocInput.doc。
5. 保存文檔在工程目錄下。
6. 運(yùn)行程序點擊cmdCreateXML 按鈕。一個 XML 文件XmlOuput.xml 就建立了。
點擊 cmdGetBinary 按鈕就可以生成word文檔 DocOutput.doc。
按照上面的方法,同樣可以將任意的二進(jìn)制數(shù)據(jù)存為xml,然后再重新生成二進(jìn)制數(shù)據(jù)?梢杂糜趙eb傳輸?shù)鹊瓤梢允褂脁mlhttp的地方。
相關(guān)閱讀
更多資訊
- 系統(tǒng)垃圾清理批處理代碼
- 使鼠標(biāo)放到任務(wù)欄右邊的日期欄時怎樣顯示星期
- 二種迅速關(guān)機(jī)辦法
- 一百四十個電腦技巧
- 跟我學(xué)在電腦右下角時間處顯示自己名字
- 7個節(jié)省設(shè)置時間的電腦小技巧
- 磁盤碎片迅速整理辦法
- 小技巧刪除快捷方式圖標(biāo)上的箭頭
- 用批處理方式刪除刪不了的文件
- 掌握快捷鍵,跟我學(xué)成為電腦高手
- 更改MAC地址,給網(wǎng)卡辦新“身份證”
- 繞過管理系統(tǒng)讓你在網(wǎng)吧上網(wǎng)不花錢
- 巧辦法找到誤刪除文件
- 五個辦法關(guān)閉優(yōu)盤自動播放技巧
- 怎么設(shè)置更好的語音效果
- 用鍵盤彈奏鋼琴曲
- 用電視卡啟動
- 在桌面上設(shè)置隱秘文件夾
- 雙擊無法打開盤符及文件夾的原因及處理辦法
- 應(yīng)付刪除不掉的文件的五個辦法
熱門文章
推薦游戲
換一換- 快來消我喵紅包版預(yù)約(暫未上線)-快來消我喵賺錢版預(yù)約v1.0.0
- 冒險戰(zhàn)歌游戲(暫未上線)-冒險戰(zhàn)歌手游預(yù)約v1.0
- 星球破壞王破解版-星球破壞王全模式解鎖版下載v1.3.8
- 烏合之眾正義集結(jié)九游版-烏合之眾正義集結(jié)九游服下載v2.2.0九游端
- 終極大亂斗高爆版(暫未上線)-終極大亂斗高爆服v2.2.2
- 悠唐天下之塔防英雄傳bt版下載v1.0.3
- 閃耀進(jìn)化之路紅包版-閃耀進(jìn)化之路游戲免費(fèi)下載v2.0領(lǐng)紅包版
- 百億總裁手游免費(fèi)下載-百億總裁游戲免費(fèi)下載v1.0
- 戰(zhàn)斗吧龍魂小米版v1.1.1
最新文章
- 人氣排行