教程中国
PHOTOSHOP CS9.0中文版 MAYA 8.5 FOR WINDOWS Corel Painter v9.0 Flash MX2004 中文版 Illustrator cs2 中文版
VC++6.0含sp6 中英文版 VB6.0 +sp6 简体中文版 Borland Delphi 7汉化版 MSDN for vb6.0中文版 Visual Studio 2005简体
教程中国下属 文件存储共享专家BIBIDU.COM 提供大型软件,教材,源码,电影,音乐,图书等下载 更多精品请点此进入
  您目前所在位置: 教程中国 >> 编程基地 >> VB >> 用vb将word文档(或其他的二进制数据)生成xml文件并互相转换 RSS订阅
用vb将word文档(或其他的二进制数据)生成xml文件并互相转换
教程(视频,书籍)下载:  ASP.NET AutoCAD 数据库 C# ASP java photoshop 网页设计 delphi 3dmax Flash C++ VB 张孝祥 实例   更多请进入BIBIDU搜索
IT搜索引擎   
用vb将word文档(或其他的二进制数据)生成xml文件并互相转换

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的地方


来源:upschool.cn
作者:
关键字:或其,二进制数据
发表日期:2006-12-20 20:46:32

网页显示有限 阅读全文请下载本文完整版WORD文档

上一篇:如何在Visual Basic 中取得变量的内存地址(Address of riables)   下一篇:怎样得到当前的屏幕分辨率?


2009-1-10 9:59:13
本文的相类似文章
  • 用vb将word文档(或其他的二进制数据)生成xml文件并互相转换
  • 二进制数据用异或法加解密过程
  • Windows启动系统时出现*.Vxd或其它文件未找到
  • [VBS]转换二进制数据为字符串常用办法
  • [VBS]转换二进制数据为字符串常用办法
  • 防止别人在QueryString中加入delete或其他字符删除你的数据库内容
  • 在配置使用Membership或其他的Providers的ASP.NET2.0时一定要设置applicationName属性
  • VB.NET操作 SQL SERVER的 二进制数据
  • 将数据库中二进制数据以异步方式写入磁盘
  • Web页面中遍历TextBox(或其它控件)的方法
  • 在学习中进步 在进步中成长 教程中国相随您的成长之路
    华腾联合科技股份有限公司版权所有
    广告联系:Rosibo@163.com