How to 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. 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 write programs.

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

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 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 MSXML.DOMDocumentDim 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.documentElement = 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.

The above is a detailed introduction to how to use DOM to create XML. For more information, see The PHP Chinese website (www.php1.cn )!


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.