A little bit more...

Saturday, January 20, 2007

DOM Level 3 Load and Save

1. Introduction

Defined by w3 consortium, just as its name indicates, it serves as interfaces to objects that dealing with loading and saving DOM objects.

It’s noteworthy that various specific implementations doesn’t always fully support.

With java, it is well supported, though, some optional specifications are not supported (at least not always workabe from my experience). However, for web developers, there’s a quote from a book for you:

Now that XMLHttpRequest is being standardized, it seems likely that LSParser will never be supported by most browsers.

In addition, with java, you can also selectively use javax.xml.transform to deal with loading and saving DOM objects.

2. Breakdown

Cite from org.w3c.dom.ls:

DOMImplementationLS
DOMImplementationLS contains the factory methods for creating Load and Save objects.

LSInput
This interface represents an input source for data.

LSLoadEvent
This interface represents a load event object that signals the completion of a document load.

LSOutput
This interface represents an output destination for data.

LSParser
An interface to an object that is able to build, or augment, a DOM tree from various input sources.

LSParserFilter
LSParserFilters provide applications the ability to examine nodes as they are being constructed while parsing.

LSProgressEvent
This interface represents a progress event object that notifies the application about progress as a document is parsed.

LSResourceResolver
LSResourceResolver provides a way for applications to redirect references to external resources.

LSSerializer
A LSSerializer provides an API for serializing (writing) a DOM document out into XML.

LSSerializerFilter
LSSerializerFilters provide applications the ability to examine nodes as they are being serialized and decide what nodes should be serialized or not.

3. Examples

a. Write an instance of Document to a file with org.w3c.dom.ls:

DOMImplementationLS domImpl = (DOMImplementationLS)doc
.getImplementation();
LSSerializer serializer = domImpl.createLSSerializer();
LSOutput output = domImpl.createLSOutput();
output.setCharacterStream(new PrintWriter(fileName));
serializer.write(doc, output);

b. Write an instance of Document to a file with javax.xml.transform:

StreamResult sr = new StreamResult(new File(fileName));
Transformer t = TransformerFactory.newInstance().newTransformer();
t.transform(new DOMSource(doc), sr);

Note: it seems, from my experience, that example b can’t preserve the print friendly presentation.

Resources:

  1. Document Object Model (DOM) Level 3 Core Specification Version 1.0
  2. Document Object Model (DOM) Level 3 Load and Save Specification Version 1.0
  3. DOM Level 3
  4. Java Impl. Package org.w3c.dom.ls (src, api spec)

No comments:

About Me

My photo
I'm finishing my master degree in Software Engineering, Computer Science. I believe and have been following what Forrest Gump's Mam said: you have to do the best with what god gave you.