• Hàm cơ bản về XML
  • Hàm cơ bản về XML

    maidinhdan > 03-05-20, 12:16 AM

    Đây là những hàm tôi sưu tầm chỉnh sửa lại, các bạn có thể nghiên cứu chi tiết trên web hoặc tại đây: Link học XML

    1. Xuất Table sang file XML:
    Mã PHP:
    Public Sub TableToXML()  'Thuoc Modules: Import-Export XML
        Application.ExportXML acExportTable, "tblCVDen", "d:\xml3.xml"
    End Sub 


    2. Các hàm của tôi Xây dựng: để tương tác với XML (Đã Test chạy ok tất cả)

    Mã PHP:
    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(strPathTrueTrue)
        
        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 XMLversion 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 ModulesHoc-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.DOMDocumentxPath As String)
        Dim foo As IXMLDOMNodeListel  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.TextxNode.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.DOMDocumentxPath As String)
        Dim foo As IXMLDOMNodeListel  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 


    Ngoài các hàm trên mong các bạn chia sẽ thêm nhưng gì còn thiếu.

    Tiếp theo tôi sẽ làm các bài liên quan đến tương tác giữa Access file CSV và TXT.

    Thân mến!
  • RE: Hàm cơ bản về XML

    ongke0711 > 03-05-20, 06:33 PM

    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é.
  • RE: Hàm cơ bản về XML

    thuyyeu99 > 03-05-20, 06:47 PM

    Cái này em thấy hay đấy đã sài window thì bắt buộc phải đụng tới nó rồi
  • RE: Hàm cơ bản về XML

    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é.

    Muốn lắm, nhưng viết thành lời và làm Demo qua file sẽ không hết ý, nên chờ sửa cái mic xong mới được.
    Mà không phải XML không đâu, còn có csv và txt nửa...đó là dự kiến. Còn nhiều hơn sẽ viết rss, html, json...

    Hàm code...có hết chỉ làm còn demo + video sao cho xác để các bạn vận dụng nó nhìu hơn.
  • RE: Hàm cơ bản về XML

    ongke0711 > 03-05-20, 10:55 PM

    (03-05-20, 06:47 PM)thuyyeu99 Đã viết: Cái này em thấy hay đấy đã sài window thì bắt buộc phải đụng tới nó rồi

    Em có ngâm cứu dùng JSON để chuyển dữ liệu thay cho XML trong ứng dụng Client/Server của em không?
  • RE: Hàm cơ bản về XML

    thuyyeu99 > 04-05-20, 07:36 AM

    Có chứ anh hihi. Nhưng không vô sâu lắm

    Em chỉ dùng xml để chuyền lệnh thôi, không nhất thiết phải xml,
    Em còn nhớ có bài anh Pos dùng cấu trúc x|y|x, cũng được miển sao mình linh động xử lý.