Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 157B: Database Management Systems II February 18 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

Similar presentations


Presentation on theme: "CS 157B: Database Management Systems II February 18 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron."— Presentation transcript:

1 CS 157B: Database Management Systems II February 18 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak www.cs.sjsu.edu/~mak

2 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 2 XML Schema  XML Schema Definition (XSD)  Specify the structure of XML data. Application-specific. Replacement for DTD.  An XML document is well-formed if it has correct XML syntax.   An XML document is valid if its contents conform to its schema. The schema specifies what elements and attributes a valid document must have, and in what order. The schema specifies the data type and format of the content. _

3 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 3 XML Schema Types  Simple type An XML element with only text content. No attributes and no child elements.  Complex types Four complex types: Complex typeAttributesChildrenText content Text onlyyes- Element onlyyes - Empty elementyes-- Mixed contentyes

4 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 4 Simple Type Definition  Example:  Some basic types: xs:string xs:integer xs:decimal xs:boolean  false or true (or: 0 or 1) xs:date  format YYYY-MM-DD xs:time  format hh:mm:ss xs:dateTime  format YYYY-MM-ddThh:mm:ss xs:anyURI Note the T

5 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 5 Simple Type Definition  Predefined value Example: If the element is present and has content, the content must match the predefined value. If the element is present but empty, it gets the predefined value. If the element is not present, it has no value.  Default value Example: If the element is empty or not present, it gets the default value. If the element is present and has content, the content that’s there is used as the value. <xs:element name="age" type="xs:integer" fixed="25"/> <xs:element name="age" type="xs:integer" default="25"/>

6 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 6 Derived Simple Types  Derive a custom simple type from a base type. Example:  This is an anonymous custom type. The type itself has no name. It applies only to the element (birthday) for which it is defined. _ <xs:element name="birthday"

7 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 7 Derived Simple Types  Named custom type. The type has a name. Multiple elements can refer to the type by name. Example:

8 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 8 Derived Simple Types  A set of acceptable content values. Example:

9 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 9 Derived Simple Types  A regular expression pattern. Example: Perl-style regular expression.

10 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 10 Derived Simple Types  A union type. Example:

11 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 11 Derived Simple Types  A list type. Example: Therefore, in the XML document, the “holidays” element can have a list of date values: 2013-01-01 2013-07-04 2013-12-25

12 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 12 Sample XML Document <catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="catalog.xsd"> Design XML Schemas Using UML Ayesha Malik Design service-oriented architecture frameworks with J2EE technology Naveen Balani Advance DAO Programming Sean Sullivan Schema reference XML document adapted from the book Pro XML Development with Java Technology, by Ajay Vohra and Deepak Vohra, Apress, 2006

13 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 13 Sample XML Schema: catalog.xsd... XML schema adapted from the book Pro XML Development with Java Technology, by Ajay Vohra and Deepak Vohra, Apress, 2006

14 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 14 Sample XML Schema: catalog.xsd... <xs:element ref="article" minOccurs="0" maxOccurs="unbounded"/>...

15 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 15 Sample XML Schema: catalog.xsd... <xs:element ref="journal" minOccurs="0" maxOccurs="unbounded"/>

16 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 16 Complex Types  Four complex types: Complex typeAttributesChildrenText content Text onlyyes- Element onlyyes - Empty elementyes-- Mixed contentyes

17 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 17 Complex Types: Element Only  Attributes and child elements, no text content. Example: imposes an order on child elements. for child elements in any order. elements come last. _

18 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 18 Complex Types: Element Only  Choice of child elements Example: Either a social security number, or a username followed by a password.

19 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 19 Complex Types: Text Only  Attributes and text content, no child elements. Example:  In this example, an element of type “year_type” must have content that is a positive integer. It must also have an “era” attribute whose value is a string. Use instead of to add restrictions (such as maximum string length) to the base type. _

20 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 20 Complex Types: Empty Element  Attributes only, no text content and no child elements. Example:

21 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 21 Complex Types: Mixed Content  Attributes, child elements, and text content. Example: The elements are interspersed among the text content. Instead of, you can also use or. _...

22 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 22 Attributes  Attributes are simple type elements. Example:  Attributes are optional by default. Add use="required" to the attribute definition to make the attribute mandatory. Add use="prohibited" to prohibit the attribute. _

23 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 23 Validating XML Documents  Install NetBeans XML tools plugins: Schema editor https://blogs.oracle.com/geertjan/entry/xml_schema_editor_in_ netbeans https://blogs.oracle.com/geertjan/entry/xml_schema_editor_in_ netbeans Query window http://plugins.netbeans.org/plugin/15704/query-xml http://plugins.netbeans.org/plugin/15704/query-xml  Validate an XML document using Java’s DOM and SAX parsers. _ Demos

24 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 24 Object-XML Mapping  JAXB 2.0 package. Automatic object-XML mapping. Object bindings: One Java object per XML element. Similar to object-relational mapping with Hibernate.  Use the xjc compiler to generate Java classes for complex types in the XML schema. Complete with Java annotations. Requires an XML schema.  At run time: Marshalling  Generate XML elements from a Java object tree. Unmarshalling  Generate a Java object tree from a XML elements.

25 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 25 Complete XML Schema <xs:element ref="article" minOccurs="0" maxOccurs="unbounded"/> <xs:element ref="journal" minOccurs="0" maxOccurs="unbounded"/> XML schema adapted from the book Pro XML Development with Java Technology, by Ajay Vohra and Deepak Vohra, Apress, 2006

26 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 26 xjc Compiler  Should be part of your standard Java installation.  Command line: is where you want the generated Java classes to go. (It’s the source directory for the Java compiler.) Example:  The xjc compiler generates Java classes for complex types in the XML schema. The classes will be properly annotated for XML mapping. _ xjc –d -p xjc -d src -p jaxbdemo.generated catalog.xsd

27 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 27 Unmarshalling an XML Document  Create an unmarshaller. Put the generated Java classes into package jaxbdemo.generated for this example.  Unmarshal the XML document. Get a Catalog object. Get the list of Journal objects. JAXBContext jaxbContext = JAXBContext.newInstance("jaxbdemo.generated"); Unmarshaller unMarshaller = jaxbContext.createUnmarshaller(); JAXBElement catalogElement = (JAXBElement ) unMarshaller.unmarshal(xmlDocument); CatalogType catalog = catalogElement.getValue(); List journalList = catalog.getJournal(); Demo

28 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 28 Marshalling an XML Document  Create a marshaller and an object factory. We'll use the Java classes in package jaxbdemo.generated for this example.  Create a Catalog and Journal objects. Get references to the lists of Journal and Article objects. Add the Journal object to the Journal list. JAXBContext jaxbContext = JAXBContext.newInstance("jaxbdemo.generated"); Marshaller marshaller = jaxbContext.createMarshaller(); ObjectFactory factory = new ObjectFactory(); CatalogType catalog = factory.createCatalogType(); List journalList = catalog.getJournal(); JournalType journal = factory.createJournalType(); List articleList = journal.getArticle(); journalList.add(journal); Demo

29 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 29 Marshalling an XML Document  Create an Article object. Add it to the Article list.  Marshal the Java objects to XML elements. Write the XML document to standard out. ArticleType article = factory.createArticleType(); article.setTitle("Service Oriented Architecture Frameworks"); article.setAuthor("Naveen Balani"); article.setLevel("Intermediate"); article.setDate("January-2004"); articleList.add(article); JAXBElement catalogElement = factory.createCatalog(catalog); marshaller.setProperty("jaxb.formatted.output", Boolean.TRUE); marshaller.marshal(catalogElement, System.out);

30 Department of Computer Science Spring 2013: February 18 CS 157B: Database Management Systems II © R. Mak 30 Project #2  Due: Friday, March 1.  Experience: Writing XML schemas Validating XML documents Marshalling and unmarshalling XML data XQuery Altova XMLSpy tool  Download the Altova XMLSpy 2013 tool from: http://www.altova.com/download-trial.html# http://www.altova.com/download-trial.html# Evaluation good for 30 days. _


Download ppt "CS 157B: Database Management Systems II February 18 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron."

Similar presentations


Ads by Google