Hello

I am writing a vb code that will generate xml files. I am making some progress, but I am stuck. When I run the vb program, the MessageDetails node is not displayed, eventhough it is contained in the vb code. What am I doing wrong? Below is the vb code

'This sample application creates a simple, but complete, XML DOM object,
'with <root> as the document element. This element contains three child elements:
'<node1>, <node2>, and <node3>. The first child element contains character data.
'The second child element contains a CDATA section. The last child element contains
'three empty child elements: <subnode1>, <subnode2>, and <subnode3>.

 Private Function CreateDOM()
    Dim dom
    Set dom = New DOMDocument30
    dom.async = False
    dom.validateOnParse = False
    dom.resolveExternals = False
    dom.preserveWhiteSpace = True
    Set CreateDOM = dom
End Function

Private Sub Form_Load()
    Dim dom, node, attr
    Dim DefaultNS As String
    
    On Error GoTo ErrorHandler

    Set dom = CreateDOM
    
    ' Create a processing instruction targeted for xml.
    Set node = dom.createProcessingInstruction("xml", "version='1.0'")
    dom.appendChild node
    Set node = Nothing
    
    ' Create a processing instruction targeted for xml-stylesheet.
    Set node = dom.createProcessingInstruction("xml-stylesheet", _
                                "type='text/xml' href='test.xsl'")
    dom.appendChild node
    Set node = Nothing
    
    ' Create a comment for the document.
    Set node = dom.createComment("sample xml file created using XML DOM object.")
    dom.appendChild node
    Set node = Nothing
    
    
    
    DefaultNS = "http://www.govtalk.gov.uk/CM/envelope"
    
    ' Create the root element.
    Dim RootElement
    Set RootElement = dom.createNode(NODE_ELEMENT, "GovTalkMessage", DefaultNS)
     Set node = dom.createNode(NODE_ELEMENT, "EnvelopeVersion", DefaultNS)

    ' Add the root element to the DOM instance.
    dom.appendChild RootElement
    ' Insert a newline + tab.
    RootElement.appendChild dom.createTextNode(vbNewLine + vbTab)
    ' Create and add more nodes to the root element just created.
    ' Create a text element.
   ' Set node = dom.createElement("node1")
    node.Text = "2.0"
    ' Add text node to the root element.
    RootElement.appendChild node
    Set node = Nothing
      ' Add a newline plus tab.
    RootElement.appendChild dom.createTextNode(vbNewLine + vbTab)
    
    ' Create an element to hold a CDATA section.
    'Set node = dom.createElement("node2")
    Set node = dom.createNode(NODE_ELEMENT, "Header", DefaultNS)

    Set cd = dom.createCDATASection("<some mark-up text>")
    node.appendChild cd
    Set cd = Nothing
    dom.documentElement.appendChild node
    
    Set node = dom.createNode(NODE_ELEMENT, "MessageDetails", DefaultNS)
    
    Set node = Nothing
      ' Add a newline plus tab.
    RootElement.appendChild dom.createTextNode(vbNewLine + vbTab)
    
    ' Create an element to hold three empty subelements.
  '  Set node = dom.createElement("node3")
    Set node = dom.createNode(NODE_ELEMENT, "node3", DefaultNS)
   
    RootElement.appendChild node
       ' Add a newline.
    RootElement.appendChild dom.createTextNode(vbNewLine)
    Set node = Nothing
    
    ' Save the XML document to a file.
    dom.save App.Path + "\dynamDom.xml"
    Set RootElement = Nothing
    Set dom = Nothing
    Exit Sub
    
ErrorHandler:
    MsgBox Err.Description
End Sub

below is the generated XML

<?xml version="1.0"?>
<?xml-stylesheet type='text/xml' href='test.xsl'?>
<!--sample xml file created using XML DOM object.-->
<GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope">
	<EnvelopeVersion>2.0</EnvelopeVersion>
	<Header><![CDATA[<some mark-up text>]]></Header>
	<node3/>
</GovTalkMessage>

the messageDetails element is missing

Member Avatar for gravyboat

You set your node to "nothing" before you append it:

Set node = dom.createNode(NODE_ELEMENT, "MessageDetails", DefaultNS)
    
    Set node = Nothing
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.