Page 1 of 1

Pretty printed XML in .docx file

PostPosted: Thu Dec 11, 2014 1:03 am
by zluspai
Dear Jason,

I'm having some problems with my generated .docx file using docx4j: Word just does not open it and it just tells me nonsense messages like I've problem in row 0 column 12334234 or something. I guess this is because in the document.xml everything is in 1/single line. It would be great if that .xml (and others too) would be pretty printed, so mere humans ;-) could find where the error is.

Is there a way to turn pretty printing on? I was using simply the wordMLPackage.save() so far.

Thanks,
Zoltan

Re: Pretty printed XML in .docx file

PostPosted: Thu Dec 11, 2014 4:47 am
by zluspai
I've tried to set JAXB_FORMATTED_OUTPUT to true on Marshallers, but does not help:
Code: Select all
   static {
      Context.jcDocPropsCore = turnOnPrettyPrint(Context.jcDocPropsCore);
      Context.jcDocPropsCustom = turnOnPrettyPrint(Context.jcDocPropsCustom);
      Context.jcDocPropsExtended = turnOnPrettyPrint(Context.jcDocPropsExtended);
      Context.jcRelationships = turnOnPrettyPrint(Context.jcRelationships);
      Context.jcCustomXmlProperties = turnOnPrettyPrint(Context.jcCustomXmlProperties);
      Context.jcContentTypes = turnOnPrettyPrint(Context.jcContentTypes);
      Context.jcSectionModel = turnOnPrettyPrint(Context.jcSectionModel);
      Context.jcXmlDSig = turnOnPrettyPrint(Context.jcXmlDSig);
      Context.jcMCE = turnOnPrettyPrint(Context.jcMCE);
   }

   private static JAXBContext turnOnPrettyPrint(final JAXBContext decorated) {
      JAXBContext withPrettyPrint = new JAXBContext() {
         
         @SuppressWarnings("deprecation")
         @Override
         public Validator createValidator() throws JAXBException {
            return decorated.createValidator();
         }
         
         @Override
         public Unmarshaller createUnmarshaller() throws JAXBException {
            return decorated.createUnmarshaller();
         }
         
         @Override
         public Marshaller createMarshaller() throws JAXBException {
            Marshaller m = decorated.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            return m;
         }
      };
      return withPrettyPrint;
   }

Re: Pretty printed XML in .docx file

PostPosted: Thu Dec 11, 2014 6:39 am
by jason
https://github.com/plutext/docx4j/commi ... c1c89ab71d does this

I've had my local env using this for a while now, but if anyone thinks it needs to be configurable (so we can produce non-pretty printed output like before), please speak up.

Re: Pretty printed XML in .docx file

PostPosted: Thu Dec 11, 2014 10:58 pm
by zluspai
Thanks Jason, that makes the xml look pretty (as far as an xml can be).