Page 1 of 1

java.lang.ClassCastException: org.docx4j.wml.R cannot be ca

PostPosted: Thu Sep 22, 2016 1:38 am
by abuzekry
I have an Object o, which I'm trying to cast to org.docx4j.wml.Text. get it's parent and then cast to javax.xml.bind.JAXBElement. The parent returns a org.docx4j.wml.R and then the casting gives a java.lang.ClassCastException, is this an expected behaviour? how can I do the casting correctly

The condition I'm trying to check is
Code: Select all
if (!((javax.xml.bind.JAXBElement)((org.docx4j.wml.Text) o).getParent()).getName().getLocalPart().equalsIgnoreCase("instrText"))

Re: java.lang.ClassCastException: org.docx4j.wml.R cannot be

PostPosted: Thu Sep 22, 2016 5:58 am
by jason
For starters, if things aren't working as you expect, don't try to do it all in one line of code...

It looks/sounds like you are trying to cast wml.R to javax.xml.bind.JAXBElement

That won't work, and you should expect the exception. See https://github.com/plutext/docx4j/blob/ ... R.java#L70

I suspect you want to know whether the R content is instrText? At https://github.com/plutext/docx4j/blob/ ... java#L3364

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
    /**
     * Create an instance of {@link JAXBElement }{@code <}{@link Text }{@code >}}
     *
     */

    @XmlElementDecl(namespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main", name = "instrText", scope = R.class)
    public JAXBElement<Text> createRInstrText(Text value) {
        return new JAXBElement<Text>(_RInstrText_QNAME, Text.class, R.class, value);
    }
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


Start by iterating through the content and printing out the name of the objects. If it is a JAXBElement, cast to that and use JAXBElement's methods to see what it wraps.

Re: java.lang.ClassCastException: org.docx4j.wml.R cannot be

PostPosted: Thu Sep 22, 2016 10:31 pm
by abuzekry
Thank you so much for this.