Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :
03/24/2008
Times viewed :
3564
createNode()
Is a member of:
DOMDocument
Syntax
As an alternative to the createElement(), createAttribute(),
etc. methods, you can also use the createNode() method, which
creates any type of Node, using the following syntax:
set objXMLDOMNode = objDOMDocument.createNode(Type, name,
namespaceURI)
Table describes the syntax of createNode().
0.0.1 Signature of the createNode method (continued)
Type
Specify the type of Node, which can
be the numeric value of the nodeType or a string of the
nodeTypeString.
name
Specify the name of the Node or, if
there is no name, then an empty string (""). Remember
that if you're dealing with an ele ment or an entity
reference, the name is case-sensitive.
namespaceURI
Specify the namespace or, if there is no
namespaceURI, then an empty string ("").
Remark
Before we continue, please refer to the nodeType property
section, which explains in detail the different Node types, their
enumeration values, and descriptions.
You cannot create a Node of the following types:
¬ Document-it is the root of the XML; it cannot be a
childNode. The
create-Node() method only creates child Nodes. To create the
root
of your XML, you initialize a DOMDocument object as we have
seen
in most examples so far.
¬ Document type-DTDs cannot be added to the DOMDocument.
See the
docType property in this section.
¬ Entity-entities are declared in the DTD; therefore,
being external to the XML file restricts their creation
¬ Notation-this
is the same as entities
If you attempt to create any of the above, an error will
occur.
Table lists what to insert in the Type and Name parts of the
create- Node() signature. We have not added the namespace property in the
signature, as this only applies to element and attribute Nodes,
where you can specify the namespace. If there is not a namespace, you need to add an
empty string (""), as this signature has no optional
addresses.
In the Type part of the createNode() signature, you can insert
either the nodeType
value or the nodeTypeString value. You can insert the Name
section in the name part of the signature.
0.0.1 Specifications of values to insert in createNode
signature (continued)
Type of Node
node Type
value
nodeType
String value
name
Element
1
"element"
tagName/nodeName (e.g.,
"PERSON")
Attribute
2
"attribute"
name of the attribute/nodeName
(e.g., "PERSONID")
Text
3
"text"
empty string ("")
CDATA section
4
"cdatasection"
empty string ("")
Entity Reference
5
"entityreference"
name of the referenced
entity/nodeName (e.g., "USA")
Processing Instruction
7
"processinginstruction"
target/nodeName
("realaudio')
Comment
8
"comment"
empty string ("")
Document Fragment
11
"documentfragment"
empty string ("")
Example
In the following examples, we create the different nodeTypes
and append them to the documentElement property of the
DOMDocument.
To create an element Node, pass the nodeTypeString as the
type:
Set objNode = m_objXMLDOM.createNode("element",
"PERSON", "")
m_objXMLDOM.documentElement.appendChild objNode
¬ It is such a pity that the DOM- NodeType enum for the
Node type in the signature was not used as the passed variable.
(Instead, an integer or string was inserted.) It would have made
the Intellisense auto-complete in VB so much easier to read.
Alternatively, you can pass the nodeType enumeration as the
type:
Set objNode = m_objXMLDOM.createNode(NODE_ELEMENT,
"PERSON", "")
m_objXMLDOM.documentElement.appendChild objNode
Otherwise, you can pass the nodeType enumeration value as the
type:
Set objNode = m_objXMLDOM.createNode(1, "PERSON",
"")
m_objXMLDOM.documentElement.appendChild objNode
The example from the createComment() method example would
change as follows for the createNode() method:
Dim objDOMDocument As DOMDocument
Dim objComment As IXMLDOMComment
Set objDOMDocument = New DOMDocument
Set objComment = objDOMDocument.createNode(NODE_COMMENT,
"", "")
objComment.Text = "This is a comment."
objDOMDocument.appendChild objComment
¬ We've used the nodeType enumuration for the
nodeType. Don't forget to pass empty strings for the other
two parameters, as they are not optional.
¬ Add the comment text.
¬ Append the comment Node to its parent, which in this
case is the DOMDocument.
This manuscript is an abridged version of a chapter from the
Manning
Publications book XMLProgramming with VB and ASP. This chapter
looks at the Microsoft DOM objects. NOTE: Most images have been
removed to increase speed and many of the code comments have also
been removed for presentation. Please purchase the book to enjoy
the full experience of all the chapters with images and code
comments!