Why Do I Need DOM?
The main reason for using DOM is to create or modify an XML document
programmatically. You can use DOM just to read an XML document, but as you
will see in the next chapter, SAX is often a better candidate for the read-only
case. If you want to create a document, you start by creating a root element
and then add attributes, content, sub-ele-ments, and so on. Once you are
finished, you can write the document out to disk or send it over a network. The
output looks just like an XML document prepared in a text editor or XML tool.
If you want to modify an existing XML document, you can read it in from
a file or other I/O source. The entire document is read into memory all at
once, so you can change any part of it at any time. The representation in
memory is a tree structure that starts with a root element that contains
attributes, content, and sub-elements. You can traverse this tree, search for a
specific node, and change its attributes or data. You can also add attrib-utes
or elements anywhere in the tree, as long as you don’t violate the rules of a
well-formed document. Again, you can write the modified document back out to
disk or to the network.
It is possible to process XML documents using other, simpler techniques,
such XSLT. The problem is that XSLT is not always expressive enough to solve
complex problems. For example, let’s say you want to search for elements
described by another element, in the case of a master/detail relationship. This
is difficult if not impossible to accomplish with XSLT.
Learning to use DOM saves you considerable time by leveraging existing
parsers. Additionally, a standard interface makes it easy to change parsers in
the event that an improved implementation becomes available.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.