XML DOM createElement() Method
Document Object
Definition and Usage
The createElement() method creates an element node.
This method returns an Element object.
Syntax
createElement(name)
| Parameters | Description |
|---|---|
| name | A string that specifies the name of the element node. |
Example
The following code snippet uses loadXMLDoc() to load "books.xml" into xmlDoc, and add the element node with a text node to each
Example
xmlDoc
=
loadXMLDoc
(
"
books.xml
"
)
;
x
=
xmlDoc
.
getElementsByTagName
(
"
book
"
)
;
for
(
i
=
0
;
i
<
x
.
length
;
i
++
)
{
newel
=
xmlDoc
.
createElement
(
"
edition
"
)
;
newtext
=
xmlDoc
.
createTextNode
(
"
first
"
)
;
newel
.
appendChild
(
newtext
)
;
x
[
i
]
.
appendChild
(
newel
)
;
}
//
Output all title and edition
y
=
xmlDoc
.
getElementsByTagName
(
"
title
"
)
;
z
=
xmlDoc
.
getElementsByTagName
(
"
edition
"
)
;
for
(
i
=
0
;
i
<
y
.
length
;
i
++
)
{
document
.
write
(
y
[
i
]
.
childNodes
[
0
]
.
nodeValue
)
;
document
.
write
(
"
- Edition:
"
)
;
document
.
write
(
z
[
i
]
.
childNodes
[
0
]
.
nodeValue
)
;
document
.
write
(
"
"
)
;
}
Try it »
Document Object