Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to insert a tag before the root in XML with VBA

I developed the following code

    Set UBLdoc = CreateObject("MSXML2.DOMDocument")
    Set root = UBLdoc.createElement("StandardBusinessDocument")
    
' Set the xmlns attribute for the root element
    root.setAttribute "xmlns", "http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader"

' Create the StandardBusinessDocumentHeader element
    Set elem = UBLdoc.createElement("StandardBusinessDocumentHeader")
' Append the StandardBusinessDocumentHeader element to the root element
    root.appendChild elem
...
' Append the root element to the document
    UBLdoc.appendChild root

that correctly generates the following XML.

<StandardBusinessDocument xmlns="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader">
    <StandardBusinessDocumentHeader>
        <HeaderVersion>1.0</HeaderVersion>
        <Sender>
            <Identifier Authority="iso6523-actorid-upis">0211:IT09229211009</Identifier>
        </Sender>
        <Receiver>
            <Identifier Authority="iso6523-actorid-upis">9934:27788012253</Identifier>
        </Receiver>
...
    </StandardBusinessDocumentHeader>
...
</StandardBusinessDocument>

I need to insert the tag.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
before the <StandardBusinessDocument> tag. I am afraid I don’t understand the exact meaning, but it is necessary for the XML to be accepted.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there anyone who can help? Thank you.

>Solution :

  • Create XML declaration with createProcessingInstruction
  • Note: UTF-8 is default encoding, so the declaration will be <?xml version="1.0" standalone="no"?>
Sub CreateXML()
    ' Create XML document
    Set ubldoc = CreateObject("MSXML2.DOMDocument")
    
    ' Create XML declaration and append it to the document
    Set xmlDeclaration = ubldoc.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8' standalone='no'")
    ubldoc.appendChild xmlDeclaration
    
    ' Create root element
    Set Root = ubldoc.createElement("StandardBusinessDocument")
    
    ' Set the xmlns attribute for the root element
    Root.setAttribute "xmlns", "http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader"

    ' Create the StandardBusinessDocumentHeader element
    Set elem = ubldoc.createElement("StandardBusinessDocumentHeader")
    
    ' Append the StandardBusinessDocumentHeader element to the root element
    Root.appendChild elem

    ' Append the root element to the document
    ubldoc.appendChild Root
    
    ' Display the XML document
    Debug.Print ubldoc.XML
End Sub

Microsoft documentation:

XmlDocument.CreateProcessingInstruction(String, String) Method

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading