Page 1 of 1

Sdt JaxbValidationEventHandler [ERROR] : unexpected element

PostPosted: Wed Apr 06, 2011 8:36 pm
by Richard
Hi Jason,

I'm trying to save specific parts of my docx files using marshalling and serialization. Unfortunalty the unmarshalling call to the following String causes the JaxbValidationEventHandler to result in an Error.

Code: Select all
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:sdt xmlns:ns16="http://opendope.org/conditions" xmlns:ns17="http://opendope.org/questions" xmlns:ns15="http://opendope.org/xpaths" xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:ns18="http://opendope.org/components" xmlns:ns19="http://schemas.openxmlformats.org/drawingml/2006/compatibility" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:ns9="http://schemas.openxmlformats.org/drawingml/2006/chartDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:ns12="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:ns7="http://schemas.openxmlformats.org/schemaLibrary/2006/main" xmlns:ns10="http://schemas.openxmlformats.org/drawingml/2006/diagram" xmlns:ns20="http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<w:sdtPr>
<w:rPr>
<w:rFonts w:cs="Arial"/>
<w:b/>
<w:color w:themeColor="text1" w:val="000000"/>
<w:sz w:val="20"/>
<w:szCs w:val="20"/>
</w:rPr>
<w:alias w:val="engagementfuehrer_x590"/>
<w:tag w:val="engagementfuehrer_x590"/>
<w:id w:val="-1455363377"/>
<w:lock w:val="sdtContentLocked"/>
<w:placeholder>
<w:docPart w:val="DefaultPlaceholder_22675703"/>
</w:placeholder>
</w:sdtPr>
<w:sdtContent>
<w:p w:rsidRDefault="006656FA" w:rsidP="00A065B1" w:rsidR="006656FA" w:rsidRPr="006656FA">
<w:pPr>
<w:rPr>
<w:rFonts w:cs="Arial"/>
<w:b/>
<w:color w:themeColor="text1" w:val="000000"/>
<w:sz w:val="20"/>
<w:szCs w:val="20"/>
</w:rPr>
</w:pPr>
<w:r w:rsidRPr="006656FA">
<w:rPr>
<w:rFonts w:cs="Arial"/>
<w:b/>
<w:color w:themeColor="text1" w:val="000000"/>
<w:sz w:val="20"/>
<w:szCs w:val="20"/>
</w:rPr>
<w:t>
engagementfuehrer_x59
</w:t>
</w:r>
<w:r w:rsidR="00A065B1">
<w:rPr>
<w:rFonts w:cs="Arial"/>
<w:b/>
<w:color w:themeColor="text1" w:val="000000"/>
<w:sz w:val="20"/>
<w:szCs w:val="20"/>
</w:rPr>
<w:t>
0
</w:t>
</w:r>
</w:p>
</w:sdtContent>
</w:sdt>


This String is an marshalled SdtElement with one embedded paragraph.

The error message interesstingly is cut at the end so this is no copy/paste error:

Code: Select all
2011-04-06 10:12:07,411 WARN  JaxbValidationEventHandler - [ERROR] : unexpected element (uri:"http://schemas.openxmlformats.org/wordprocessingml/2006/main", local:"p"). Expected elements ar
2011-04-06 10:12:07,411 INFO  JaxbValidationEventHandler - continuing (with possible element/attribute loss)


Other unmarshalled SdtElements get the same error as many times as paragraphs occur in it. Of course, in the result no element is returned.

I've tried to create a minimal example by creating a sdtBlock and adding a paragraph to its content. Marshalling and Unmarshalling fails for this example, too!

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
            P paragraph = new P();
            paragraph.setRsidRDefault("006656FA");
            paragraph.setRsidP("00A065B1");
            paragraph.setRsidR("006656FA");
            paragraph.setRsidRPr("006656FA");
           
            SdtContentBlock content = new SdtContentBlock();
           
            SdtBlock sdtElem = Context.getWmlObjectFactory().createSdtBlock();
            sdtElem.setSdtContent(content);
            sdtElem.getContent().getContent().add(paragraph);

            String s = XmlUtils.marshaltoString(sdtElem, true);
            Object o2 = XmlUtils.unmarshalString(s);
 
Parsed in 0.017 seconds, using GeSHi 1.0.8.4


Can you help me out on this, please?

Richard

Re: JaxbValidationEventHandler - [ERROR] : unexpected elemen

PostPosted: Wed Apr 06, 2011 9:08 pm
by jason
Hi Richard

Your example works for me:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
import org.docx4j.XmlUtils;
import org.docx4j.jaxb.Context;
import org.docx4j.wml.P;
import org.docx4j.wml.SdtBlock;
import org.docx4j.wml.SdtContentBlock;

public class TestUMMMM {

        public static void main(String[] args) throws Exception {
       
    P paragraph = new P();
    paragraph.setRsidRDefault("006656FA");
    paragraph.setRsidP("00A065B1");
    paragraph.setRsidR("006656FA");
    paragraph.setRsidRPr("006656FA");
   
    SdtContentBlock content = new SdtContentBlock();
   
    SdtBlock sdtElem = Context.getWmlObjectFactory().createSdtBlock();
    sdtElem.setSdtContent(content);
    sdtElem.getContent().getContent().add(paragraph);

    String s = XmlUtils.marshaltoString(sdtElem, true);
   
    System.out.println(s);
   
    Object o2 = XmlUtils.unmarshalString(s);

        }      
}
 
Parsed in 0.016 seconds, using GeSHi 1.0.8.4


I tested with svn head, but I'd be very surprised if it didn't work with 2.6.0. So, something odd about your environment? Changes to docx4j src?

You can stop the error message being truncated at 120 chars, by setting logging on JaxbValidationEventHandler to DEBUG. The message is truncated by default, since it is very long, and not very useful :-)

cheers .. Jason

Re: JaxbValidationEventHandler - [ERROR] : unexpected elemen

PostPosted: Thu Apr 07, 2011 12:24 am
by Richard
Thanks for your reply.

I'm also using the latest svn version and there is nothing special about my environment (Java 6.0.23 / JAXB: Java 6 implementation).

Can you please try to run the following code to see how many elements are in the sdtElement after the unmarshalling?!

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
import org.docx4j.XmlUtils;
import org.docx4j.jaxb.Context;
import org.docx4j.wml.P;
import org.docx4j.wml.R;
import org.docx4j.wml.SdtBlock;
import org.docx4j.wml.SdtContentBlock;

public class UnMarshall {

    public static void main(String[] args) throws Exception {
       
    P paragraph = new P();
    R run = new R();

    SdtContentBlock content = new SdtContentBlock();
   
    SdtBlock sdtElem = Context.getWmlObjectFactory().createSdtBlock();
    sdtElem.setSdtContent(content);
    sdtElem.getContent().getContent().add(paragraph);
    sdtElem.getContent().getContent().add(run);

    String s = XmlUtils.marshaltoString(sdtElem, true);
   
    System.out.println(s);
   
    System.out.println(((SdtRun) XmlUtils.unmarshalString(s)).getContent().getContent().size());
    }      
}
 
Parsed in 0.016 seconds, using GeSHi 1.0.8.4


For me it prints '1' and has actually only the run in the content list.

Richard

Update: I forgot to mention that I've changed the org.docx4j.wml.P class compared to the svn source, but nothing special here. I've only add a getContent() method, which consists of a lonely call to getParagraphContent().

Re: JaxbValidationEventHandler - [ERROR] : unexpected elemen

PostPosted: Thu Apr 07, 2011 1:27 am
by jason
Of course the content you are creating (w:p and w:r as siblings) is not valid OpenXML.

The reason you get 1 is that docx4j is unmarshalling it as SdtRun.

This can't contain a w:p, so it is dropped:

Code: Select all
06.04.2011 23:21:49 *WARN * JaxbValidationEventHandler: [ERROR] : unexpected element (uri:"http://schemas.openxmlformats.org/wordprocessingml/2006/main", local:"p").
06.04.2011 23:21:49 *INFO * JaxbValidationEventHandler: continuing (with possible element/attribute loss) (JaxbValidationEventHandler.java, line 70)


If instead you force it to unmarshal as an SdtBlock, with:
Code: Select all
    System.out.println(
          ((SdtBlock) XmlUtils.unmarshalString(s, Context.jc, SdtBlock.class )).getContent().getContent().size());
    }       


its the w:r which is dropped:

Code: Select all
06.04.2011 23:30:10 *WARN * JaxbValidationEventHandler: [ERROR] : unexpected element (uri:"http://schemas.openxmlformats.org/wordprocessingml/2006/main", local:"r").


If instead I put 2 paragraphs in the sdtContent, JAXB still unmarshalls it as an SdtRun - unless you specify you want an SdtBlock via unmarshalString(s, Context.jc, SdtBlock.class ).

Re: JaxbValidationEventHandler - [ERROR] : unexpected elemen

PostPosted: Fri Apr 08, 2011 5:59 pm
by Richard
I've noticed that it is always a SdtRun, but didn't know how to unmarshall all kinds of elements correctly.

Now I see the difference and it absolutly makes sense. :)

Thanks for your help, I appreciate it.