Suppose we want to use DOM to produce the following document or its equivalent, modulo white space and other insignifcancies:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> <svg xmlns="http://www.w3.org/2000/svg"/> I claim the following code should do the trick, given that impl is a DOMImplementation object: // Create the document DocumentType svgDOCTYPE = impl.createDocumentType( "svg", "-//W3C//DTD SVG 1.0//EN", "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd" ); Document doc = impl.createDocument( "http://www.w3.org/2000/svg", "svg", svgDOCTYPE); // Serialize the document onto System.out TransformerFactory xformFactory = TransformerFactory.newInstance(); Transformer idTransform = xformFactory.newTransformer(); Source input = new DOMSource(doc); Result output = new StreamResult(System.out); idTransform.transform(input, output); However, what this in fact produces is <?xml version="1.0" encoding="UTF-8"?> <svg/> There are two problems here: 1. The namespace is lost on the svg element. 2. The DOCTYPE declaration is lost. I think both of these are problems with the idTransform. According to the JAXP specification, p. 62, "If all that is desired is the simple identity transformation of a source to a result, then TransformerFactory90 provides a newTransformer()93 method with no arguments. This method creates a Transformer that effectively copies the source to the result. This method may be used to create a DOM from SAX events or to create an XML or HTML stream from a DOM or SAX events." This is less than perfectly clear on issues like whether it should insert namespace attributes as necessary or include the DOCTYPE declaration. However, I think that what's actually output really doesn't strike me as a copy of the source to the result because a lot of significant information has been lost. Yes, I know that I could add the xmlns attribute, at least, manually. However, I think this should be closer to the default behavior of XMLSerializer where namespace declaration attributes are inserted automatically in the stream as necessary. -- +-----------------------+------------------------+-------------------+ | Elliotte Rusty Harold | [EMAIL PROTECTED] | Writer/Programmer | +-----------------------+------------------------+-------------------+ | Java I/O (O'Reilly & Associates, 1999) | | http://www.ibiblio.org/javafaq/books/javaio/ | | http://www.amazon.com/exec/obidos/ISBN=1565924851/cafeaulaitA/ | +----------------------------------+---------------------------------+ | Read Cafe au Lait for Java News: http://www.cafeaulait.org/ | | Read Cafe con Leche for XML News: http://www.ibiblio.org/xml/ | +----------------------------------+---------------------------------+ --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
