Binding XML Schemas to Java Classes with JAXB
An Introduction |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
Schema Type | Java Type |
xsd:boolean | boolean |
xsd:byte | byte |
xsd:dateTime | java.util.Calendar |
xsd:date | java.util.Calendar |
xsd:decimal | java.math.BigDecimal |
xsd:double | double |
xsd:float | float |
xsd:int | int |
xsd:integer | java.math.BigInteger |
xsd:long | long |
xsd:short | short |
xsd:string | java.lang.String |
xsd:time | java.util.Calendar |
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="Customer" type="CustomerType"/> <xsd:complexType name="CustomerType"> <xsd:sequence> <xsd:element name="name" type="xsd:string" /> <xsd:element name="id" type="xsd:positiveInteger" /> </xsd:sequence> </xsd:complexType> </xsd:schema>
// // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v1.0 // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> // Any modifications to this file will be lost upon recompilation of the source schema. // Generated on: 2003.03.19 at 12:12:25 EST // package generated; /** * Java content class for CustomerType complex type. * <p>The following schema fragment specifies the expected content contained within this java content object. * <p> * <pre> * <complexType name="CustomerType"> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <sequence> * <element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/> * <element name="id" type="{http://www.w3.org/2001/XMLSchema}positiveInteger"/> * </sequence> * </restriction> * </complexContent> * </complexType> * </pre> * */ public interface CustomerType { java.lang.String getName(); void setName(java.lang.String value); java.math.BigInteger getId(); void setId(java.math.BigInteger value); }
// // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v1.0 // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> // Any modifications to this file will be lost upon recompilation of the source schema. // Generated on: 2003.03.19 at 12:12:25 EST // package generated.impl; public class CustomerTypeImpl implements generated.CustomerType, com.sun.xml.bind.unmarshaller.UnmarshallableObject, com.sun.xml.bind.serializer.XMLSerializable, com.sun.xml.bind.validator.ValidatableObject { protected java.lang.String _Name; protected java.math.BigInteger _Id; private final static com.sun.msv.grammar.Grammar schemaFragment = com.sun.xml.bind.validator.SchemaDeserializer.deserialize("\u00ac\u00ed\u0000\u0005sr\u0000\u001fcom.sun.msv.grammar.SequenceExp\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0000xr\u0000\u001dcom.sun.msv.grammar.BinaryExp\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0002L\u0000\u0004exp1t\u0000 Lcom/sun/msv/grammar/Expression;L\u0000\u0004exp2q\u0000~\u0000\u0002xr\u0000\u001ecom.sun.msv.grammar.Expression\u00f8\u0018\u0082\u00e8N5~O\u0002\u0000\u0003I\u0000\u000ecachedHashCodeL\u0000\u0013epsilonReducibilityt\u0000\u0013Ljava/lang/Boolean;L\u0000\u000bexpandedExpq\u0000~\u0000\u0002xp\u0003qv\bppsr\u0000\'com.sun.msv.grammar.trex.ElementPattern\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0001L\u0000\tnameClasst\u0000\u001fLcom/sun/msv/grammar/NameClass;xr\u0000\u001ecom.sun.msv.grammar.ElementExp\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0002Z\u0000\u001aignoreUndeclaredAttributesL\u0000\fcontentModelq\u0000~\u0000\u0002xq\u0000~\u0000\u0003\u0001\u00fcn]pp\u0000sr\u0000\u001bcom.sun.msv.grammar.DataExp\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0003L\u0000\u0002dtt\u0000\u001fLorg/relaxng/datatype/Datatype;L\u0000\u0006exceptq\u0000~\u0000\u0002L\u0000\u0004namet\u0000\u001dLcom/sun/msv/util/StringPair;xq\u0000~\u0000\u0003\u0001\u00fcnRppsr\u0000#com.sun.msv.datatype.xsd.StringType\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0001Z\u0000\risAlwaysValidxr\u0000*com.sun.msv.datatype.xsd.BuiltinAtomicType\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0000xr\u0000%com.sun.msv.datatype.xsd.ConcreteType\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0000xr\u0000\'com.sun.msv.datatype.xsd.XSDatatypeImpl\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0003L\u0000\fnamespaceUrit\u0000\u0012Ljava/lang/String;L\u0000\btypeNameq\u0000~\u0000\u0012L\u0000\nwhiteSpacet\u0000.Lcom/sun/msv/datatype/xsd/WhiteSpaceProcessor;xpt\u0000 http://www.w3.org/2001/XMLSchemat\u0000\u0006stringsr\u0000.com.sun.msv.datatype.xsd.WhiteSpaceProcessor$1\u0013JMoI\u00db\u00a4G\u0002\u0000\u0000xr\u0000,com.sun.msv.datatype.xsd.WhiteSpaceProcessor\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0000xp\u0001sr\u00000com.sun.msv.grammar.Expression$NullSetExpression\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0000xq\u0000~\u0000\u0003\u0000\u0000\u0000\nppsr\u0000\u001bcom.sun.msv.util.StringPair\u00d0t\u001ejB\u008f\u008d\u00a0\u0002\u0000\u0002L\u0000\tlocalNameq\u0000~\u0000\u0012L\u0000\fnamespaceURIq\u0000~\u0000\u0012xpq\u0000~\u0000\u0016q\u0000~\u0000\u0015sr\u0000#com.sun.msv.grammar.SimpleNameClass\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0002L\u0000\tlocalNameq\u0000~\u0000\u0012L\u0000\fnamespaceURIq\u0000~\u0000\u0012xr\u0000\u001dcom.sun.msv.grammar.NameClass\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0000xpt\u0000\u0004namet\u0000\u0000sq\u0000~\u0000\u0006\u0001u\u0007\u00a6pp\u0000sq\u0000~\u0000\n\u0001u\u0007\u009bppsr\u0000,com.sun.msv.datatype.xsd.PositiveIntegerType\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0000xr\u0000$com.sun.msv.datatype.xsd.IntegerType\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0000xr\u0000+com.sun.msv.datatype.xsd.IntegerDerivedType\u0099\u00f1]\u0090&6k\u00be\u0002\u0000\u0000xq\u0000~\u0000\u000fq\u0000~\u0000\u0015t\u0000\u000fpositiveIntegersr\u0000.com.sun.msv.datatype.xsd.WhiteSpaceProcessor$2\u0087z9\u00ee\u00f8,N\u0005\u0002\u0000\u0000xq\u0000~\u0000\u0018q\u0000~\u0000\u001bsq\u0000~\u0000\u001cq\u0000~\u0000)q\u0000~\u0000\u0015sq\u0000~\u0000\u001et\u0000\u0002idq\u0000~\u0000\"sr\u0000\"com.sun.msv.grammar.ExpressionPool\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002\u0000\u0001L\u0000\bexpTablet\u0000/Lcom/sun/msv/grammar/ExpressionPool$ClosedHash;xpsr\u0000-com.sun.msv.grammar.ExpressionPool$ClosedHash\u00d7j\u00d0N\u00ef\u00e8\u00ed\u001c\u0002\u0000\u0004I\u0000\u0005countI\u0000\tthresholdL\u0000\u0006parentq\u0000~\u00000[\u0000\u0005tablet\u0000![Lcom/sun/msv/grammar/Expression;xp\u0000\u0000\u0000\u0001\u0000\u0000\u00009pur\u0000![Lcom.sun.msv.grammar.Expression;\u00d68D\u00c3]\u00ad\u00a7\n\u0002\u0000\u0000xp\u0000\u0000\u0000\u00bfpppppppppppppppppppppppppppppppppppppppppppppppppppppppq\u0000~\u0000\u0005ppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp"); private final static java.lang.Class PRIMARY_INTERFACE_CLASS() { return generated.CustomerType.class; } public java.lang.String getName() { return _Name; } public void setName(java.lang.String value) { _Name = value; } public java.math.BigInteger getId() { return _Id; } public void setId(java.math.BigInteger value) { _Id = value; } public com.sun.xml.bind.unmarshaller.ContentHandlerEx getUnmarshaller(com.sun.xml.bind.unmarshaller.UnmarshallingContext context) { return new generated.impl.CustomerTypeImpl.Unmarshaller(context); } public java.lang.Class getPrimaryInterfaceClass() { return PRIMARY_INTERFACE_CLASS(); } public void serializeElements(com.sun.xml.bind.serializer.XMLSerializer context) throws org.xml.sax.SAXException { context.startElement("", "name"); context.endAttributes(); try { context.text(((java.lang.String) _Name)); } catch (java.lang.Exception e) { com.sun.xml.bind.marshaller.Util.handlePrintConversionException(this, e, context); } context.endElement(); context.startElement("", "id"); context.endAttributes(); try { context.text(javax.xml.bind.DatatypeConverter.printInteger(((java.math.BigInteger) _Id))); } catch (java.lang.Exception e) { com.sun.xml.bind.marshaller.Util.handlePrintConversionException(this, e, context); } context.endElement(); } public void serializeAttributes(com.sun.xml.bind.serializer.XMLSerializer context) throws org.xml.sax.SAXException { } public void serializeAttributeBodies(com.sun.xml.bind.serializer.XMLSerializer context) throws org.xml.sax.SAXException { } public java.lang.Class getPrimaryInterface() { return (generated.CustomerType.class); } public com.sun.msv.verifier.DocumentDeclaration createRawValidator() { return new com.sun.msv.verifier.regexp.REDocumentDeclaration(schemaFragment); } public class Unmarshaller extends com.sun.xml.bind.unmarshaller.ContentHandlerEx { public Unmarshaller(com.sun.xml.bind.unmarshaller.UnmarshallingContext context) { super(context, "-------"); } protected com.sun.xml.bind.unmarshaller.UnmarshallableObject owner() { return generated.impl.CustomerTypeImpl.this; } public void enterElement(java.lang.String ___uri, java.lang.String ___local, org.xml.sax.Attributes __atts) throws com.sun.xml.bind.unmarshaller.UnreportedException { switch (state) { case 3 : if (("" == ___uri)&&("id" == ___local)) { context.pushAttributes(__atts); state = 4; return ; } break; case 0 : if (("" == ___uri)&&("name" == ___local)) { context.pushAttributes(__atts); state = 1; return ; } break; case 6 : revertToParentFromEnterElement(___uri, ___local, __atts); return ; } super.enterElement(___uri, ___local, __atts); } public void leaveElement(java.lang.String ___uri, java.lang.String ___local) throws com.sun.xml.bind.unmarshaller.UnreportedException { switch (state) { case 6 : revertToParentFromLeaveElement(___uri, ___local); return ; case 5 : if (("" == ___uri)&&("id" == ___local)) { context.popAttributes(); state = 6; return ; } break; case 2 : if (("" == ___uri)&&("name" == ___local)) { context.popAttributes(); state = 3; return ; } break; } super.leaveElement(___uri, ___local); } public void enterAttribute(java.lang.String ___uri, java.lang.String ___local) throws com.sun.xml.bind.unmarshaller.UnreportedException { switch (state) { case 6 : revertToParentFromEnterAttribute(___uri, ___local); return ; } super.enterAttribute(___uri, ___local); } public void leaveAttribute(java.lang.String ___uri, java.lang.String ___local) throws com.sun.xml.bind.unmarshaller.UnreportedException { switch (state) { case 6 : revertToParentFromLeaveAttribute(___uri, ___local); return ; } super.leaveAttribute(___uri, ___local); } public void text(java.lang.String value) throws com.sun.xml.bind.unmarshaller.UnreportedException { try { switch (state) { case 6 : revertToParentFromText(value); return ; case 4 : try { _Id = javax.xml.bind.DatatypeConverter.parseInteger(com.sun.xml.bind.WhiteSpaceProcessor.collapse(value)); } catch (java.lang.Exception e) { handleParseConversionException(e); } state = 5; return ; case 1 : try { _Name = value; } catch (java.lang.Exception e) { handleParseConversionException(e); } state = 2; return ; } } catch (java.lang.RuntimeException e) { handleUnexpectedTextException(value, e); } } } }
// // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v1.0 // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> // Any modifications to this file will be lost upon recompilation of the source schema. // Generated on: 2003.03.19 at 12:12:25 EST // package generated; /** * This object contains factory methods for each Java content interface and Java element interface generated in the generated package. <p>An ObjectFactory allows you to programatically construct new instances of the Java representation for XML content. The Java representation of XML content can consist of schema derived interfaces and classes representing the binding of schema type definitions, element declarations and model groups. Factory methods for each of these are provided in this class. * */ public class ObjectFactory extends com.sun.xml.bind.DefaultJAXBContextImpl { private static java.util.HashMap defaultImplementations = new java.util.HashMap(); static { defaultImplementations.put((generated.CustomerType.class), (generated.impl.CustomerTypeImpl.class)); defaultImplementations.put((generated.Customer.class), (generated.impl.CustomerImpl.class)); } /** * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: generated * */ public ObjectFactory() { super(new generated.ObjectFactory.GrammarInfoImpl()); } /** * Create an instance of the specified Java content interface. * * @param javaContentInterface the Class object of the javacontent interface to instantiate * @return a new instance * @throws JAXBException if an error occurs */ public java.lang.Object newInstance(java.lang.Class javaContentInterface) throws javax.xml.bind.JAXBException { return super.newInstance(javaContentInterface); } /** * Get the specified property. This method can only be * used to get provider specific properties. * Attempting to get an undefined property will result * in a PropertyException being thrown. * * @param name the name of the property to retrieve * @return the value of the requested property * @throws PropertyException when there is an error retrieving the given property or value */ public java.lang.Object getProperty(java.lang.String name) throws javax.xml.bind.PropertyException { return super.getProperty(name); } /** * Set the specified property. This method can only be * used to set provider specific properties. * Attempting to set an undefined property will result * in a PropertyException being thrown. * * @param name the name of the property to retrieve * @param value the value of the property to be set * @throws PropertyException when there is an error processing the given property or value */ public void setProperty(java.lang.String name, java.lang.Object value) throws javax.xml.bind.PropertyException { super.setProperty(name, value); } /** * Create an instance of CustomerType * * @throws JAXBException if an error occurs */ public generated.CustomerType createCustomerType() throws javax.xml.bind.JAXBException { return ((generated.CustomerType) newInstance((generated.CustomerType.class))); } /** * Create an instance of Customer * * @throws JAXBException if an error occurs */ public generated.Customer createCustomer() throws javax.xml.bind.JAXBException { return ((generated.Customer) newInstance((generated.Customer.class))); } private static class GrammarInfoImpl extends com.sun.xml.bind.GrammarInfo { public java.lang.Class getDefaultImplementation(java.lang.Class javaContentInterface) { return ((java.lang.Class) defaultImplementations.get(javaContentInterface)); } public java.lang.Class getRootElement(java.lang.String uri, java.lang.String local) { if (("" == uri)&&("Customer" == local)) { return (generated.impl.CustomerImpl.class); } return null; } public java.lang.String[] getProbePoints() { return new java.lang.String[] {"", "Customer"}; } } }
Customer c; ObjectFactory factory; factory = new ObjectFactory(); c = factory.createCustomer();
<?xml version="1.0"?> <Customer> <name>David Bernstein</name> <id>10209</id> </Customer>
Customer c; JAXBContext jc; Unmarshaller u; jc = JAXBContext.newInstance(packageName); u = jc.createUnmarshaller(); c = (Customer)u.unmarshal(new FileInputStream(fileName));
Customer c; JAXBContext jc; Marshaller m; c = new Customer(); // Set and/or modify attributes of c m = jc.createMarshaller(); m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE ); m.marshal(c, System.out);