Page 1 of 1

Error in pptx TraverseSlide sample? or in pptx4j?

PostPosted: Mon Mar 05, 2012 7:47 am
by trigeek
When I run this sample against a powerpoint with a single slide that has an embedded chart, I get the following:

Exception in thread "main" java.lang.RuntimeException: javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "org.pptx4j.pml.CTGraphicalObjectFrame" as an element because it is missing an @XmlRootElement annotation]
at org.docx4j.XmlUtils.marshaltoString(XmlUtils.java:438)
at org.docx4j.XmlUtils.marshaltoString(XmlUtils.java:362)
at test.pptx.Traverse$1.apply(Traverse.java:44)
at test.pptx.Traverse$1.walkJAXBElements(Traverse.java:81)
at org.docx4j.TraversalUtil.<init>(TraversalUtil.java:150)
at test.pptx.Traverse.main(Traverse.java:33)
Caused by: javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "org.pptx4j.pml.CTGraphicalObjectFrame" as an element because it is missing an @XmlRootElement annotation]
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(Unknown Source)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source)
at org.docx4j.XmlUtils.marshaltoString(XmlUtils.java:426)
... 5 more
Caused by: com.sun.istack.internal.SAXException2: unable to marshal type "org.pptx4j.pml.CTGraphicalObjectFrame" as an element because it is missing an @XmlRootElement annotation
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(Unknown Source)
... 9 more

Am I doing something incorrectly? The traverse runs fine if I delete the chart from the slide...

Thanks,
Rob

Re: Error in pptx TraverseSlide sample? or in pptx4j?

PostPosted: Mon Mar 05, 2012 11:04 am
by jason
That error occurs when the type CTGraphicalObjectFrame is marshalled by XmlUtils.marshaltoString(o, true, org.pptx4j.jaxb.Context.jcPML)

There is an explanation in the Getting Started guide - JAXB needs clues of one sort or another to marshal certain things on their own, and this is one of them. As per the message, you need a @XmlRootElement annotation (in the source code, which we can do if the type has a 1:1 mapping with an XML element), or you can be explicit when you marshal:

Code: Select all
void marshal(Object jaxbElement,
             Writer writer)
             throws JAXBException


by using constructor JAXBElement(QName, Class, Object) where QName is the XML element's namespace + local name.

If you load then save your pptx by using RoundTripTest, you'll see that this doesn't prevent you from saving the pptx. It only ever sometimes presents an issue in certain when you pick an individual object out of the tree and attempt to marshall it (and in those cases, one or both of the above workarounds are available).

For the purposes of the sample you were running, the following would workaround the error:

Code: Select all
            try {
               System.out.println(indent + o.getClass().getName() + "\n\n" + XmlUtils.marshaltoString(o, true, org.pptx4j.jaxb.Context.jcPML));               
            } catch (RuntimeException me) {               
               System.out.println(indent + o.getClass().getName() );                              
            }


hope this helps .. Jason