質問

I'm trying to have docx4j support MOXy as its JAXB implementation.

We're pretty much there; see docx4j and MOXy

The problem I'm having is that I have a class:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main", name = "CT_Text", propOrder = {
    "value"
})
@XmlRootElement(name = "t")
public class Text

MOXy is marshalling this to w:delInstrText, instead of w:t, which is what I'd expect/hope, and what the Java 6 / reference implementations do.

From the schema:

        <xsd:element name="t" type="CT_Text">
            <xsd:annotation>
                <xsd:documentation>Text</xsd:documentation>
            </xsd:annotation>
        </xsd:element>

        <xsd:element name="delInstrText" type="CT_Text">
            <xsd:annotation>
                <xsd:documentation>Deleted Field Code</xsd:documentation>
            </xsd:annotation>
        </xsd:element>

FWIW, ObjectFactory contains:

public Text createText() {
    return new Text();
}

@XmlElementDecl(namespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main", name = "delInstrText", scope = R.class)
public JAXBElement<Text> createRDelInstrText(Text value) {
    return new JAXBElement<Text>(_RDelInstrText_QNAME, Text.class, R.class, value);
}

This is with MOXy jars:

        +- org.eclipse.persistence:org.eclipse.persistence.moxy:jar:2.4.1
        |  +- org.eclipse.persistence:org.eclipse.persistence.core:jar:2.4.1
        |  |  \- org.eclipse.persistence:org.eclipse.persistence.asm:jar:3.3.1.v201206041142
        |  \- org.eclipse.persistence:org.eclipse.persistence.antlr:jar:3.2.0.v201206041011             

Update:

Here is a test case:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;

import org.docx4j.wml.R;
import org.docx4j.wml.Text;


public class MOXyTest {

    public static void main(String[] args) throws Exception {


        JAXBContext jc = JAXBContext.newInstance("org.docx4j.wml");
//        System.out.println(Version.getVersion());
//        System.out.println(jc.getClass());

        R run = new R();
        Text text = new Text();
        run.getContent().add(text);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(run, System.out);

    }
}
役に立ちましたか?

解決

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.


UPDATE

We have been able to reproduce the error you are seeing in EclipseLink 2.4.1. We have not been able to reproduce the issue in the EclipseLink 2.4.2 or 2.5.0 streams. I would recommend downloading the latest 2.4.2 nightly build and trying it out:

We are still investigating this issue to ensure that it is truly fixed.


ORIGINAL ANSWER

So far I have been unable to reproduce the results from your question when MOXy is used as the JAXB provider. Could you provide some additional information to help me reproduce your use case. Below is what I have tried so far:

Java Model

I took the Java model from the following location on GitHub:

jaxb.properties

I added a file called jaxb.properties in the org.docx4j.wml package to enable MOXy as the JAXB provider.

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

Demo

Below is the demo code I used to try and reproduce the issue:

package org.docx4j.wml;

import javax.xml.bind.*;
import org.eclipse.persistence.Version;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance("org.docx4j.wml");
        System.out.println(Version.getVersion());
        System.out.println(jc.getClass());

        Text text = new Text();

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(text, System.out);
    }

}

Output

Below is the output from running the demo code. I'm seeing the proper root element t marshalled out instead of delInstrText as described in the question.

2.4.1
class org.eclipse.persistence.jaxb.JAXBContext
<?xml version="1.0" encoding="UTF-8"?>
<ns0:t xmlns:ns2="http://schemas.openxmlformats.org/schemaLibrary/2006/main" xmlns:ns1="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:ns4="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:ns3="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:ns0="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:ns5="http://schemas.openxmlformats.org/officeDocument/2006/relationships"/>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top