maidinhdan > 03-05-20, 12:16 AM
Public Sub TableToXML() 'Thuoc Modules: Import-Export XML
Application.ExportXML acExportTable, "tblCVDen", "d:\xml3.xml"
End Sub
Public Sub TaoFileXML2()
Dim rst As Recordset
Dim strSQL As String
strSQL = "SELECT SoTT, Ngayden, Soden, Tacgia FROM tblCVDen"
Set rst = CurrentDb.OpenRecordset(strSQL)
Dim strPath As String
strPath = CurrentProject.path & "\CVDenXML.xml"
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim objFile As Object
Set objFile = fso.CreateTextFile(strPath, True, True)
objFile.Write "<?xml version='1.0'?>" & vbCrLf
objFile.Write "<TableCongVanDen>" & vbCrLf
Do Until rst.EOF
objFile.Write vbTab & "<Dong SoTT=" & """" & rst.Fields("SoTT") & """" & ">" & vbCrLf
objFile.Write vbTab & vbTab & "<Ngayden>" & rst.Fields("Ngayden") & "</Ngayden>" & vbCrLf
objFile.Write vbTab & vbTab & "<Soden>" & rst.Fields("Soden") & "</Soden>" & vbCrLf
objFile.Write vbTab & vbTab & "<Tacgia>" & rst.Fields("Tacgia") & "</Tacgia>" & vbCrLf
objFile.Write vbTab & "</Dong>" & vbCrLf
rst.MoveNext
Loop
objFile.Write "</TableCongVanDen>"
rst.Close
Set rst = Nothing
Set objFile = Nothing
Set fso = Nothing
MsgBox "Complete"
End Sub
Function DocNoiDungXML()
' Khai bao bien: Kieu ket noi muon
'Dim objParser As Object
'Set objParser = CreateObject("Microsoft.XMLDOM") ' or Set objParser = Server.CreateObject( "Microsoft.XMLDOM" )
'or 'Dang ky them thu vien: Microsoft XML, version 3.0 , provided in Msxml.dll
' Khai bao bien: Kieu ket noi som
Dim xDoc As MSXML2.DOMDocument
Set xDoc = New MSXML2.DOMDocument
Dim strPath As String
strPath = CurrentProject.path & "\CVDenXML.xml"
If xDoc.Load(strPath) Then
DisplayNode xDoc.childNodes, 0
Else
MsgBox "Load file that bai"
' Hien thi ma loi
Dim strErrText As String
Dim xPE As MSXML2.IXMLDOMParseError
' Lay doi tuong ParseError
Set xPE = xDoc.parseError
With xPE
strErrText = "Tai lieu XML cua ban khong Load len duoc" & _
"Do loi sau:" & vbCrLf & _
"Loi #: " & .errorCode & ": " & xPE.reason & _
"Dong #: " & .line & vbCrLf & _
"Vi tri dong: " & .linepos & vbCrLf & _
"Vi tri trong File: " & .filepos & vbCrLf & _
"Nguon van ban: " & .srcText & vbCrLf & _
"Document URL: " & .url
End With
MsgBox strErrText, vbExclamation
Set xPE = Nothing
End If
Set xDoc = Nothing
End Function
Sub DisplayNode(ByRef Nodes As MSXML2.IXMLDOMNodeList, ByVal Indent As Integer)
Dim xNode As MSXML2.IXMLDOMNode
Indent = Indent + 2
For Each xNode In Nodes
If xNode.nodeType = NODE_TEXT Then
Debug.Print Space(Indent) & xNode.parentNode.nodeName & ": " & xNode.nodeValue
End If
If xNode.hasChildNodes Then
DisplayNode xNode.childNodes, Indent
End If
Next xNode
End Sub
Public Sub XoaNode() 'Thuoc Modules: Hoc-Import-Export XM2
Dim xDoc As MSXML2.DOMDocument
Dim strPath As String
Dim strSearch As String
strSearch = 2
strPath = CurrentProject.path & "\CVDenXML.xml"
Set xDoc = New MSXML2.DOMDocument
xDoc.validateOnParse = False
xDoc.Load strPath
DeleteNodes xDoc, "//TableCongVanDen/Dong[@SoTT='" & strSearch & "']"
xDoc.Save strPath ''save new document
Set xDoc = Nothing
End Sub
Sub DeleteNodes(xDoc As MSXML2.DOMDocument, xPath As String)
Dim foo As IXMLDOMNodeList, el As IXMLDOMElement
Set foo = xDoc.selectNodes(xPath)
If foo.Length > 0 Then Debug.Print "[Xoa Node: " & xPath
For Each el In foo
el.parentNode.removeChild el
Next el
Set foo = Nothing
End Sub
Public Sub TimNoiDungFileXML()
Dim xDoc As New MSXML2.DOMDocument30
Dim xNode As MSXML2.IXMLDOMElement
Dim strPath As String
Dim strSearch As String
strSearch = 14
strPath = CurrentProject.path & "\CVDenXML.xml"
'Try To Load The Document
With xDoc
If Not .Load(strPath) Then
MsgBox "Unable to load"
Exit Sub
End If
Set xNode = .selectSingleNode("//TableCongVanDen/Dong/Soden[.='" & strSearch & "']")
' Set xNode = .selectSingleNode("//Categories/Ngayden[.='08/12/2011']")
If Not xNode Is Nothing Then
Debug.Print xNode.Text, xNode.nodeName
End If
End With
Set xNode = Nothing
Set xDoc = Nothing
End Sub
Public Sub SuaNoiDungXML()
Dim xDoc As New MSXML2.DOMDocument30
Dim xNode As MSXML2.IXMLDOMElement
Dim strSearch As String
strSearch = "222222"
Dim strPath As String
strPath = CurrentProject.path & "\CVDenXML.xml"
'Try To Load The Document
With xDoc
If Not .Load(strPath) Then
MsgBox "Unable to load"
Exit Sub
End If
Set xNode = .selectSingleNode("//TableCongVanDen/Dong/Soden[.='" & strSearch & "']")
' Set xNode = .selectSingleNode("//Categories/Ngayden[.='08/12/2011']")
If Not xNode Is Nothing Then
EditNodes xDoc, "//TableCongVanDen/Dong/Soden[.='" & strSearch & "']"
End If
End With
xDoc.Save strPath
Set xNode = Nothing
Set xDoc = Nothing
End Sub
Sub EditNodes(xDoc As MSXML2.DOMDocument, xPath As String)
Dim foo As IXMLDOMNodeList, el As IXMLDOMElement
Set foo = xDoc.selectNodes(xPath)
If foo.Length > 0 Then Debug.Print "[Sua NoiDung Node: " & xPath
For Each el In foo
el.Text = "aaaa"
Next el
Set foo = Nothing
End Sub
ongke0711 > 03-05-20, 06:33 PM
thuyyeu99 > 03-05-20, 06:47 PM
maidinhdan > 03-05-20, 09:45 PM
(03-05-20, 06:33 PM)ongke0711 Đã viết: Dân nên giới thiệu thêm về XML và các ứng dụng của việc chuyển đổi dữ liệu sang XML để mọi người hiểu thêm các lợi ích, tầm quan trọng của nó như thế nào nhé.
thuyyeu99 > 04-05-20, 07:36 AM