Writing XML file using msXML2 - child nodes
Hi there,
I'm trying to write an XML file (from VBA code) that looks like this ...
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:product xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns1:version>2.0</ns1:version>
<ns1:energyType>electricity</ns1:energyType>
<ns1:isAllIn>false</ns1:isAllIn>
<ns1:allCostsKnown>true</ns1:allCostsKnown>
<ns1:pricing>
<ns1:validityBegin>2013-09-05</ns1:validityBegin>
<ns1:validityEnd>2013-10-07</ns1:validityEnd>
<ns1:consumptionLevel1>
<ns1:annualRange>
<ns1:min>0</ns1:min>
<ns1:max>50000</ns1:max>
</ns1:annualRange>
etc ...
The problem is that I always end up with something that looks like this.
It closes the "ns1:pricing" node too early.
I have no clue of what I'm doing wrong. Please help.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:product xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns1:version>2.0</ns1:version>
<ns1:energyType>electricity</ns1:energyType>
<ns1:isAllIn>false</ns1:isAllIn>
<ns1:allCostsKnown>true</ns1:allCostsKnown>
<ns1:pricing>
<ns1:validityBegin>2013-09-05</ns1:validityBegin>
<ns1:validityEnd>2013-10-07</ns1:validityEnd>
</ns1:pricing>
<ns1:consumptionLevel1>
<ns1:annualRange>
<ns1:min>0</ns1:min>
<ns1:max>50000</ns1:max>
</ns1:annualRange>
etc ...
My code looks like this ...
Dim Doc As MSXML2.DOMDocument
Dim Node As MSXML2.IXMLDOMNode
Dim Root As MSXML2.IXMLDOMElement
Dim Elem As MSXML2.IXMLDOMElement
Set Doc = New DOMDocument
Doc.resolveExternals = True
Set Node = Doc.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8' standalone='yes'")
Set Node = Doc.insertBefore(Node, Doc.childNodes.Item(0))
Set Root = Doc.createElement("ns1:product")
Set Doc.documentElement = Root
Root.setAttribute "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"
Set Elem = Doc.createElement("ns1:version")
Elem.Text = "2.0"
Doc.documentElement.appendChild Elem
Set Elem = Doc.createElement("ns1:energytype")
Elem.Text = "electricity"
Doc.documentElement.appendChild Elem
Set Elem = Doc.createElement("ns1:isAllIn")
Elem.Text = "false"
Doc.documentElement.appendChild Elem
Set Elem = Doc.createElement("ns1:allCostsKnown")
Elem.Text = "true"
Doc.documentElement.appendChild Elem
Set Elem = Doc.createElement("ns1:pricing")
Doc.documentElement.appendChild Elem
Elem.appendChild(Doc.createElement("ns1:validityBe gin")).Text = _
rst("ValidFromYYYY") & "-" & Format(rst("ValidFromMM"), "00") & "-" & Format(rst("ValidFromDD"), "00")
Elem.appendChild(Doc.createElement("ns1:validityEn d")).Text = _
rst("ValidToYYYY") & "-" & Format(rst("ValidToMM"), "00") & "-" & Format(rst("ValidToDD"), "00")
Set Elem = Doc.createElement("ns1:consumptionLevel1")
Doc.documentElement.appendChild Elem
Elem.appendChild(Doc.createElement("ns1:min")).Tex t = "0"
Elem.appendChild(Doc.createElement("ns1:max")).Tex t = "50000"
Set Elem = Doc.createElement("ns1:splitPricing")
Elem.Text = "false"
Doc.documentElement.appendChild Elem
Doc.Save "c:\users\wim\desktop\" & rst("ProductCode") & ".xml"
|