When I used to introduce XML and try to use Dom, not only did one ask me if I could use Dom to directly generate an XML file out of thin air.
Of course, this is acceptable, followed by how to writeProgramAnd.
Then I am dedicated to this question.ArticleHere is an example of Dom implementation using the COM interface of VB and MSXML:
First of all, I want to explain that the MSXML version I use is ie5, and the version number is 5.0.2919.3800. the interfaces of earlier Microsoft versions are different from those of the new version, let's take a look at her interfaces and descriptions.
If you are not very familiar with VB and COM, it may be difficult to look at the following. However, VB is simpler and clearer than other languages.
First, declare the variables of the following objects:
Dim tempdoc as msmxml. domdocument
Dim tempnode as MSXML. ixmldomnode
Dim tempelement as MSXML. ixmldomelement
Dim tempattribute as MSXML. ixmldomelement
Dim root as MSXML. ixmldomelement
Generate an XML domdocument object
Set tempdoc = new MSXML. domdocument
Generate the root node and set it as the root of the file
Set root = tempdoc. createelement ("myroot ")
Set tempdoc.doc umentelement = root
Generate a child node and add it to the root node, and set an attribute for this node
Set tempnode = tempdoc. createnode (MSXML. node_element, "mynode ","")
Tempnode. Text = "mynodevalue"
Root. appendchild tempnode
Get the element node interface and add attributes
Set tempelement = tempnode
Tempelement. setattribute "myattribute", "myattributevalue"
Write XML files
Open "myxmlfile. xml" for output as #1
Print #1, root. xml
Close #1
The content of the XML file generated by the above program is as follows:
Mynodevalue
In MSXML, non-dom interfaces are also available, which depends on your usage.