Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 25th, 2013, 05:24 PM
Registered User
 
Join Date: Sep 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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"
 
Old September 26th, 2013, 05:05 AM
Registered User
 
Join Date: Sep 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Solved it!!!

It turns out to be real simple, as I thought ...

You just need to declare the parentnodes explicitely.
Afterwards you can reference them like

parentnodeX.appendchild nodeY

This way you can totally control where a new childnote is put, under which parent.

Plain simple really
 
Old October 8th, 2013, 02:48 PM
Friend of Wrox
 
Join Date: Sep 2010
Posts: 245
Thanks: 5
Thanked 24 Times in 23 Posts
Default

Thanks for posting your solution!
__________________
Boyd Trimmell aka HiTechCoach (.com)
Microsoft Access MVP Alumni 2010-2015
 
Old October 8th, 2013, 02:54 PM
Friend of Wrox
 
Join Date: Sep 2010
Posts: 245
Thanks: 5
Thanked 24 Times in 23 Posts
Default

Thanks for posting your solution!
__________________
Boyd Trimmell aka HiTechCoach (.com)
Microsoft Access MVP Alumni 2010-2015





Similar Threads
Thread Thread Starter Forum Replies Last Post
XML - Extract Data from Child Nodes with different Names hendrik_sa XSLT 6 January 8th, 2009 05:32 AM
How can I display parent & child nodes if XML .... vishnu108mishra ASP.NET 2.0 Basics 0 November 17th, 2007 07:45 AM
Appending child nodes to a RDF/OWL file using xslt sesath XSLT 2 May 10th, 2007 04:37 AM
I cant retrieve XMl child nodes jfergy Classic ASP XML 0 December 8th, 2006 10:24 PM
writing into xml file jerryjohn XML 1 August 18th, 2006 02:40 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.