How to add an indent and a newline for each tag for DOMDocument60 created in VBA.

jacky Perpète 41 Reputation points
2023-08-19T19:27:39.6766667+00:00

Hello,

When I create an XML document in VBA, if I display the content of the document using the xml property of the document, I get the following result :

Result

I would like the display to contain indents and line breaks like the following result :

Result1

Is there a possibility with DOMDocument60?

Here is my test VBA code.

Private Sub Commande0_Click()
Dim xmlDomDoc As New DOMDocument60, newxmlDomDoc As New DOMDocument60
Dim nodeParent As IXMLDOMElement
Dim nodeControl As IXMLDOMElement
Dim codexml As String
Const nameSpace As String = "http://Essai"

xmlDomDoc.SetProperty "SelectionNamespaces", "xmlns:ns='" & nameSpace & "'"

Set nodeControl = xmlDomDoc.createNode(NODE_ELEMENT, "Essai", nameSpace)
nodeControl.setAttribute "key", "1"
xmlDomDoc.appendChild nodeControl

Set nodeControl = xmlDomDoc.createNode(NODE_ELEMENT, "Essai1", nameSpace)
nodeControl.setAttribute "key", "2"
Set nodeParent = xmlDomDoc.SelectSingleNode("//ns:*[@key=1]")
nodeParent.appendChild nodeControl

Set nodeControl = xmlDomDoc.createNode(NODE_ELEMENT, "Essai2", nameSpace)
nodeControl.setAttribute "key", "3"
Set nodeParent = xmlDomDoc.SelectSingleNode("//ns:*[@key=2]")
nodeParent.appendChild nodeControl

codexml = xmlDomDoc.XML

End Sub

Access
Access
A family of Microsoft relational database management systems designed for ease of use.
395 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,933 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. jacky Perpète 41 Reputation points
    2023-08-20T14:11:47.1+00:00

    Hello,

    After long research on the net, here is a simple solution.

    Here is the code:

    Function AddindentStringXML(xml As String) As String
    
    'Crée un nouveau document avec un retrait pour les balises xml
    'xml: chaine de caractères du code XML
    'Retour: chaine de caractères du code XML avec un retrait pour les balises
    
    Dim writer As New MSXML2.MXXMLWriter60
    Dim reader As New MSXML2.SAXXMLReader60
    Dim newDomDoc As New MSXML2.DOMDocument60
    
    'Omet la déclaration xml
    writer.omitXMLDeclaration = True
    
    'Active l'ajoute d'un retrait aux balises
    writer.indent = True
    
    'Défini le gestionnaire de contenu et passe le code XML
    Set reader.contentHandler = writer
    reader.parse xml
    
    'Charge le code xml avec les retraits dans un nouveau document
    newDomDoc.LoadXML writer.output
    
    'Renvoie le code XML du nouveau document
    AddindentStringXML = newDomDoc.xml
    
    End Function
    

    Call of the function with the value of the xml code and in return a string with the addition of indents and line breaks.

    Dim codeXML As String
    
    codeXML = AddindentStringXML(xmlDomDoc.xml)
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.