|
|
What DOMinates XML?by Nate Zelnick Understanding the DOM
As you can see, the container model of HTML maps really well to the idea of a document tree that shows the relationship between a branch (called a node) and the trunk it grows out of (called the root). The tree is just a metaphor that will help you to visualize how a document's elements, attributes and content are related to each other. A DOM implementation may not actually represent its data in this way, but the ways that you can manipulate that structure of a document will work against this kind of visualization, so it's an extremely useful tool. The key to DOM compliance is that the interfaces that a document exposes are the same. From the TopThe interfaces that the DOM exposes are really a set of methods for dealing with the structure map of a document once it has been parsed. The parsing process is actually where the structure map is created. In HTML, the structures are understood ahead of time. The document will have a top-level root called HTML, and it will contain a HEAD element that will have some limited set of possible sub-elements with their own limited set of attributes. These elements will contain data that will be children of the elements or their attributes. But what about XML? Since there are no known elements or attributes of XML ahead of time, the parsing process is really the act of building a structure map of the arbitrary relationship as the document is being read. So a simple XML snippet that describes a customer might look like this in a structure map:
How this works is that the root element -- CUSTOMER -- is the top-level object. It contains an element called NAME that has two children: FIRST and LAST. The values of FIRST and LAST are "Joe" and "User". CUSTOMER also contains an element called ADDRESS that also has two children: STREET and CITY. STREET and CITY have values of "123 Main Street" and "Anywhere, USA". So the XML that this structure map describes looks like this: <CUSTOMER> <NAME> <FIRST>Joe</FIRST> <LAST>User</LAST> </NAME> <ADDRESS> <STREET>123 Main Street</STREET> <CITY>Anywhere, USA</CITY> </ADDRESS> </CUSTOMER> Now the key question is how do you work with this structure? That's the essence of the DOM -- and the subject of the next column.
Contact the WebDeveloper.com® staff Last modified: Friday, 22-Aug-2008 13:46:48 EDT
|
Refresh Daily
|