Sub exportToXML2(hFileName As String, Optional hReplace As Integer = True)
'Auteur :patricktoulon
Dim vHeaders As Variant
Dim i As Long
Dim c As Long
Dim iNbrLig As Long
Dim iNbrCol As Integer
Dim vColumnValues As Variant
Dim XmlDoc, Postprocc, ROOT, Record, elem, oStream
If pOTools.isFileExists(hFileName) Then
If hReplace <> -1 Then
Err.Raise _
ERROR_MESSAGE, _
ERROR_SOURCE, _
ERROR_MSG & " : Le fichier " & hFileName & " existe déjà"
End If
End If
Close
vColumnValues = getTableCells
iNbrLig = pDataBody.Rows.Count + 1
iNbrCol = pDataBody.Columns.Count
vHeaders = getHeaders()
Set XmlDoc = CreateObject("Microsoft.XMLDOM") 'CREATION D'UN DOMDOCUMENT XML
With XmlDoc
Set ROOT = .appendchild(.createelement(CStr(pNameTS))) 'CREATION DE L'ELEMENT ROOT
'creation et intégration du post processing
Set Postprocc = .createProcessingInstruction("xml", "version=""1.0"" encoding=""UTF-8""") 'creation de l'entete du processing
.InsertBefore Postprocc, .ChildNodes.Item(0) 'insertion parametre processing
'ajout des elements
For i = 2 To iNbrLig
Set Record = ROOT.appendchild(.createelement("RECORD_0" & i - 1))
For c = 1 To iNbrCol
Set elem = Record.appendchild(.createelement(pDataBody.Cells(1, c).Offset(-1)))
elem.Text = pDataBody.Cells(1, c)
Next
Next
'arrivé ici on a le code xml en format zip (tout accroché)
Set oStream = CreateObject("ADODB.Stream")
oStream.Charset = "utf-8"
oStream.Open
oStream.WriteText IndenterXMLCode(XmlDoc.XML)'on passe par la fonction d'indentation pour l'ecriture avec indentation
oStream.SaveToFile hFileName, 2
End With
End Sub
'version 2 en memoire
'Fonction basique minimale pour indenter un code xml(issue de creatorribbonx (patricktoulon))
Public Function IndenterXMLCode(ByVal vDomOrString As Variant) As String
Dim XMLWriter As Object ' MSXML2.MXXMLWriter
On Error GoTo QH
Set XMLWriter = CreateObject("MSXML2.MXXMLWriter")
XMLWriter.indent = True 'ajoute l'attribut indent
With CreateObject("MSXML2.SAXXMLReader")
Set .contentHandler = XMLWriter
.Parse vDomOrString
End With
IndenterXMLCode = Replace(Replace(XMLWriter.output, "UTF-16", "UTF-8"), "standalone=""no""", "")
Exit Function
QH:
End Function