The Source for Java Technology Collaboration
User: Password:



Changshin Lee's Blog

June 2006 Archives


Truth about "format-pretty-output"

Posted by iasandcb on June 15, 2006 at 11:08 PM | Permalink | Comments (0)

I got so much feedback from my recent blog at http://weblogs.java.net/blog/iasandcb/archive/2006/06/serializing_xml.html, and it inspired me to write another.

The most noticeable comment on the blog is to use JAXP transformer to serialize Document, and in fact I couldn't agree on the solution more because I also used it.

Apart from its standard feature, transformer gives you one more nice option: make your Document more human-readable with so-called "pretty-output". The option is so popular that it is almost essential when the output should be seen by a human being, not a machine.

Transformer is basically designed to visualize abstract XML infosets, so it is no wonder to have a diversity of presentations. Even though we do canonicalization on occasion, some want 2 spaces for indentation, and some want no self-closing tags, and so on.

That strength really works for serialization of Document. On the other hand, DOM L3 LSSerializer also supports "format-pretty-output", but unfortunately you can't expect it from JAXP RI, which is also built in Java SE 5 and 6. There's an issue posted for the problem and a possible solution at https://jaxp.dev.java.net/issues/show_bug.cgi?id=6. The patch was actually applied to Apache Xerces, so the option is supported by Xerces 2.8.0.

To sum them up, Transformer can be your friend if you like to control your outputs in details. In addition to that, it's worthwhile considering taking advantage of brevity from LSSerializer.



The world's first Java EE 5 compatible implementation

Posted by iasandcb on June 15, 2006 at 07:08 PM | Permalink | Comments (0)

TmaxSoft (yes, where I work now) achieved it (of course except the Sun RI :-). See http://java.sun.com/javaee/overview/compatibility.jsp for more details. We are planning to release some preview as well.

Thank you all for your great help, especially Alan, Arthur, and Stephen from the Sun CTS team. I couldn't forget such a beautiful weather in Boston.

P.S. More about this to come, so stay tuned!



Serializing XML, not printing XML

Posted by iasandcb on June 14, 2006 at 09:49 PM | Permalink | Comments (10)

Suppose you have an org.w3c.dom.Document instance to output, for example, to a web browser screen or just simply on a Console, then hmm... unfortunately, we used to have no standard way within DOM API (used, not now).

Even though we can take advantage of DOM L3 Load/Save from JAXP 1.3, it doesn't mean that everybody is happy with that mainly because JAXP 1.3 is fairly new and you might have no choice but to stick to JAXP 1.2 and earlier.

One fine day, I encountered an outstanding snippet of code:

System.out.println(doc);

where doc is of Document.

And the result? Yes! it works!


...


OK, then can we generalize this? Again unfortunately, in this case Document.toString() method implementation is overriden but Document API doesn't require so. In other words, behaviors of Document.toString() vary from implementation to implementation, so you can't expect it to work always.

Therefore, I'd like to recommend two things:

1. Use the DOM L3 Save feature as long as you can.
If you use JDK 5 and later, you don't have to pay a penny for that. If you need to use JDK 4 and earlier, consider that your system is able to allow you to endorse the new JAXP set.

Here's a short example of how-to.

DOMImplementation implementation= DOMImplementationRegistry.newInstance()
.getDOMImplementation("XML 3.0");
DOMImplementationLS feature = (DOMImplementationLS) implementation.getFeature("LS",
"3.0");
LSSerializer serializer = feature.createLSSerializer();
LSOutput output = feature.createLSOutput();
output.setByteStream(System.out);
serializer.write(doc, output);

2. If your situation is inevitable, choose XMLSerializer in Xerces without depending on Document.toString(). Although it's not a standard approach, it has been working since its inception for many XML guys :-)

Here's also a short example of how-to.

XMLSerializer serializer = new XMLSerializer();
serializer.setOutputByteStream(System.out);
serializer.serialize(doc);

3. I got valuable input from commentators. It's very coincidental that I just recognized that I used the same method as them :-)

TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty("indent", "yes");
transformer.transform(new DOMSource(node), new StreamResult(System.out));

#1 and #3 uses JAXP API, so if you're concerned about portability of your code, those choices can help you.





Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds