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:
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:
DOMImplementationLSDOMImplementationLS 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.
LSParserFilterLSParserFilters 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.
LSResourceResolverLSResourceResolver 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.
LSSerializerFilterLSSerializerFilters 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:

No comments:
Post a Comment