This discussion is archived
8 Replies Latest reply: Feb 22, 2012 11:41 AM by jtahlborn RSS

JAXB, JAXB_FORMATTED_OUTPUT property, levels of indentation

834151 Newbie
Currently Being Moderated
Is it possible to provide greater control over formatted output, especially indentation, than appears to be possible with the JAXB_FORMATTED_OUTPUT property?

Currently, when I reach the 10th level of indentation, the formatted output shifts back to the left margin and restarts indentation. For a large data file, this can be disconcerting, and is definitely not what is desired.

I cannot find any way of turning off this behavior. I found, on the web, references to a property called "com.sun.xml.bind.indentString", but when I try to set this to, for example, two spaces rather than four, it throws a PropertyException, and the Marshaller documentation does not list this as a supported property.

Can anyone tell me if I can turn off this behavior, or change the number of levels of indentation before the left-shift-back-to-margin is triggered?

Thanks.

¦{Þ                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
  • 1. Re: JAXB, JAXB_FORMATTED_OUTPUT property, levels of indentation
    EJP Guru
    Currently Being Moderated
    More probably the viewer you are using does that, not the output formatting.
  • 2. Re: JAXB, JAXB_FORMATTED_OUTPUT property, levels of indentation
    834151 Newbie
    Currently Being Moderated
    No. I am using a text editor to examine the marshalled XML.

    I also wrote a SSCE that displays the behavior. I left it at work, so I will post it tomorrow.

    ¦{Þ                                                                                                                                                                                                                                                                                                                                               
  • 3. Re: JAXB, JAXB_FORMATTED_OUTPUT property, levels of indentation
    EJP Guru
    Currently Being Moderated
    No. I am using a text editor to examine the marshalled XML.
    Exactly. Possibly the text editor is wrapping the lines.
  • 4. Re: JAXB, JAXB_FORMATTED_OUTPUT property, levels of indentation
    834151 Newbie
    Currently Being Moderated
    EJP wrote:
    No. I am using a text editor to examine the marshalled XML.
    Exactly. Possibly the text editor is wrapping the lines.
    No. By default, word wrap is turned off. I did check that. I'll just post the SSCE tomorrow.

    ¦{Þ                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
  • 5. Re: JAXB, JAXB_FORMATTED_OUTPUT property, levels of indentation
    834151 Newbie
    Currently Being Moderated
    Ok, here is an SSCE to show my problem.

    First, the schema. This is a simple schema that just creates a new level of indentation for each type.
    // In this example, this schema is named testIndentationDepthSchema.xsd
    <xsd:schema
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      elementFormDefault="qualified"
      attributeFormDefault="unqualified"
      >
    
      <xsd:element
        name = "levelO" type = "Level0Type"
        />
    
      <xsd:complexType name = "Level0Type">
        <xsd:sequence>
          <xsd:element
            name = "level1"
            type = "Level1Type"
            />
        </xsd:sequence>
      </xsd:complexType>
    
      <xsd:complexType name = "Level1Type">
        <xsd:sequence>
          <xsd:element
            name = "level2"
            type = "Level2Type"
            />
        </xsd:sequence>
      </xsd:complexType>
    
      <xsd:complexType name = "Level2Type">
        <xsd:sequence>
          <xsd:element
            name = "level3"
            type = "Level3Type"
            />
        </xsd:sequence>
      </xsd:complexType>
    
      <xsd:complexType name = "Level3Type">
        <xsd:sequence>
          <xsd:element
            name = "level4"
            type = "Level4Type"
            />
        </xsd:sequence>
      </xsd:complexType>
    
      <xsd:complexType name = "Level4Type">
        <xsd:sequence>
          <xsd:element
            name = "level5"
            type = "Level5Type"
            />
        </xsd:sequence>
      </xsd:complexType>
    
      <xsd:complexType name = "Level5Type">
        <xsd:sequence>
          <xsd:element
            name = "level6"
            type = "Level6Type"
            />
        </xsd:sequence>
      </xsd:complexType>
    
      <xsd:complexType name = "Level6Type">
        <xsd:sequence>
          <xsd:element
            name = "level7"
            type = "Level7Type"
            />
        </xsd:sequence>
      </xsd:complexType>
    
      <xsd:complexType name = "Level7Type">
        <xsd:sequence>
          <xsd:element
            name = "level8"
            type = "Level8Type"
            />
        </xsd:sequence>
      </xsd:complexType>
    
      <xsd:complexType name = "Level8Type">
        <xsd:sequence>
          <xsd:element
            name = "level9"
            type = "Level9Type"
            />
        </xsd:sequence>
      </xsd:complexType>
    
      <xsd:complexType name = "Level9Type">
        <xsd:sequence>
          <xsd:element
            name = "level10"
            type = "xsd:string"
            />
        </xsd:sequence>
      </xsd:complexType>
    
    </xsd:schema>
    Next, the marshalling test code.
    package jaxbIndentationTest;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBElement;
    import javax.xml.bind.JAXBException;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.Unmarshaller;
    
    public class JaxbIndentationTest
    {
      public static void main( String args[] )
      {
        Level0Type l0 = new Level0Type();
        Level1Type l1 = new Level1Type();
        Level2Type l2 = new Level2Type();
        Level3Type l3 = new Level3Type();
        Level4Type l4 = new Level4Type();
        Level5Type l5 = new Level5Type();
        Level6Type l6 = new Level6Type();
        Level7Type l7 = new Level7Type();
        Level8Type l8 = new Level8Type();
        Level9Type l9 = new Level9Type();
    
        l0.setLevel1( l1 );
        l1.setLevel2( l2 );
        l2.setLevel3( l3 );
        l3.setLevel4( l4 );
        l4.setLevel5( l5 );
        l5.setLevel6( l6 );
        l6.setLevel7( l7 );
        l7.setLevel8( l8 );
        l8.setLevel9( l9 );
        l9.setLevel10( "Final level" );
    
        try
        {
          JAXBContext jaxbContext =
            JAXBContext.newInstance( "jaxbIndentationTest" );
    
          Marshaller m = jaxbContext.createMarshaller();
    
          System.out.println( "Marshaller class == " + m.getClass().getName() );
    
          m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
    //      m.setProperty( "com.sun.xml.bin.indentString", "  " );
    
          OutputStream os = new FileOutputStream( new File( "./exportData.xml" ) );
    
          JAXBElement<Level0Type> l0Element
            = (new ObjectFactory()).createLevelO( l0 );
    
           m.marshal( l0Element, os );
        }
        catch (JAXBException e)
        {
          System.out.println
          (
            "JaxbIndentationTest::main(): " + e.getMessage()
          );
        }
        catch ( IOException e )
        {
          System.out.println
          (
            "JaxbIndentationTest::main(): " + e.getMessage()
          );
        }
      }
    }
    The directory structure I used was as follows:
    test
      |
      +-classes (destination directory for class files)
      |
      +-src (contains schema)
          |
          +-jaxbIndentationTest (contains test code)
    The compiler used to run this test was jdk1.6.0_30_windows_64bit (specified by current project sw docs).

    In the test/src directory, I execute the following command to generate the jaxb source code:
    xjc -d . -p jaxbIndentationTest testIndentationDepthSchema.xsd
    This generates the jaxb source code in the test/src/jaxbIndentationTest directory.

    Then, in the test directory, I execute the following commands:
    javac -d classes src/jaxbIndentationTest/*.java
    java -cp classes jaxbIndentationTest.JaxbIndentationTest
    This creates the file exportData.xml in the test directory. Examining this file in a text editor with word wrap off, you will see that between tags *<level7>* and *<level8>*, the indentation returns to the left margin, and subsequent levels are indented based on the level of indentation of the previous tag.

    To show that this is in the file itself, I ran the (cygwin) command od -c exportData.xml with the following result:
    0000000   <   ?   x   m   l       v   e   r   s   i   o   n   =   "   1
    0000020   .   0   "       e   n   c   o   d   i   n   g   =   "   U   T
    0000040   F   -   8   "       s   t   a   n   d   a   l   o   n   e   =
    0000060   "   y   e   s   "   ?   >  \n   <   l   e   v   e   l   O   >
    0000100  \n                   <   l   e   v   e   l   1   >  \n
    0000120                           <   l   e   v   e   l   2   >  \n
    0000140                                               <   l   e   v   e
    0000160   l   3   >  \n
    0000200                   <   l   e   v   e   l   4   >  \n
    0000220
    0000240       <   l   e   v   e   l   5   >  \n
    0000260
    0000300           <   l   e   v   e   l   6   >  \n
    0000320
    0000340                               <   l   e   v   e   l   7   >  \n
    0000360   <   l   e   v   e   l   8   >  \n                   <   l   e
    0000400   v   e   l   9   >  \n                                   <   l
    0000420   e   v   e   l   1   0   >   F   i   n   a   l       l   e   v
    0000440   e   l   <   /   l   e   v   e   l   1   0   >  \n
    0000460       <   /   l   e   v   e   l   9   >  \n   <   /   l   e   v
    0000500   e   l   8   >  \n
    0000520
    0000540       <   /   l   e   v   e   l   7   >  \n
    0000560
    0000600               <   /   l   e   v   e   l   6   >  \n
    0000620
    0000640       <   /   l   e   v   e   l   5   >  \n
    0000660                                               <   /   l   e   v
    0000700   e   l   4   >  \n
    0000720       <   /   l   e   v   e   l   3   >  \n
    0000740               <   /   l   e   v   e   l   2   >  \n
    0000760       <   /   l   e   v   e   l   1   >  \n   <   /   l   e   v
    0001000   e   l   O   >  \n
    0001005
    You can clearly see that between the *<level7>* and *<level8>* tags, there are no spaces. That is, the indentation level is reset to 0 after 8 levels of indentation.

    I would appreciate any help anyone can give me in turning off this behavior, so that the indentation level does not reset.

    Thanks.

    ¦{Þ                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
  • 6. Re: JAXB, JAXB_FORMATTED_OUTPUT property, levels of indentation
    jtahlborn Expert
    Currently Being Moderated
    yep, looks like this is hardcoded into the default oracle jaxb impl (com/sun/xml/internal/bind/v2/runtime/output/IndentingUTF8XmlOutput.java), which supports at most 8 units of indentation:
        private void printIndent() throws IOException {
            write('\n');
            int i = depth%8;
    
            write( indent8.buf, 0, i*unitLen );
    
            i>>=3;  // really i /= 8;
    
            for( ; i>0; i-- )
                indent8.write(this);
        }
  • 7. Re: JAXB, JAXB_FORMATTED_OUTPUT property, levels of indentation
    834151 Newbie
    Currently Being Moderated
    jtahlborn wrote:
    yep, looks like this is hardcoded into the default oracle jaxb impl (com/sun/xml/internal/bind/v2/runtime/output/IndentingUTF8XmlOutput.java), which supports at most 8 units of indentation:
    private void printIndent() throws IOException {
    write('\n');
    int i = depth%8;
    
    write( indent8.buf, 0, i*unitLen );
    
    i>>=3;  // really i /= 8;
    
    for( ; i>0; i-- )
    indent8.write(this);
    }
    <sigh/> Hardcoded. So, the answer is ... I can't control this.

    I looked for the source to the marshaller implementation class, but it was not included with the source release with the JDK. At that point, I stopped looking ... my bad. Where do you find this source, if you don't mind me asking.

    Thanks.

    ¦{Þ                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
  • 8. Re: JAXB, JAXB_FORMATTED_OUTPUT property, levels of indentation
    jtahlborn Expert
    Currently Being Moderated
    sharkura wrote:
    I looked for the source to the marshaller implementation class, but it was not included with the source release with the JDK. At that point, I stopped looking ... my bad. Where do you find this source, if you don't mind me asking.
    the openjdk project has the complete source for jdk 1.6 and 1.7.

    if you really need this, you could marshal to DOM and then use a Transformer to do the formatting, which may not have the same limitation. or, if you need something more performant, you could marshal to a SAX or StAX result and possibly have that API control the formatting (or possibly do the indentation yourself).

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points