You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by "Desmond Whewell (CV/ETL)" <de...@ericsson.com> on 2007/03/30 14:29:04 UTC

Serializing Attribute nodes

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: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org


RE: Serializing Attribute nodes

Posted by "Desmond Whewell (CV/ETL)" <de...@ericsson.com>.
I can confirm that using Xerces 2.9.0, Attr nodes are serialized as
required.

Cheers, Des 

-----Original Message-----
From: Desmond Whewell (CV/ETL) [mailto:desmond.whewell@ericsson.com] 
Sent: 05 April 2007 08:14
To: j-users@xerces.apache.org
Subject: RE: Serializing Attribute nodes

Michael, thank you for your advice.

I will investigate the Xerces 2.9.0 behaviour; portability is not
significant for my application.

As long as I can get Xerces to write the Attr node value in 'character
referenced' form (e.g. "Thia & That" as "This &amp; That"), I can get
by.

Thanks again,

-----Original Message-----
From: Michael Glavassevich [mailto:mrglavas@ca.ibm.com]
Sent: 04 April 2007 18:04
To: j-users@xerces.apache.org
Cc: Desmond Whewell (CV/ETL)
Subject: Re: Serializing Attribute nodes

Hi Desmond,

"Desmond Whewell \(CV/ETL\)" <de...@ericsson.com> 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: j-users-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-users-help@xerces.apache.org

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org


RE: Serializing Attribute nodes

Posted by "Desmond Whewell (CV/ETL)" <de...@ericsson.com>.
Michael, thank you for your advice.

I will investigate the Xerces 2.9.0 behaviour; portability is not
significant for my application.

As long as I can get Xerces to write the Attr node value in 'character
referenced' form (e.g. "Thia & That" as "This &amp; That"), I can get
by.

Thanks again,

-----Original Message-----
From: Michael Glavassevich [mailto:mrglavas@ca.ibm.com] 
Sent: 04 April 2007 18:04
To: j-users@xerces.apache.org
Cc: Desmond Whewell (CV/ETL)
Subject: Re: Serializing Attribute nodes

Hi Desmond,

"Desmond Whewell \(CV/ETL\)" <de...@ericsson.com> 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: j-users-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-users-help@xerces.apache.org

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org


Re: Serializing Attribute nodes

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
Hi Desmond,

"Desmond Whewell \(CV/ETL\)" <de...@ericsson.com> 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: j-users-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-users-help@xerces.apache.org

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org