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.
Is there anyone who can help? Thank you.
>Solution :
- Create XML declaration with
createProcessingInstruction - Note:
UTF-8is 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