Java Reference
In-Depth Information
Node root
=
doc
.
createElement
(
"Poem"
);
doc
.
appendChild
(
root
);
Node stanza
=
doc
.
createElement
(
"Stanza"
);
root
.
appendChild
(
stanza
);
Node line
=
doc
.
createElement
(
"Line"
);
stanza
.
appendChild
(
line
);
line
.
appendChild
(
doc
.
createTextNode
(
"Once, upon a midnight dreary"
));
line
=
doc
.
createElement
(
"Line"
);
stanza
.
appendChild
(
line
);
line
.
appendChild
(
doc
.
createTextNode
(
"While I pondered, weak and weary"
));
return
return
doc
;
}
catch
catch
(
Exception ex
) {
System
.
err
.
println
(
"+============================+"
);
System
.
err
.
println
(
"| XML Error |"
);
System
.
err
.
println
(
"+============================+"
);
System
.
err
.
println
(
ex
.
getClass
());
System
.
err
.
println
(
ex
.
getMessage
());
System
.
err
.
println
(
"+============================+"
);
return
return null
null
;
}
}
}
A more complete program would create an output file and have better error reporting. It
would also have more lines of the poem than I can remember.
Program: xml2mif
Format), which is vaguely related to XML but is not well-formed. Let's look at a program
that uses DOM to read an entire document and generate code in MIF for each node. This pro-
gram was in fact used to create some chapters of the first edition.
one of several output generator classes. This could be used as a basis for generating other
formats.
Example 20-16. XmlForm.java