' This procedure creates XML document
' and saves it to disk.
' Requires msxml.dll (Go to Project --> References and
' and choose Microsoft XML version 2.0, or whatever the
' current version you have installed)
' The example given below will write the following XML
' documents.
'
' <Family>
' <Member Relationship="Father">
' <Name>Some Guy</Name>
' </Member>
' </Family>
'
'but it should be clear how to modify the code
'to create your own documents
Private Sub Create_XML()
Dim objDom As DOMDocument
Dim objRootElem As IXMLDOMElement
Dim objMemberElem As IXMLDOMElement
Dim objMemberRel As IXMLDOMAttribute
Dim objMemberName As IXMLDOMElement
Set objDom = New DOMDocument
' Creates root element
Set objRootElem = objDom.createElement("Family")
objDom.appendChild objRootElem
' Creates Member element
Set objMemberElem = objDom.createElement("Member")
objRootElem.appendChild objMemberElem
' Creates Attribute to the Member Element
Set objMemberRel = objDom.createAttribute("Relationship")
objMemberRel.nodeValue = "Father"
objMemberElem.setAttributeNode objMemberRel
' Create element under Member element, and
' gives value "some guy"
Set objMemberName = objDom.createElement("Name")
objMemberElem.appendChild objMemberName
objMemberName.Text = "Some Guy"
' Saves XML data to disk.
objDom.save ("c:\temp\andrew.xml")
End Sub
Refactorings
No refactoring yet !
Ants
April 27, 2010, April 27, 2010 23:12, permalink
Why instantiate a whole DOM from the imported MSXML, when you can go cheap and use the CLR's XmlTextWriter?
See this article in MSDN: http://msdn.microsoft.com/en-us/library/wkee9k2s(VS.71).aspx
jbrain.myopenid.com
April 28, 2010, April 28, 2010 19:52, permalink
Oh! Thank you very much for the tip, I'm a novice programmer. I was tasked with creating xml-files based on the data tables in MS Access. I'll try to do this with XmlTextWriter.
jbrain.myopenid.com
April 28, 2010, April 28, 2010 20:47, permalink
@Ants But is is possible to use the XmlTextWriter in the project for MS Access to VBA?
Ants
April 29, 2010, April 29, 2010 09:14, permalink
Your original question was tagged as VB.NET and you made no mention in your description that you needed to do this from VBA.
Anyway, the answer is yes, you can use XmlTextWriter from VBA. See http://msdn.microsoft.com/en-us/library/aa140276(office.10).aspx for an example of using VBA in Word to use the XmlTextWriter. All of Office uses the same VBA code so it should work in Access.
Red
February 17, 2011, February 17, 2011 00:22, permalink
I am trying to format my xml and was having trouble understanding how to do that with this code?!?!?! can you help?
Thx
Red
Ants
February 17, 2011, February 17, 2011 17:37, permalink
Read here how to do the formatting: http://msdn.microsoft.com/en-us/library/6ts85e64.aspx
web developer
February 24, 2011, February 24, 2011 10:29, permalink
Hi,
I found that Andoid application can totally managed using xml specifically for the data driven application having pre-defined database which is being displayed on request. This xml format can be used in that application?
Test