求学快递网是国内领先的B2M2C教育培训网上信息平台!

首页 | 分站加盟 | 课程发布 | 最新课程 | 最新机构 | 培训专题 | 全国分站 | 帮助中心

计算机等级考试_计算机等级考试报名_计算机考试成绩查询_求学快递网

计算机等级考试一步到位
您的位置:求学快递网 > 计算机等级考试 > 应试技巧 > 如何用VB将word文档生成xml文件并互相转换 - 正文内容

如何用VB将word文档生成xml文件并互相转换

如何用VB将word文档生成xml文件并互相转换

 

求学快递网计算机等级考试专题重点推荐:2016年计算机等级考试报考时间 计算机等级考试证书领取时间2016年计算机等级考试报考指南2016年计算机等级考试大纲2016年计算机等级考试政策2016年计算机等级考试报考条件2016年计算机等级考试科目等最新新闻资讯。

 

    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 Doc = New DOMDocument

    oDoc.resolveExternals = True

    注释: Create processing instruction and document root

    Set Node = oDoc.createProcessingInstruction("xml", "version=注释:1.0注释:")

    Set Node = oDoc.insertBefore(oNode, oDoc.childNodes.Item(0))

    注释: Create document root

    Set Root = 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 Node = oDoc.createElement("Document")

    oNode.Text = "Demo"

    oRoot.appendChild oNode

    Set Node = oDoc.createElement("CreateDate")

    oRoot.appendChild oNode

    Set Ele = oNode

    注释: Use DataType so MSXML will validate the data type

    oEle.dataType = "date"

    oEle.nodeTypedValue = Now

    Set Node = oDoc.createElement("bgColor")

    oRoot.appendChild oNode

    Set Ele = oNode

    注释: Use DataType so MSXML will validate the data type

    oEle.dataType = "bin.hex"

    oEle.Text = &HFFCCCC

    Set Node = oDoc.createElement("Data")

    oRoot.appendChild oNode

    Set Ele = 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 Aclearcase/" target="_blank" >ccess 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 Node = 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 Doc = 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 Doc = Nothing

    End Sub

    4. 建立word文档DocInput.doc.

    5. 保存文档在工程目录下

    6. 运行程序点击cmdCreateXML 按钮.一个 XML 文件XmlOuput.xml 就建立了.

    点击 cmdGetBinary 按钮就可以生成word文档 DocOutput.doc.

    按照上面的方法,同样可以将任意的二进制数据存为xml,然后再重新生成二进制数据

    可以用于web传输等等可以使用xmlhttp的地方

 

 

 

 

关注求学快递网计算机等级考试专题,帮你了解最新的计算机等级考试资讯2016年计算机等级考试成绩查询,让你轻松备考计算机等级考试!

求学快递网计算机等级考试专题的小编们根据网民搜索喜爱,第一时间公布2016年计算机一级考试报名网址2016年计算机二级考试模拟试题2016年计算机三级考试历年真题2016年计算机四级考试复习资料供广大求学爱好者学习使用,欢迎继续关注求学快递网计算机等级考试专题,并给我们提供宝贵的意见!

 

相关推荐

计算机等级考试最新机试技巧
计算机等级考试高效利用技巧

最新计算机资讯

热点专题

计算机等级考试经验技巧分享

关于本站  诚征英才  广告服务  招生服务  免责声明  分站加盟  VIP会员  联系我们  网站地图  网站记事

版权所有 CopyRight 2008-2017 粤ICP备14009694号 www.studyems.com, Inc. All Rights Reserved
违法和不良信息举报邮箱:2881796407@qq.com 举报电话: