Creating XML using the DOM

Source: Internet
Author: User
Tags interface version
Dom|xml| created I used to introduce XML and let others try to use DOM, and more than one person asked me whether Dom can be used to generate an XML file directly from scratch.

Of course, this is certainly possible, followed by how to write the program.

So I'm going to talk about this as an example of the DOM implementation of VB and MSXML's COM interface in this article:

The first thing I would like to say is that the version of MSXML I use is IE5, the version number is 5.0.2919.3800, and Microsoft's earlier version of the interface is somewhat different from the new one, so when you're programming, you should look at her interface and instructions.

If you are not familiar with VB and COM, look at the following may be more laborious, but VB than other language implementation, should be relatively simple and clear.

First declare the variables for the following objects:

Dim Tempdoc as MSXML. 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

To generate the root node and set it to the root of the file

Set root = Tempdoc.createelement ("Myroot")
Set tempdoc.documentelement = root

Build child node add to root node and set a property for this node

Set Tempnode = Tempdoc.createnode (MSXML. Node_element, "Mynode", "")
Tempnode. Text = "Mynodevalue"
Root.appendchild Tempnode

Gets the interface of the element node, adding attributes

Set tempelement = Tempnode
Tempelement.setattribute "MyAttribute", "Myattributevalue"

Write XML file

Open "Myxmlfile.xml" for output as #1
Print #1, Root. Xml
Close #1

The following is the contents of the XML file generated by the above program:


There are also non-dom interfaces available in MSXML, which depends on how you use them.

Author: Keep your/xml

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.