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
Set oNode = oDoc.createProcessingInstruction("xml", "version='1.0'")
Set oNode = oDoc.insertBefore(oNode, oDoc.childNodes.Item(0))
Set oRoot = oDoc.createElement("Root")
Set oDoc.documentElement = oRoot
oRoot.setAttribute "xmlns:dt", "urn:schemas-microsoft-com:datatypes"
Set oNode = oDoc.createElement("Document")
oNode.Text = "Demo"
oRoot.appendChild oNode
Set oNode = oDoc.createElement("CreateDate")
oRoot.appendChild oNode
Set oEle = oNode
oEle.dataType = "date"
oEle.nodeTypedValue = Now
Set oNode = oDoc.createElement("bgColor")
oRoot.appendChild oNode
Set oEle = oNode
oEle.dataType = "bin.hex"
oEle.Text = &HFFCCCC
Set oNode = oDoc.createElement("Data")
oRoot.appendChild oNode
Set oEle = oNode
oEle.dataType = "bin.base64"
oEle.nodeTypedValue = ReadBinData(DOCINPATH)
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
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
Set oNode = oDoc.documentElement.selectSingleNode("/Root/Data")
arrBuffer = oNode.nodeTypedValue
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
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
Sunday, June 17, 2012
VB6 Code - XML Yang Mengandung Binary Data
Labels:
XML-VB6