Access
A family of Microsoft relational database management systems designed for ease of use.
395 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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 :
I would like the display to contain indents and line breaks like the following result :
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
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)