You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Stuart Roebuck <sr...@adolos.com> on 2000/03/31 16:35:31 UTC

XML Serialization Pretty-printing

I'm trying to write out a DOM to a file in a fairly standard easy-to-read format.

Having discovered the serialization functionality I thought I was high and  
dry until I tried it.  The end result seems to have very little formatting  
at all with most items appearing in one line separated by five or so space  
characters.

Here's what I parse in:

<?xml version="1.0"?>
<!DOCTYPE adolos:pda>

<pda version="0.1">
  <form>
    <entity type="text" id="field1" title="First Field">
      <text>
        This is the text content of the first field.
      </text>
    </entity>
    <entity type="text" id="field2" title="Second Field">
      <text>
        This is the text content of the second field.
      </text>
    </entity>
  </form>

  <content>
    <entry id="field1">
      <date>2000-03-31:21:02</date>
      <text>
        This is the content of the first field of the form.
      </text>
    </entry>
  </content>

  <comments>
    <comment id="field1">
      <date>2000-03-31:22:20</date>
      <author>Stuart Roebuck</author>
      <text>This is my comment on field 1!</text>
    </comment>
  </comments>
</pda>


Here's what I get out:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<pda version="0.1">   <form>     <entity id="field1" title="First Field"
            type="text">       <text>         This is the text content of
                the first field.       </text>     </entity>     <entity
            id="field2" title="Second Field" type="text">       <text>
                This is the text content of the second field.       </text>
        </entity>   </form>    <content>     <entry id="field1">
            <date>2000-03-31:21:02</date>       <text>         This is the
                content of the first field of the form.       </text>
        </entry>   </content>    <comments>     <comment id="field1">
            <date>2000-03-31:22:20</date>       <author>Stuart
            Roebuck</author>       <text>This is my comment on field
            1!</text>     </comment>   </comments> </pda>

Can anyone suggest where I might be going wrong?

Here's my code:
            OutputFormat of = new OutputFormat();
            of.setIndenting(true);
            of.setLineSeparator("\n");
            of.setLineWidth(75);
            of.setPreserveSpace(false);
            of.setStandalone(true);
            Serializer serial = new XMLSerializer(of);
            FileWriter writer = new FileWriter(filepath);
            serial.setOutputCharStream(writer);
            serial.asDOMSerializer().serialize(doc);

Thanks,

Stuart.