I have a requirement to serialize parts of an XML document, either the whole document or an element or an attribute. My code works fine for documents and elements, but fails for attributes. If I try to serialize an attribute node, no output is generated. I expected the value of the attribute to be written, e.g. for <... Fred="hello"> , I expected "hello" to be serialized.
Why does this not happen? I am using xerces 2.8.1 and Java 1.5_04 Here is some test code that reflects my usage. START_OF_CODE package test; import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.DOMConfiguration; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.bootstrap.DOMImplementationRegistry; import org.w3c.dom.ls.DOMImplementationLS; import org.w3c.dom.ls.LSOutput; import org.w3c.dom.ls.LSSerializer; import org.xml.sax.InputSource; public class Test { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { // Dummy document StringBuffer sb = new StringBuffer(); sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"); sb.append("<wrapperRoot>"); sb.append("<wrapperElem wrapperAtt=\"hello\">"); sb.append("</wrapperElem>"); sb.append("</wrapperRoot>"); // Parse dummy document Document newdoc = null; try { DocumentBuilderFactory fa = DocumentBuilderFactory.newInstance(); fa.setNamespaceAware(true); fa.setFeature( "http://apache.org/xml/features/dom/defer-node-expansion", false); DocumentBuilder parser = null; parser = fa.newDocumentBuilder(); InputSource inputSource = new InputSource(new StringReader( sb.toString())); inputSource.setEncoding("UTF-8"); newdoc = parser.parse(inputSource); } catch (Exception e) { e.printStackTrace(); System.exit(1); } // Document - This works doSerialize(newdoc, true); System.out.println(); // Root Element- This works doSerialize(newdoc.getDocumentElement(), false); System.out.println(); // Element - This works doSerialize(newdoc.getDocumentElement().getFirstChild(), false); System.out.println(); // Attribute - This does not work, nothing is written to 'out' doSerialize(newdoc.getDocumentElement().getFirstChild().getAttributes(). item(0), false); System.out.println(); } public static void doSerialize(Node node, boolean xmlDecl) throws Exception { // Get DOM Implementation using DOM Registry System.setProperty(DOMImplementationRegistry.PROPERTY, "org.apache.xerces.dom.DOMXSImplementationSourceImpl"); DOMImplementationRegistry registry; registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); // Create and configure an LSSerializer LSSerializer lSSerializer = impl.createLSSerializer(); DOMConfiguration config = lSSerializer.getDomConfig(); config.setParameter("xml-declaration", xmlDecl); config.setParameter("format-pretty-print", false); lSSerializer.setNewLine("\n"); // Create an output sink LSOutput lSOutput = impl.createLSOutput(); lSOutput.setByteStream(System.out); lSOutput.setEncoding("UTF-8"); // Serialise... lSSerializer.write(node, lSOutput); } } END_OF_CODE --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]