Hi Desmond,

"Desmond Whewell \(CV/ETL\)" <[EMAIL PROTECTED]> wrote on 
03/30/2007 08:29:04 AM:

> 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?

Because it was never implemented in Xerces' serializer (and probably never 
will be since it's now deprecated). 

I believe the new LSSerializer (based on Xalan) which is included in 
Xerces 2.9.0 will write out bare attribute nodes. Note that the result of 
serializing Attr nodes is implementation dependent, meaning if you're 
depending on a specific serialized form your application won't be 
portable.

> 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]

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: [EMAIL PROTECTED]
E-mail: [EMAIL PROTECTED]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to