users@jaxb.java.net

Re: Generating Java code from XML instance?

From: Jason Harrop <jharrop_at_gmail.com>
Date: Wed, 9 Jul 2008 22:27:22 +1000

> Could you give an example?


Sure.

docx4j uses JAXB to marshal/unmarshal Microsoft's OpenXML.

The following snippet of OpenXML is used to draw a horizontal line across
the page (like HTML's <hr>):

        <w:p>
            <w:pPr>
                <w:pBdr>
                    <w:bottom w:val="single" w:sz="6" w:space="1"
w:color="auto" />
                </w:pBdr>
            </w:pPr>
        </w:p>

I can produce that XML using the classes generated by XJC like this:

                        org.docx4j.wml.ObjectFactory factory = new
org.docx4j.wml.ObjectFactory();
                        org.docx4j.wml.P p = factory.createP();
                        org.docx4j.wml.PPr pPr = factory.createPPr();
                        p.setPPr(pPr);
                        org.docx4j.wml.PPrBase.PBdr bdr =
factory.createPPrBasePBdr();
                        org.docx4j.wml.CTBorder bottom =
factory.createCTBorder();
                        pPr.setPBdr(bdr);
                        bdr.setBottom(bottom);
                        bottom.setVal( org.docx4j.wml.STBorder.SINGLE );
                        bottom.setSz( new java.math.BigInteger("6")); //yuck
                        bottom.setSpace(new java.math.BigInteger("1"));
                        bottom.setColor("auto");

But it would be nice to say, "JAXB, here is the XML I'd like to generate;
pls create some code for me", and get something like the above back.

cheers

Jason