When I was introducing XML and asking others to try to use DOM, more than one person asked me if using DOM can be used to generate an XML file out of thin air.
Of course, this is certainly possible, and the second is how to write the program.
Then I will give an example of this question specifically in this article using the DOM implementation of the COM interface of VB and MSXML:
First of all, I want to make it clear that the MSXML version I use is equipped with IE5, with the version number of 5.0.2919.3800. The interfaces of Microsoft's early versions are somewhat different from those of the new ones, so when programming by yourself, you should check out its interface and instructions.
If you are not familiar with vb and COM, it may be more difficult to read the following ones, but VB should be simpler and clearer than other language implementations.
First declare the following variables of the objects to be used:
Dim tempdoc As
Dim tempnode As
Dim tempelement As
Dim tempattribute As
Dim root As
Generate an XML DOMDocument object
Set tempdoc = New
Generate the root node and set it as the root of the file
Set root = ("MyRoot")
Set = root
Generate the child node and add it to the root node and set a property for this node.
Set tempnode = (MSXML.NODE_ELEMENT, "MyNode", "")
= "MyNodeValue"
tempnode
Get the interface of the element node and add attributes
Set tempelement = tempnode
"MyAttribute", "MyAttributeValue"
Write an xml file
Open "" for output as #1
Print #1,
Close #1
The following is the XML file generated by the above program:
MyNodeValue
There are also non-DOM interfaces that can be used in MSXML, which depends on your own usage.