Use DOM to create XML

Source: Internet
Author: User
When I used to allow XML and try to apply the DOM, not only did one ask me if I could use the DOM to create an XML file out of thin air. Of course, this is acceptable, followed by How to write programs. That's the title. when I used to introduce XML and let others try to apply the DOM, not only did I ask if I can use the DOM to create an XML file out of thin air.

Of course, this is acceptable, followed by How to write programs.

In this article, I will use the DOM implementation of the COM interface of VB and MSXML as an example:

First of all, I want to clarify that the MSXML version I use is IE5, and the version number is 5.0.2919.3800. the interfaces in earlier Microsoft versions are different from those in the new version. Therefore, when programming on your own, let's take a look at her Interface and clarification.

If you are not familiar with vb and COM, it may be difficult to see the following. However, compared with other languages, VB should be relatively simple and clear.

First, declare the variables of 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

A natural XML DOMDocument object

Set tempdoc = New MSXML. DOMDocument

Native root node and set it as the root of the file

Set root = tempdoc. createElement ('myroot ')
Set tempdoc.doc umentElement = root

Add a child node to the root node and set an attribute for the 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

Below is the content of the XML file inherent in the above program:

   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.