• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
    • 軟件測試技術
    • 軟件測試博客
    • 軟件測試視頻
    • 開源軟件測試技術
    • 軟件測試論壇
    • 軟件測試沙龍
    • 軟件測試資料下載
    • 軟件測試雜志
    • 軟件測試人才招聘
      暫時沒有公告

    字號: | 推薦給好友 上一篇 | 下一篇

    用vb將word文檔生成xml文件并互相轉換

    發布: 2007-7-14 20:28 | 作者: 佚名    | 來源: 網絡轉載     | 查看: 12次 | 進入軟件測試論壇討論

    領測軟件測試網 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. 運行程序點擊cmdCreateXML 按鈕.一個 XML 文件XmlOuput.xml 就建立了.
    點擊 cmdGetBinary 按鈕就可以生成word文檔 DocOutput.doc.


    按照上面的方法,同樣可以將任意的二進制數據存為xml,然后再重新生成二進制數據

    可以用于web傳輸等等可以使用xmlhttp的地方

    延伸閱讀

    文章來源于領測軟件測試網 http://www.kjueaiud.com/


    關于領測軟件測試網 | 領測軟件測試網合作伙伴 | 廣告服務 | 投稿指南 | 聯系我們 | 網站地圖 | 友情鏈接
    版權所有(C) 2003-2010 TestAge(領測軟件測試網)|領測國際科技(北京)有限公司|軟件測試工程師培訓網 All Rights Reserved
    北京市海淀區中關村南大街9號北京理工科技大廈1402室 京ICP備2023014753號-2
    技術支持和業務聯系:info@testage.com.cn 電話:010-51297073

    軟件測試 | 領測國際ISTQBISTQB官網TMMiTMMi認證國際軟件測試工程師認證領測軟件測試網

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>