使用具有属性的 XML 个元素将 Excelsheet 转换为 XML 文件

Converting Excelsheet into XML file with XML elements having attributes

我是 VBA 的新手,想在 excel 上使用 VBA 并编写一个宏来每行导出一个 xml 文件(参见打印中的示例屏幕):Excel Example

Account 应该是 XML 元素,transactionID 应该是 XML 属性 账户.

第 1 行 Excel 转换为 XML 文件的示例:Example XML file

到目前为止,我有以下 VBA 代码将 Excel 按行转换为 xml 文件:

Sub test2XLStoXML()
 sTemplateXML = _
        "<?xml version='1.0'?>" + vbNewLine + _
        "<Account transactionId='???'>" + vbNewLine + "</Account>"
 
 Debug.Print sTemplateXML
 
 Set doc = CreateObject("MSXML2.DOMDocument")
 doc.async = False
 doc.validateOnParse = False
 doc.resolveExternals = False

 With ActiveWorkbook.Worksheets(1)
  lLastRow = .UsedRange.Rows.Count

  For lRow = 2 To 7
   sFile = "/Users/user/Documents/" & .Cells(lRow, 1).Value & ".xml"
   
   Dim sAccount As String
   Dim sTransactionId As String
   
   sAccount = CStr(.Cells(lRow, 2).Value)
   sTransactionId = CStr(.Cells(lRow, 3).Value)
   
   doc.LoadXML sTemplateXML
   doc.getElementsByTagName("Account")(0).appendChild doc.createTextNode(sAccount)
   doc.getElementsByTagName("Account")(0).appendChild doc.create???(sTransactionId)
   
   doc.Save sFile
  Next

 End With
End Sub

但我不知道如何编码属性“transactionID”将从 excel 中获取动态值。

很高兴能得到一些帮助。

提前致谢。

像这样的东西会起作用:

Option Explicit

Sub test2XLStoXML()

    Dim doc As Object, root As Object, sTemplateXML As String, sFile As String
    Dim sAccount As String
    Dim sTransactionId As String, el, el2, att, lRow As Long
    
    With ActiveWorkbook.Worksheets(1)
      
        For lRow = 2 To .Cells(.Rows.Count, 1).End(xlUp).Row
        
            sFile = "/Users/user/Documents/" & .Cells(lRow, 1).Value & ".xml"
            sAccount = CStr(.Cells(lRow, 2).Value)
            sTransactionId = CStr(.Cells(lRow, 3).Value)
            
            With EmptyDoc 'moved XML doc creation to separate function
                
                .appendchild .createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'")
                'root node with attribute
                Set el = .createnode(1, "Account", "")
                Set att = .CreateAttribute("transactionId")
                att.Value = sTransactionId
                el.Attributes.setnameditem att
                'child "val" node
                Set el2 = .createnode(1, "val", "")
                el2.appendchild .createtextnode(sTransactionId)
                el.appendchild el2
                .appendchild el
                
                'Debug.Print .XML
                .Save sFile
            End With
            
        Next lRow
    End With
     
End Sub

'create and return an empty XML document
Function EmptyDoc() As Object
    Dim doc As Object
    Set doc = CreateObject("MSXML2.DOMDocument")
    doc.async = False
    doc.validateOnParse = False
    doc.resolveExternals = False
    Set EmptyDoc = doc
End Function

编辑:在 Mac 上创建和写出 XML 为文本可能更容易 -

Sub test2XLStoXMLString()

    Dim xml As String, sAccount As String
    Dim sTransactionId As String, lRow As Long, sFile As String
    
    With ActiveWorkbook.Worksheets(1)
      
        For lRow = 2 To .Cells(.Rows.Count, 1).End(xlUp).Row
            sFile = "/Users/user/Documents/" & .Cells(lRow, 1).Value & ".xml"
            sAccount = CStr(.Cells(lRow, 2).Value)
            sTransactionId = CStr(.Cells(lRow, 3).Value)
            
            xml = "<?xml version=""1.0""?><Account transactionId=""{tId}""><val>{acct}</val></Account>"
            xml = Replace(xml, "{tId}", sTransactionId)
            xml = Replace(xml, "{acct}", sAccount)
            SaveToFile sFile, xml
            'Debug.Print xml
        Next lRow
    End With
     
End Sub

Sub SaveToFile(fPath As String, sContent As String)
    Dim ff
    ff = FreeFile
    Open fPath For Output As #ff
    Print #ff, sContent
    Close #ff
End Sub