- XML Reference Guide
- Overview
- What Is XML?
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Table of Contents
- The Document Object Model
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- DOM and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Implementations
- DOM and JavaScript
- Using a Repeater
- Repeaters and XML
- Repeater Resources
- DOM and .NET
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Downloads
- DOM and C++
- DOM and C++ Resources
- DOM and Perl
- DOM and Perl Resources
- DOM and PHP
- DOM and PHP Resources
- DOM Level 3
- DOM Level 3 Core
- DOM Level 3 Load and Save
- DOM Level 3 XPath
- DOM Level 3 Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- The Simple API for XML (SAX)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SAX and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- SAX and .NET
- Informit Articles and Sample Chapters
- SAX and Perl
- SAX and Perl Resources
- SAX and PHP
- SAX and PHP Resources
- Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Document Type Definitions (DTDs)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Schemas
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- RELAX NG
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Schematron
- Official Documentation and Implementations
- Validation in Applications
- Informit Articles and Sample Chapters
- Books and e-Books
- XSL Transformations (XSLT)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XSLT in Java
- Java in XSLT Resources
- XSLT and RSS in .NET
- XSLT and RSS in .NET Resources
- XSL-FO
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XPath
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Base
- Informit Articles and Sample Chapters
- Official Documentation
- XHTML
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XHTML 2.0
- Documentation
- Cascading Style Sheets
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XUL
- XUL References
- XML Events
- XML Events Resources
- XML Data Binding
- Informit Articles and Sample Chapters
- Books and e-Books
- Specifications
- Implementations
- XML and Databases
- Informit Articles and Sample Chapters
- Books and e-Books
- Online Resources
- Official Documentation
- SQL Server and FOR XML
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- Service Oriented Architecture
- Web Services
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Creating a Perl Web Service Client
- SOAP::Lite
- Amazon Web Services
- Creating the Movable Type Plug-in
- Perl, Amazon, and Movable Type Resources
- Apache Axis2
- REST
- REST Resources
- SOAP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SOAP and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- WSDL
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- UDDI
- UDDI Resources
- XML-RPC
- XML-RPC in PHP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Ajax
- Asynchronous Javascript
- Client-side XSLT
- SAJAX and PHP
- Ajax Resources
- JSON
- Ruby on Rails
- Creating Objects
- Ruby Basics: Arrays and Other Sundry Bits
- Ruby Basics: Iterators and Persistence
- Starting on the Rails
- Rails and Databases
- Rails: Ajax and Partials
- Rails Resources
- Web Services Security
- Web Services Security Resources
- SAML
- Informit Articles and Sample Chapters
- Books and e-Books
- Specification and Implementation
- XML Digital Signatures
- XML Digital Signatures Resources
- XML Key Management Services
- Resources for XML Key Management Services
- Internationalization
- Resources
- Grid Computing
- Grid Resources
- Web Services Resource Framework
- Web Services Resource Framework Resources
- WS-Addressing
- WS-Addressing Resources
- WS-Notifications
- New Languages: XML in Use
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Google Web Toolkit
- GWT Basic Interactivity
- Google Sitemaps
- Google Sitemaps Resources
- Accessibility
- Web Accessibility
- XML Accessibility
- Accessibility Resources
- The Semantic Web
- Defining a New Ontology
- OWL: Web Ontology Language
- Semantic Web Resources
- Google Base
- Microformats
- StructuredBlogging
- Live Clipboard
- WML
- XHTML-MP
- WML Resources
- Google Web Services
- Google Web Services API
- Google Web Services Resources
- The Yahoo! Web Services Interface
- Yahoo! Web Services and PHP
- Yahoo! Web Services Resources
- eBay REST API
- WordML
- WordML Part 2: Lists
- WordML Part 3: Tables
- WordML Resources
- DocBook
- Articles
- Books and e-Books
- Official Documentation and Implementations
- XML Query
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XForms
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Resource Description Framework (RDF)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Topic Maps
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation, Implementations, and Other Resources
- Rich Site Summary (RSS)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Simple Sharing Extensions (SSE)
- Atom
- Podcasting
- Podcasting Resources
- Scalable Vector Graphics (SVG)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- OPML
- OPML Resources
- Summary
- Projects
- JavaScript TimeTracker: JSON and PHP
- The Javascript Timetracker
- Refactoring to Javascript Objects
- Creating the Yahoo! Widget
- Web Mashup
- Google Maps
- Indeed Mashup
- Mashup Part 3: Putting It All Together
- Additional Resources
- Frequently Asked Questions About XML
- What's XML, and why should I use it?
- What's a well-formed document?
- What's the difference between XML and HTML?
- What's the difference between HTML and XHTML?
- Can I use XML in a browser?
- Should I use elements or attributes for my document?
- What's a namespace?
- Where can I get an XML parser?
- What's the difference between a well-formed document and a valid document?
- What's a validating parser?
- Should I use DOM or SAX for my application?
- How can I stop a SAX parser before it has parsed the entire document?
- 2005 Predictions
- 2006 Predictions
- Nick's Book Picks
If you're an object-oriented programmer, you probably gravitated towards the Document Object Model for
manipulating your XML data. DOM is handy, in that it's widely supported, it lets you manage your data in an
almost tangible way, and it lets you make changes to the Document
in memory. Oh sure, it's
not the only way to manipulate XML data, and in some cases, it's not even the best way. But it's the most
common way, and for most of us, it was our first way.
Of course, there are certain ... annoyances when it comes to working with the Document Object Model.
For example, reading a document from a file is fine, but what if you want to create one from scratch? For that matter,
how do you create a parser? Output the data? These are all issues that suffer from irritations stemming from a lack
of standardization or just downright difficulty. And don't even get me started on moving a Node
from one Document
to another!
Fortunately, I'm not the only one to notice these problems. In fact, developers started complaining about them way back when the DOM was first introduced, so it was inevitable that someone would take up the cause and solve them. Enter the Document Object Model, Level 3. DOM Level 3 consists of several different recommendations and notes:
- DOM Level 3 Core: The DOM Level 3 Core Recommendation's big claim to fame is the specification
of "bootstrapping", or a standard way in which to create a DOM
Document
(or specifically, aDocumentImplementation
) from scratch. Previously, the fact that everything was defined as interfaces made it necessary to "hard-code" the creation of aDocument
to a particular implementation. In other words, you couldn't run your Xerces-Java code in a JAXP implementation. The Core also clears up some issues regarding namespace resolution and adds the abililty to create aDOMConfiguration
. Another interesting feature of DOM Level 3 is the ability to bind your own non-XML data to aDocument
via the newDOMUserData
type. This Recommendation also introduces some new methods to existing objects, making it possible to moveNode
s betweenDocument
s, normalizeDocument
s, and solve other problems that have aggravated developers for years. - DOM Level 3 Load and Save: If the ability to easily load and save an XML
Document
object seems insignificant to you, then either you've never done it or you've gotten used to jumping through hoops such as performing an XSL identity transformation just to send your XML out to a file. This Recommendation defines a standard way to not only create and configure a parser, but also to specify an output location, configure the output, and actually output your XML to a destination. - DOM Level 3 Validation: This recommendation solves the problem of creating a document that is not only valid when you parse it, but also after you've edited it. It creates the ability to validate a document "on demand", as well as the ability to prevent non-valid changes to the document. It also gives you the ability to know what changes are permitted at a particular node (in order to list available options, for example) or to know whether a particular change is valid.
- DOM Level 3 XPath Working Group Note: If you've ever tried to get to a specific node within a
Document
object, you'll appreciate an implementation of this document. It defines the means by which a DOM implementation can enable you to select aNode
orNode
s based on an XPath expression, just as you would in an XSL Transformation. This document is not a recomendation, as the Working Group doesn't expect to get two implementations that use the same binding. - DOM Level 3 Views and Formatting Working Group Note: This document builds on the DOM Level 2 Views Recommendation, but at the moment doesn't see a lot of activity.
In order to use the Document Object Model Level 3, you need to have an implementation handy, which at the time of this writing is easier said than done. Because I'm predisposed toward Java, Here are the instructions for making DOM Level 3 available to you:
- Install Java. You can download it from java.sun.com. I'm using Java 1.4.x for these instructions. Hopefully the final release of Java 1.5 will have this capability included and this will be a moot point.
- Download
beta2-dom3-Xerces-J-bin.2.6.2.zip
from Apache and extract the files from it. - Create the directories
<javahome>/lib/endorsed
and<javahome>/jre/lib/endorsed
and add thedom3-xercesImpl.jar
anddom3-xml-apis.jar
files to both. - You may also need to add the jar files to your classpath, although technically that shouldn't be necessary.
- In order to get the documentation, download the Xerces source and tools distributions. Unpack both.
- Copy the
tools
directory to thexerces-2_6_2
directory (or whatever directory you chose for the source files). - Open a command prompt window and change to the
xerces-2_6_2
directory. - Type
build javadocs
to generate the API documentation. Note that this documentation is different from what appears in the samples on the Xerces website. (You can also usebuild jar-dom3
andbuild apijar-dom3
to generate newer versions of thedom3-xercesImpl.jar
anddom3-xml-apis.jar
files.) - Now you can test your installation. Create a small XML file called
personal.xml
and save it in the directory in which you'll create your new class. - Create a new Java class:
import org.w3c.dom.bootstrap.DOMImplementationRegistry; import org.w3c.dom.DOMImplementationList; import org.w3c.dom.ls.DOMImplementationLS; import org.w3c.dom.ls.LSParser; import org.w3c.dom.Document; import org.w3c.dom.ls.LSSerializer; import org.w3c.dom.ls.LSOutput; public class testDom { public static void main (String args[]){ try { System.setProperty(DOMImplementationRegistry.PROPERTY, "org.apache.xerces.dom.DOMImplementationSourceImpl"); DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS)registry.getDOMImplementation("LS"); LSParser builder = impl.createLSParser( DOMImplementationLS.MODE_SYNCHRONOUS, null); Document document = builder.parseURI("personal.xml"); LSSerializer writer = impl.createLSSerializer(); LSOutput output = impl.createLSOutput(); output.setSystemId("file:///personalout.xml"); writer.write(document, output); } catch (Exception e){ System.out.println(e.toString()); } } }
- Compile and run the class. You should see a new file,
personalout.xml
, in the root directory.
Of course, there's a lot of new information here, and in the coming weeks, I'll be going over it to explain how DOM Level 3 works, but for now, just compile and run the application to make sure it works.