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 Th...@emc.com on 2009/12/14 19:02:22 UTC

whitespace management with DOM element

Hello,

 

I have an element that usually contains alpha characters, but depending
upon the user input can contain a single space.  I'm using the
DocumentBuilderFactory to create an instance of DocumentBuilder, and
then generating DOM elements from that and serializing the output to a
file.

 

I'm seeing the resulting serialized file is simply an empty XML element.
Is it possible to configure the DOM builder to preserve the spaces if
they exist, or does this go against the XML spec?  If so, can you help
me understand how to make that happen?

 

Thanks,

Nicholas

 


RE: whitespace management with DOM element

Posted by Th...@emc.com.
Hello Michael,

 

Thanks for the response.  After more investigation, I found that the
desired text was indeed being stored with the DOM Text element, but that
the serializer was truncating its value.  After quite a bit of searching
on the web, I found that setting the 'xml:space=preserve' attribute on
the parent node resulted in the output being generated in the
pretty-print format desired but that spaces are preserved during this
writer output operation.

 

For those others who may be having a similar issue and are interested,
here is the xml that allowed the serializer to correctly preserve the
spaces:

 

<test>

    <first>abc</first>

    <second xml:space="preserve"> </second>

    <third>def</third>

</test>

 

Without the 'xml:space=preserve' attribute, the following serialized XML
is generated from the serializer:

 

<test>

    <first>abc</first>

    <second />

    <third>def</third>

</test>

 

I found the serializer could be told to preserve spaces, but then the
output was not generated in pretty-print format, which was desired.

 

Perhaps it is common knowledge about the xml:space attribute, but not to
me and was really the answer I was looking for because I didn't want to
alter the actual text data to contain a non-whitespace character to
accommodate the serializer which would have repercussions because on the
flip-side the parser then would need to be modified to perform the
necessary stripping of the content data.

 

Regards,

Nicholas

 

 

From: Michael Glavassevich [mailto:mrglavas@ca.ibm.com] 
Sent: Monday, December 14, 2009 8:59 PM
To: j-users@xerces.apache.org
Subject: Re: whitespace management with DOM element

 

Hi Nicholas,

I'm not sure I follow the issue you're having. Can you give a concrete
example?

Thanks.

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

<Th...@emc.com> wrote on 12/14/2009 01:02:22 PM:

> Hello,
>  
> I have an element that usually contains alpha characters, but 
> depending upon the user input can contain a single space.  I'm using
> the DocumentBuilderFactory to create an instance of DocumentBuilder,
> and then generating DOM elements from that and serializing the 
> output to a file.
>  
> I'm seeing the resulting serialized file is simply an empty XML 
> element.  Is it possible to configure the DOM builder to preserve 
> the spaces if they exist, or does this go against the XML spec?  If 
> so, can you help me understand how to make that happen?
>  
> Thanks,
> Nicholas


Re: whitespace management with DOM element

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

I'm not sure I follow the issue you're having. Can you give a concrete
example?

Thanks.

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

<Th...@emc.com> wrote on 12/14/2009 01:02:22 PM:

> Hello,
>
> I have an element that usually contains alpha characters, but
> depending upon the user input can contain a single space.  I'm using
> the DocumentBuilderFactory to create an instance of DocumentBuilder,
> and then generating DOM elements from that and serializing the
> output to a file.
>
> I'm seeing the resulting serialized file is simply an empty XML
> element.  Is it possible to configure the DOM builder to preserve
> the spaces if they exist, or does this go against the XML spec?  If
> so, can you help me understand how to make that happen?
>
> Thanks,
> Nicholas