users@jaxb.java.net

Re: How can I specify length constraints?

From: Andy Davidson <andy_davidson_at_apple.com>
Date: Thu, 3 Mar 2011 11:16:58 -0800

Hi David

I think you may have a misunderstanding about how xml schema maps to a 3g lang run time like java.

Tools and frameworks like XJC/JAXB take XML Schema as input and generate a programming specific data model. In the case of XJC/JAXB the generated data models are POJO's. The data models do not have any validation logic in them. The validation is done using a XML validating parser as part of the Marshall/UnMarshall process.

Here some code you might find helpful. Often you only validate when going from xml to your 3g data objects. If you want you can validation to the marshal operation

Andy

p.s. you might want to check out users_at_jaxb.java.net


  private Unmarshaller createUnMarshaller(String packageName,
            String xsdFileName) {
        Unmarshaller unmarshaller = null;

        try {
            JAXBContext jc;
            // see
            // http://download.oracle.com/javase/6/docs/api/javax/xml/bind/JAXBContext.html
            // you can pass a colon separated list of packages.
            jc = JAXBContext.newInstance(packageName);

            unmarshaller = jc.createUnmarshaller();
        } catch (JAXBException e) {
            err.println(" [ERROR] " + e);
            e.printStackTrace();
            System.exit(1);
        }

        // set up validation
        SchemaFactory factory = SchemaFactory
                .newInstance("http://www.w3.org/2001/XMLSchema");
        Schema schema = null;
        try {
            schema = factory.newSchema(new File(xsdFileName));

        } catch (SAXParseException spe) {
            err.println("[ERROR] Parse error file:" + xsdFileName + " line:"
                    + spe.getLineNumber() + " col:" + spe.getColumnNumber()
                    + "\n\t" + spe.getMessage());
            System.exit(1);

        } catch (SAXException e) {
            err.println(" [ERROR] " + e);
            e.printStackTrace();
            System.exit(1);
        }

        unmarshaller.setSchema(schema);
        return unmarshaller;
    }


On Mar 1, 2011, at 3:42 PM, Benson Margulies wrote:

> David,
>
> JAX-B is not a complete representation of XML Schema. There are many
> schema constructs that do not correspond to any @nnotation. If you run
> xsd-to-java they get dropped, and of course in the other direction
> they never appear.
>
>
>
> On Tue, Mar 1, 2011 at 6:30 PM, KARR, DAVID (ATTSI) <dk068x_at_att.com> wrote:
>>> -----Original Message-----
>>> From: KARR, DAVID (ATTSI)
>>> Sent: Tuesday, March 01, 2011 3:17 PM
>>> To: users_at_jaxb.java.net
>>> Subject: How can I specify length constraints?
>>>
>>> When I write pure XML Schema, I can define a simple type based on
>>> string
>>> that has length restrictions, being a minimum and maximum length. I
>>> don't see a way to do that with JAXB annotations. Assuming I've just
>>> missed it, will schemagen respect that annotation in the generated
>>> schema?
>>
>> Is the only reasonable strategy to have a hand-coded "simple types"
>> schema, and to have your JAXB classes reference those types?
>>