Use Dom to create XML

Source: Internet
Author: User

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.

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: info-contact@alibabacloud.com 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.