.NET Developer Tip
(Receive this column in your inbox,
click Edit your Profile to subscribe.)
The VB DOMDocument class
This tip, excerpted from InformIT, offers some information on one class that provides a few really useful properties and methods.
Visual Basic.NET provides integrated tools for using XML, schemas, XSL, and other XML-related tools, but there's no need to wait for VB.NET in order to get started. Microsoft XML Core Services (MSXML) version 4.0 gives you the tools you need to load and save XML documents from your Visual Basic 6 application. Download the most recent version of MSXML at msdn.microsoft.com/xml/default.asp, and install it on your computer. To use MSXML in a Visual Basic 6 program, select the Project menu's References command. Select Microsoft XML, v 4.0 (or whatever the version du jour is), and click OK. Now you're ready to add XML objects to your program.
The DOM (document object model) describes an XML file's hierarchical nature using a corresponding set of programming objects. DOMDocument is the MSXML class that represents an XML document's DOM structure.
The DOMDocument class provides only a few really useful properties and methods. The load method makes the object load an XML file. The loadXML method makes it load XML data from a string. For example, the following code loads a small XML file into the document named xml_document.
Dim xml_document As New DOMDocument xml_document.loadXML _ "<Person>" & vbCrLf & _ " <FirstName>Rod</FirstName>" & vbCrLf & _ " <LastName>Stephens</LastName>" & vbCrLf & _ "</Person>"
The DOMDocument's xml property returns the document's XML representation. You can display this value to see what the document looks like. You could also save it into a file, but there's really no need to do that because the DOMDocument object's save method does it automatically.
The object's documentElement property is a node that represents the document's topmost data node. All the other nodes lie within that one. Usually, when you manipulate an XML document's nodes, you will start at this node.
DOMDocument provides several methods for creating new nodes. The createElement method makes a new element node for the document. This is the kind of node you usually want to create. Other node creation methods include createAttribute, createProcessingInstruction, and createTextNode, although I won't say anything about those methods in this article.
To read the article from which this tip is excerpted, click over to InformIT. You have to register there, but the registration is free.
For More Information:
- Looking for free research? Browse our comprehensive White Papers section by topic, author or keyword.
- Are you tired of technospeak? The Web Services Advisor column uses plain talk without the hype.
- For insightful opinion and commentary from today's industry leaders, read our Guest Commentary columns.
- Hey Codeheads! Start benefiting from other time-saving XML Developer Tips and .NET Developer Tips.
- Visit our huge Best Web Links for Web Services collection for the freshest editor-selected resources.
- Choking on the alphabet soup of industry acronyms? Visit our helpful Glossary for the latest lingo.
- Visit Ask the Experts for answers to your Web services, SOAP, WSDL, XML, .NET, Java and EAI questions.
- Discuss this issue, voice your opinion or just talk with your peers in the SearchWebServices Discussion Forums.
This was first published in October 2002