CERN European Organization for Nuclear Research IT Department

CERN European Organization for Nuclear Research IT Department e-Business Section JAXB JAXB XML XML Afonin Alexey, Derek Mathieson e-Business section, IT department CERN Geneva, Switzerland JAXB

JAXB JAXB: The Java Architecture for XML Binding 8 2003 http://java.sun.com/xml/downloads/jaxb.html : SUN, Java Web Services Developer Pack http://java.sun.com/xml/downloads/jaxb.html Apache JAXME http://ws.apache.org/jaxme/ JAXB JAXB : : XML XML

1001 2005-01-01 First Order 10 1002 2005-01-02 Second Order 20.2 : : XML XML Schema Schema

: : Java Java : %jwsdp.home%\jaxb\bin\xjc.bat -p generated -d src simple-order.xsd Ant:

: : Java Java OrdersListType.java public interface OrdersListType { java.util.List getOrder(); } OrderType.java public interface OrderType { double getAmount(); void setAmount(double value); java.lang.String getDescription(); void setDescription(java.lang.String value); java.util.Calendar getDate(); void setDate(java.util.Calendar value); long getNumber();

void setNumber(long value); } : : JAXB JAXB XML JAXBContext jaxbCtx = JAXBContext.newInstance("generated"); Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller(); OrdersListType orders = (OrdersListType)unmarshaller.unmarshal( new File("simple-order.xml")); List ordersList = orders.getOrder(); for (int i = 0; i < ordersList.size(); i++) { OrderType order = (OrderType)ordersList.get(i); System.out.println("Number = " + order.getNumber() + ", amount = " + order.getAmount()); }

... : : JAXB JAXB XML ... ObjectFactory objectFactory = new ObjectFactory(); Order newOrder = objectFactory.createOrder(); newOrder.setNumber(2006); newOrder.setDate(Calendar.getInstance()); newOrder.setDescription("New Order"); newOrder.setAmount(100); orders.getOrder().add(newOrder); Marshaller marshaller = jaxbCtx.createMarshaller(); marshaller.marshal(orders, new FileOutputStream(new File("simple-order.xml")));

Binding Framework : (marshalling)marshalling) (marshalling)unmarshalling) (marshalling)validation) javax.xml.bind.JAXBContext - " " JAXB API " " JAXB API JAXB API public abstract class JAXBContext { static JAXBContext newInstance(String contextPath) static JAXBContext newInstance(String contextPath, ClassLoader contextPathCL) abstract Unmarshaller createUnmarshaller(); abstract Marshaller createMarshaller(); abstract Validator createValidator(); }

XML XML JAXB XML : (marshalling)unmarshall-time validation) (marshalling)on-demand validation) " " JAXB API " " JAXB API (marshalling)fail-fast validation) Validator public interface Validator { ValidationEventHandler getEventHandler() void setEventHandler(ValidationEventHandler handler) boolean validate(java.lang.Object subrootObject) boolean validateRoot(java.lang.Object rootObject) }

Unmarshaller XML , public interface Unmarshaller { boolean isValidating() throws JAXBException; void setValidating(boolean b) throws JAXBException; ValidationEventHandler getEventHandler() throws JAXBException; void setEventHandler(ValidationEventHandler handler) throws JAXBException; Object Object Object Object Object Object } unmarshal(File file) throws JAXBException;

unmarshal(InputStream is) throws JAXBException; unmarshal(URL url) throws JAXBException; unmarshal(Source source) throws JAXBException; unmarshal(Node node) throws JAXBException; unmarshal(InputSource is) throws JAXBException; Marshaller XML public interface Marshaller { ValidationEventHandler getEventHandler() throws JAXBException; void setEventHandler(ValidationEventHandler handler) throws JAXBException; void void void void void Node

marshal(Object marshal(Object marshal(Object marshal(Object marshal(Object getNode(Object o, o, o, o, o, o) OutputStream os) throws JAXBException; Writer writer) throws JAXBException; Result result) throws JAXBException; Node node) throws JAXBException;

ContentHandler ch) throws JAXBException; throws JAXBException; Object getProperty(String s) throws PropertyException; void setProperty(String s, Object o) throws PropertyException; String JAXB_ENCODING = "jaxb.encoding"; String JAXB_FORMATTED_OUTPUT = "jaxb.formatted.output"; } XML Java Binding XML Schema to Java Classes : : XML XML Java Java

XML MixedCaseName getMixedCaseName MIXED_CASE_NAME Answer42 getAnswer42 ANSWER_42

name-with-dashes NameWithDashes getNameWithDashes NAME_WITH_DASHES other_punct-chars OtherPunctChars getOtherPunctChars OTHER_PUNCT_CHARS mixedCaseName

Answer42 XML Java public interface PersonType { long getDepartmentId(); void setDepartmentId(long value);

java.lang.String getName(); void setName(java.lang.String value); long getId(); void setId(long value); } P (marshalling)(marshalling)Properties) Properties) Properties) roperties) XML Java (marshalling)Simple Property) java.lang.String getName(); void setName(java.lang.String value);

(marshalling)Collection Property) java.util.List getItem(); TType (marshalling)(marshalling)Type Safe Enumeration Enumeration)) Type ype Safe (marshalling)Type Type Safe Enumeration)

public class CountryType { private final String value; protected CountryType(String v) { value = v; } public final static CountryType FRANCE = new CountryType("FRANCE"); public final static CountryType SWITZERLAND = new generated.CountryType("SWITZERLAND"); ... }

(marshalling)Simple Built-in Types) XML Java xsd:string java.lang.String xsd:integer java.math.BigInteger xsd:int int xsd:long

long xsd:decimal java.math.BigDecimal xsd:float float xsd:double double xsd:boolean boolean xsd:byte

byte xsd:dateTime java.util.Calendar xsd:anySimpleType java.lang.String XML (marshalling)restriction restriction) (marshalling)extension) Java

XML (marshalling)restriction restriction)

(marshalling)extension) Java public interface Address { java.lang.String getCity(); void setCity(java.lang.String value); java.lang.String getStreet(); void setStreet(java.lang.String value); java.lang.String getName(); void setName(java.lang.String value); } public interface USAddress extends Address { java.lang.String getState(); void setState(java.lang.String value); long getZip(); void setZip(long value); }

Customizing JAXB Bindings ? ? (marshalling)Default Default Binding) : (marshalling)Javadoc Javadoc) /

XML *

xjc b Ant [ collectionType = "collectionType" ] [ generateIsSetMethod= "true" | "false" | "1" | "0" ] [ enableFailFastCheck = "true" | "false" | "1" | "0" ] [ underscoreBinding = "asWordSeparator" | "asCharInWord" ] [ typesafeEnumBase = "typesafeEnumBase" ]

[ ... ]* [ package ] [ ... ]* ... ] [ ] [ ]

[ ... ] ... ]

[ ... ] ... Java XML

...

... : :

cern.edh.cxml.objects.Money cern.edh.cxml.objects.MoneyType : :

package cern.edh.cxml; public class DateConverter { public static java.util.Date parseDateTz(String lexicalDate) throws ParseException {...} public static String printDateTz(java.util.Date date) {...} } : :

... public interface CXMLType { ... java.util.Date getTimestamp(); void setTimestamp(java.util.Date value); } ...

: : public class DeploymentModeValue { public final static DeploymentModeValue PRODUCTION = new DeploymentModeValue(_PRODUCTION); public final static DeploymentModeValue TEST = new DeploymentModeValue(_TEST); ... }

JAXB JAXB vs. vs. JAXP JAXP JAXB : XML JAXP : , DTD , XSLT XML JAXB JAXB

(marshalling)EDH EDH) B2B: cXML ~ 100 ~ 100 ! ! XML XML XML Java (marshalling)Complex

Complex type definition): ... public interface PersonType { ... } public interface Person extends javax.xml.bind.Element, PersonType { } XML XML XML Java (marshalling)Simple

Simple type definition): public interface Name extends javax.xml.bind.Element { java.lang.String getValue(); void setValue(java.lang.String value); }

:

Recently Viewed Presentations

  • PROJECT PARTNERS Funded by the Government of Ontario

    PROJECT PARTNERS Funded by the Government of Ontario

    A Mood Walks Summit at the end of the project for knowledge exchange and celebration! Mood Walks Newsletter that all participating groups and individuals will be invited to contribute and share their stories! ... At Laurier in Waterloo, we had...
  • MCR_MIL-STD_Update_082610.ppt

    MCR_MIL-STD_Update_082610.ppt

    MIL-STD 881 Update Neil F. Albert MCR, LLC ... and performance management Ensure Industry Participation Mr. Young's memorandum of 9 January 2009 Establishes MIL-HDBK as MIL-STD to ensure consistency in application Enforces April 2005 Memorandum and DFARs clause - WBS...
  • Ozymandias: A Study - Maite Space

    Ozymandias: A Study - Maite Space

    Ozymandias: A Study Written by Percy Bysshe Shelley Essay by Sanger Rainsford Setting in Ozymandias The setting of Ozymandias is a desolate wasteland Deserts represent the sands of time and the expanse of time before and after Ozymandias.
  • Introduction to CSCE 221 - Lab

    Introduction to CSCE 221 - Lab

    Vector ADT. The Vector ADT extends the notion of array by storing a sequence of arbitrary objects. An element can be accessed, inserted or removed by specifying its rank (number of elements preceding it)
  • When God Seems Silent - Amazon S3

    When God Seems Silent - Amazon S3

    Lord, Do Not Keep Silent. 1. Do not keep silent, O God of my praise! 2. For the mouth of the wicked and the mouth of the deceitful have opened against me. They have spoken against me with a lying...
  • Sustaining Institutional Transformation: How Trinity Lost Its ...

    Sustaining Institutional Transformation: How Trinity Lost Its ...

    Civic Virtue Starts at Home:Faith and Freedom for Institutional TransformationRemarks for the Lilly Fellows ConferenceUniversity of ScrantonOctober 19, 2013. President Patricia McGuire. Trinity Washington University. Washington, DC
  • Russian Formalism and American New Criticism

    Russian Formalism and American New Criticism

    Burdens of History. Russian literary criticism in the nineteenth century -literature is the essence of a nation's history. Censorship both in tsarist and Soviet Russia . The formalists were allies of the Futurists and they included linguists, literary historians, and...
  • Insert title here

    Insert title here

    It seeks to inform the Committee on Administrative and Budgetary Affairs (CAAP) on the support of Permanent Observers to the OAS during 2008 in accordance with General Assembly Resolution AG/RES. 2437, which requires the annual reporting of fundraising from external...