660 likes | 802 Views
Lecture 2.1. 2. Outline. Introduction to XML SchemaXML Schema components
E N D
1. Lecture 2.1 1 Data Modeling II
XML Schema & JAXB
2. Lecture 2.1 2 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
3. Lecture 2.1 3 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.
4. Lecture 2.1 4 XML Schema http://www.w3.org/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.
5. Lecture 2.1 5 XML Schema
6. Lecture 2.1 6 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
7. Lecture 2.1 7 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.
8. Lecture 2.1 8 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
9. Lecture 2.1 9 XML Schema - Components Helper Components
Annotations
Model groups
Particles
Wildcards
Attribute uses
10. Lecture 2.1 10 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.
11. Lecture 2.1 11 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.
12. Lecture 2.1 12 XML Schema – Simple Types
13. Lecture 2.1 13 XML Schema – Simple Types
14. Lecture 2.1 14 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.
15. Lecture 2.1 15 XML Schema – Complex Types
16. Lecture 2.1 16 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.
17. Lecture 2.1 17 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.
18. Lecture 2.1 18 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>
19. Lecture 2.1 19 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.
20. Lecture 2.1 20 Content Models
21. Lecture 2.1 21 Content Models - simpleType <simpleType> allows you to create simple content as well, in addition to adding restrictions (or constraints).
22. Lecture 2.1 22 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>
23. Lecture 2.1 23 Content Models - complexType <simpleContent> is used to describe data with only text and no child elements.
It can be a restriction or extension.
24. Lecture 2.1 24 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.
25. Lecture 2.1 25 Content Models - complexType
26. Lecture 2.1 26 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.
27. Lecture 2.1 27 Attribute Declarations
28. Lecture 2.1 28 Attribute Declarations
29. Lecture 2.1 29 Attribute Declarations
30. Lecture 2.1 30 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.
31. Lecture 2.1 31 Model Groups - choice
32. Lecture 2.1 32 Model Groups - sequence
33. Lecture 2.1 33 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
34. Lecture 2.1 34 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.
35. Lecture 2.1 35
36. Lecture 2.1 36 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.
37. Lecture 2.1 37 Datatypes – Facets Constraining facets
length
minLength
maxLength
pattern
enumeration
whitespace
minInclusive/minExclusive
maxInclusive/maxExclusive
totalDigits/fractionDigits
38. Lecture 2.1 38 Datatypes
39. Lecture 2.1 39 XML Schema
Questions?
40. Lecture 2.1 40 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
41. Lecture 2.1 41 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.
42. Lecture 2.1 42 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.
43. Lecture 2.1 43 Availability The JAXB Reference Implementation was produced by SUN Microsystems.
http://java.sun.com/xml/jaxb
Version 1.0
Part of the Java Web Services Developer Pack (JWSDP)
44. Lecture 2.1 44 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
45. Lecture 2.1 45 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.
46. Lecture 2.1 46 JAXB Architecture
47. Lecture 2.1 47 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.
48. Lecture 2.1 48 Binding Compiler
49. Lecture 2.1 49 Java Representation Set of interfaces which models the XML Schema hierarchy.
Like JavaBeans, uses get and set methods for properties
Implementation classes also generated.
50. Lecture 2.1 50 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.
51. Lecture 2.1 51 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
52. Lecture 2.1 52 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.
53. Lecture 2.1 53 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.
54. Lecture 2.1 54 JAXBContext
55. Lecture 2.1 55 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.
56. Lecture 2.1 56 Validator
57. Lecture 2.1 57 Unmarshaller
58. Lecture 2.1 58 Unmarshalling
59. Lecture 2.1 59 Marshaller
60. Lecture 2.1 60 Marshalling
61. Lecture 2.1 61 Default Bindings
62. Lecture 2.1 62 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.
63. Lecture 2.1 63 Java Representation
64. Lecture 2.1 64
65. Lecture 2.1 65 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.
66. Lecture 2.1 66 Not Covered Details about default bindings such as lists (sequence) , and model groups.
Custom bindings
67. Lecture 2.1 67 JAXB
Questions?