Download presentation
Presentation is loading. Please wait.
Published byLinda Nicholson Modified over 6 years ago
1
Data Modeling II XML Schema & JAXB Marc Dumontier May 4, 2004
Lecture 2.1 (c) 2004 CGDN
2
Outline Introduction to XML Schema XML Schema components & structure
Marc Dumontier May 4, 2004 Outline Introduction to XML Schema XML Schema components & structure XML Schema datatypes Representing and Modeling data Introduction to Java Architecture for XML Binding (JAXB) JAXB Architecture Lecture 2.1 (c) 2004 CGDN
3
Marc Dumontier May 4, 2004 DTDs and XML Schema Document Type Definition (DTD): A DTD defines the elements that can appear in an XML document, the order in which they can appear, how they can be nested inside each other, and other basic details of XML document structure. DTDs are part of the original XML specification. XML Schema: A schema can define all of the document structures that you can put in a DTD, and it can also define data types and more complicated rules than a DTD can. The W3C developed the XML Schema specification. Lecture 2.1 (c) 2004 CGDN
4
XML Schema http://www.w3.org/XML/Schema
Marc Dumontier May 4, 2004 XML Schema XML Schemas are well-formed XML documents. Can be parsed by XML parser Can be referenced using XPath XML Schemas share similar characteristics to classes in object oriented programming languages; and an XML document which conforms to that schema is said to be an ‘instance document’ XML Schema also has object oriented features such as inheritance, and type definitions. Ideal for transitioning XML to programming languages. Lecture 2.1 (c) 2004 CGDN
5
Marc Dumontier May 4, 2004 XML Schema Lecture 2.1 (c) 2004 CGDN
6
Outline Introduction to XML Schema XML Schema components & structure
Marc Dumontier May 4, 2004 Outline Introduction to XML Schema XML Schema components & structure XML Schema datatypes Representing and Modeling data Introduction to Java Architecture for XML Binding (JAXB) JAXB Architecture Lecture 2.1 (c) 2004 CGDN
7
Marc Dumontier May 4, 2004 XML Schema - Structure The <schema> element: The root element of an XML Schema. Has many attributes to specify general information about the schema such as default values for attributes, and target namespace. Customary to use ‘xs’ or ‘xsd’ as the namespace prefix in an XML Schema. Components are the structures which outline the constraints placed on instance documents. Lecture 2.1 (c) 2004 CGDN
8
XML Schema - Components
Marc Dumontier May 4, 2004 XML Schema - Components Primary Components Simple type definitions Complex type definitions Element declarations Attribute declarations Secondary Components Attribute group definitions Identity-constraint definitions Model group definitions Notation declarations Lecture 2.1 (c) 2004 CGDN
9
XML Schema - Components
Marc Dumontier May 4, 2004 XML Schema - Components Helper Components Annotations Model groups Particles Wildcards Attribute uses Lecture 2.1 (c) 2004 CGDN
10
XML Schema – Type Definitions
Marc Dumontier May 4, 2004 XML Schema – Type Definitions There are 2 types of type definitions: simple and complex. They are used to define datatypes which may act as global type definitions or be applied to other components, such as element declarations and attribute declarations. All types are derived from anyType. Lecture 2.1 (c) 2004 CGDN
11
XML Schema – Simple Types
Marc Dumontier May 4, 2004 XML Schema – Simple Types A simple type is a restriction on a primitive type or another user-defined simple type. Can be defined anywhere in the schema (named), or used anonymously within an element declaration. Use the <simpleType> tag. Lecture 2.1 (c) 2004 CGDN
12
XML Schema – Simple Types
Marc Dumontier May 4, 2004 XML Schema – Simple Types Lecture 2.1 (c) 2004 CGDN
13
XML Schema – Simple Types
Marc Dumontier May 4, 2004 XML Schema – Simple Types Lecture 2.1 (c) 2004 CGDN
14
XML Schema – Complex Types
Marc Dumontier May 4, 2004 XML Schema – Complex Types Complex types can be restrictions or extensions. Can be a restriction on a complex type. Can be a restriction on some other complex type. Can be an extension of a simple type. Can be an extension of another complex type. Declared either globally or locally. Lecture 2.1 (c) 2004 CGDN
15
XML Schema – Complex Types
Marc Dumontier May 4, 2004 XML Schema – Complex Types Lecture 2.1 (c) 2004 CGDN
16
Element Declarations Created with the <element> tag.
Marc Dumontier May 4, 2004 Element Declarations Created with the <element> tag. Child elements can be <simpleType>, <complexType>, <simpleContent>, and <complexContent> to specify specify details about the content model for that element. Can specify default or fixed values. Can manage the relationship between related elements. Lecture 2.1 (c) 2004 CGDN
17
Constraining Elements with Attributes
Marc Dumontier May 4, 2004 Constraining Elements with Attributes name – specify the name of the element. default – specify a default value for the element. fixed – forces the instance document to include information. maxOccurs – Specifies how many times an element may appear in the document or content model. 0,1,…n or unbounded. minOccurs – see above. Lecture 2.1 (c) 2004 CGDN
18
Marc Dumontier May 4, 2004 Content Models Describes what content is allowable in an element. If the content consists of only text and no child elements, then the content is said to be simple. Otherwise, it is said to be complex or mixed. The <element> tag can contain the following elements <annotation> <simpleType> <complexType> Lecture 2.1 (c) 2004 CGDN
19
Marc Dumontier May 4, 2004 Content Models XML Schema has built in datatypes such as integer, decimal, date, boolean, and string. If element has no restrictions, use the built-in datatypes directly in the element tag. Lecture 2.1 (c) 2004 CGDN
20
Marc Dumontier May 4, 2004 Content Models Lecture 2.1 (c) 2004 CGDN
21
Content Models - simpleType
Marc Dumontier May 4, 2004 Content Models - simpleType <simpleType> allows you to create simple content as well, in addition to adding restrictions (or constraints). Lecture 2.1 (c) 2004 CGDN
22
Content Models - complexType
Marc Dumontier May 4, 2004 Content Models - complexType <complexType> is used to create content models which consist of element content or mixed content. Valid children of this tag are <simpleContent> <complexContent> <sequence> <choice> <all> <group> Lecture 2.1 (c) 2004 CGDN
23
Content Models - complexType
Marc Dumontier May 4, 2004 Content Models - complexType <simpleContent> is used to describe data with only text and no child elements. It can be a restriction or extension. Lecture 2.1 (c) 2004 CGDN
24
Content Models - complexType
Marc Dumontier May 4, 2004 Content Models - complexType <complexContent> can be used to represent either a complex content model, or a mixed one either as a restriction or an extension. Lecture 2.1 (c) 2004 CGDN
25
Content Models - complexType
Marc Dumontier May 4, 2004 Content Models - complexType Lecture 2.1 (c) 2004 CGDN
26
Attribute Declarations
Marc Dumontier May 4, 2004 Attribute Declarations Attributes are declared with the <attribute> tag. Can have global or local scope. Can have a default or fixed value. Attribute groups can be created using the <attributeGroup> tag. Enumerations can be created using the <enumeration> tag as a restriction. Lecture 2.1 (c) 2004 CGDN
27
Attribute Declarations
Marc Dumontier May 4, 2004 Attribute Declarations Lecture 2.1 (c) 2004 CGDN
28
Attribute Declarations
Marc Dumontier May 4, 2004 Attribute Declarations Lecture 2.1 (c) 2004 CGDN
29
Attribute Declarations
Marc Dumontier May 4, 2004 Attribute Declarations Lecture 2.1 (c) 2004 CGDN
30
Marc Dumontier May 4, 2004 Model Groups Particles: A list of the individual particles in the model group, such as elements. Compositor: Maybe be all, choice, or sequence, which specify how that model group will be used. Annotation: Comments or description about the model group. Lecture 2.1 (c) 2004 CGDN
31
Model Groups - choice Marc Dumontier May 4, 2004 Lecture 2.1
(c) 2004 CGDN
32
Model Groups - sequence
Marc Dumontier May 4, 2004 Model Groups - sequence Lecture 2.1 (c) 2004 CGDN
33
Outline Introduction to XML Schema XML Schema components & structure
Marc Dumontier May 4, 2004 Outline Introduction to XML Schema XML Schema components & structure XML Schema datatypes Representing and Modeling data Introduction to Java Architecture for XML Binding (JAXB) JAXB Architecture Lecture 2.1 (c) 2004 CGDN
34
Datatypes Datatypes are essential to model data properly.
Marc Dumontier May 4, 2004 Datatypes Datatypes are essential to model data properly. Datatypes are a mechanism for us to agree on the representation of the data in our data set. XML Schema provides two type of datatypes: primitive and derived. Lecture 2.1 (c) 2004 CGDN
35
Marc Dumontier May 4, 2004 Lecture 2.1 (c) 2004 CGDN
36
Marc Dumontier May 4, 2004 Primitive Datatypes Primitive datatypes are those that are not defined in terms of other datatypes; they exist ab initio. Derived datatypes are those that are defined in terms of other datatypes. Lecture 2.1 (c) 2004 CGDN
37
Datatypes – Facets Constraining facets length minLength maxLength
Marc Dumontier May 4, 2004 Datatypes – Facets Constraining facets length minLength maxLength pattern enumeration whitespace minInclusive/minExclusive maxInclusive/maxExclusive totalDigits/fractionDigits Lecture 2.1 (c) 2004 CGDN
38
Marc Dumontier May 4, 2004 Datatypes Lecture 2.1 (c) 2004 CGDN
39
XML Schema Questions? Marc Dumontier May 4, 2004 Lecture 2.1
(c) 2004 CGDN
40
Outline Introduction to XML Schema XML Schema components & structure
Marc Dumontier May 4, 2004 Outline Introduction to XML Schema XML Schema components & structure XML Schema datatypes Representing and Modeling data Introduction to Java Architecture for XML Binding (JAXB) JAXB Architecture Lecture 2.1 (c) 2004 CGDN
41
Java Architecture for XML Binding (JAXB)
Marc Dumontier May 4, 2004 Java Architecture for XML Binding (JAXB) A framework for automating the process of mapping XML documents to java data objects. Provides a compiler which takes in an XML Schema as input, and auto-generates the Java code for the data structures. Provides a runtime library to unmarshal XML documents to Java representation, marshall the Java objects to XML, and validates the Java objects against the XML Schema. Lecture 2.1 (c) 2004 CGDN
42
Marc Dumontier May 4, 2004 Why use JAXB? Simplifies using XML by generating objects specific to your model instead of working with SAX or DOM which is very general. Provides a high level API for working with XML documents. Allows customization. Very useful for large data specifications. Eg. BIND has 2,200 individual fields which generated ~3000 classes. Lecture 2.1 (c) 2004 CGDN
43
Marc Dumontier May 4, 2004 Availability The JAXB Reference Implementation was produced by SUN Microsystems. Version 1.0 Part of the Java Web Services Developer Pack (JWSDP) Lecture 2.1 (c) 2004 CGDN
44
Outline Introduction to XML Schema XML Schema components & structure
Marc Dumontier May 4, 2004 Outline Introduction to XML Schema XML Schema components & structure XML Schema datatypes Representing and Modeling data Introduction to Java Architecture for XML Binding (JAXB) JAXB Architecture Lecture 2.1 (c) 2004 CGDN
45
Marc Dumontier May 4, 2004 JAXB Architecture The binding compiler transforms a source schema to a set of content classes in Java. The binding runtime framework provides the interfaces for the functionality of unmarshalling, marshalling and validation for content classes. The binding language is an XML-based language that describes the binding of a source schema to a java representation. Lecture 2.1 (c) 2004 CGDN
46
JAXB Architecture SOURCE: java.sun.com Marc Dumontier May 4, 2004
Lecture 2.1 (c) 2004 CGDN
47
Marc Dumontier May 4, 2004 Binding Compiler Binding a schema means generating a set of Java classes that represents the schema. All JAXB implementations provide a tool called a binding compiler to bind a schema (the way the binding compiler is invoked can be implementation-specific). For example, the JAXB Reference Implementation provides a binding compiler that you can invoke through scripts. Lecture 2.1 (c) 2004 CGDN
48
Binding Compiler SOURCE: java.sun.com Marc Dumontier May 4, 2004
Lecture 2.1 (c) 2004 CGDN
49
Marc Dumontier May 4, 2004 Java Representation Set of interfaces which models the XML Schema hierarchy. Like JavaBeans, uses get and set methods for properties Implementation classes also generated. Lecture 2.1 (c) 2004 CGDN
50
Binding Framework 3 primary operations
Marc Dumontier May 4, 2004 Binding Framework 3 primary operations Unmarshalling: Reading of the XML document and translation to tree of content objects. Marshalling: inverse of unmarshalling; traversal of content tree and writing an XML document that reflects the tree’s content. Validation: validating data in content tree against XML Schema. Lecture 2.1 (c) 2004 CGDN
51
Binding Declarations Allows for the customization of the mapping.
Marc Dumontier May 4, 2004 Binding Declarations Allows for the customization of the mapping. A set of binding declarations are writing in a binding language which itself is XML. Each binding declaration can occur in the <appinfo> tag of a schema or the set can be aggregated in a separate file. Binding declarations allow the developer to override the default binding rules Lecture 2.1 (c) 2004 CGDN
52
Marc Dumontier May 4, 2004 Types of validation A type constraint imposes requirements upon the values that may be provided by constraint facets in simple type definitions. A local structural constraint imposes requirements upon every instance of a given element. (eg. Required attributes have values) A global structural constraint imposes requirements upon an entire document. Lecture 2.1 (c) 2004 CGDN
53
Marc Dumontier May 4, 2004 The Binding Framework The API which provides access to unmarshalling, marshalling, and validation. Root package is javax.xml.bind The JAXBContext class is the entry point into the API. This class allows for the initialization of classes for a particular content tree. Lecture 2.1 (c) 2004 CGDN
54
Marc Dumontier May 4, 2004 JAXBContext Lecture 2.1 (c) 2004 CGDN
55
General Validation Processing
Marc Dumontier May 4, 2004 General Validation Processing Unmarshal-time validation: Validation errors and warnings detected while unmarshalling an instance document. On-demand validation: An application can validate it’s content tree against schema constraints at any time. Fail-fast validation: An application gets notified about validation errors and warnings upon a modification to the content tree. Lecture 2.1 (c) 2004 CGDN
56
Marc Dumontier May 4, 2004 Validator Lecture 2.1 (c) 2004 CGDN
57
Marc Dumontier May 4, 2004 Unmarshaller Lecture 2.1 (c) 2004 CGDN
58
Unmarshalling SOURCE: java.sun.com Marc Dumontier May 4, 2004
Lecture 2.1 (c) 2004 CGDN
59
Marc Dumontier May 4, 2004 Marshaller Lecture 2.1 (c) 2004 CGDN
60
Marshalling SOURCE: java.sun.com Marc Dumontier May 4, 2004
Lecture 2.1 (c) 2004 CGDN
61
Marc Dumontier May 4, 2004 Default Bindings Lecture 2.1 (c) 2004 CGDN
62
Marc Dumontier May 4, 2004 Java Representation A package containts an ObjectFactory class, a jaxb.properties files, a set of interfaces for each content model, a set of interfaces for each element declaration, and the implementation classes. Lecture 2.1 (c) 2004 CGDN
63
Java Representation Marc Dumontier May 4, 2004 Lecture 2.1
(c) 2004 CGDN
64
Marc Dumontier May 4, 2004 Lecture 2.1 (c) 2004 CGDN
65
Creating Content Trees
Marc Dumontier May 4, 2004 Creating Content Trees You don’t need an XML document as input Static factory class (ObjectFactory) to instantiate implementations of interfaces. Use mutator (get/set) methods to set content. Lecture 2.1 (c) 2004 CGDN
66
Marc Dumontier May 4, 2004 Not Covered Details about default bindings such as lists (sequence) , and model groups. Custom bindings Lecture 2.1 (c) 2004 CGDN
67
Marc Dumontier May 4, 2004 JAXB Questions? Lecture 2.1 (c) 2004 CGDN
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.