31 Signs That Technology Has Taken Over Your Life: #6. When you go - - PowerPoint PPT Presentation

31 signs that technology has taken over your life 6 when
SMART_READER_LITE
LIVE PREVIEW

31 Signs That Technology Has Taken Over Your Life: #6. When you go - - PowerPoint PPT Presentation

31 Signs That Technology Has Taken Over Your Life: #6. When you go into a computer store, you eavesdrop on a salesperson talking with customers -- and you butt in to correct him and spend the next twenty minutes answering the customers'


slide-1
SLIDE 1

31 Signs That Technology Has Taken Over Your Life: #6. When you go into a computer store, you eavesdrop on a salesperson talking with customers -- and you butt in to correct him and spend the next twenty minutes answering the customers' questions, while the salesperson stands by silently, nodding his head.

slide-2
SLIDE 2

Advanced Java Class

XML

slide-3
SLIDE 3

Intro

  • XML = eXtensible Markup Language
  • HTML 4 = XHTML 1.0 (2000)
  • Important improvements over HTML

– Can define your own XML tags – Can define a strict grammar to find mistakes more easily

  • Complex XML is very powerful
  • Simple XML is very easy to start working

with

slide-4
SLIDE 4

XML Syntax

  • <tagname></tagname>
  • <tagname/>
  • <tagname attribute_name=”value”.. />
  • <!-- Comments go here -->
  • Special Characters - like html:

– & = &amp; – ‘ = &apos; – > = &gt; – < = &lt; – “ = &quot;

slide-5
SLIDE 5

XML Namespaces

  • Defined as a attribute (usually of the root

tag)

  • xmlns=http://www.defaulttags.com/tags

[default]

  • xmlns:myTags=”…” [custom tag library]

– <myTags:tag_name …/>

slide-6
SLIDE 6

XML Structure

  • Nested elements must end before their enclosing

elements

  • Elements can have attributes and/or children

elements

  • Whether to put info in attributes or child tags is a

style choice

slide-7
SLIDE 7

HTML, XHTML and XML

  • HTML is not necessarily XML

– unclosed tags – overlapping tags – case-insensitivity

  • HTML that is XML is XHTML
slide-8
SLIDE 8

An XML Example Document

<location id="on.ottawa"> <geographic-info> <name>Ottawa</name> <province>ON</province> <type>municipality</type> </geographic-info> <geodetic-info> <longitude-deg>-75.7</longitude-deg> <latitude-deg>45.3</latitude-deg> <elevation-ft>300</elevation-ft> </geodetic-info> </location>

slide-9
SLIDE 9

Parsing & Creating XML with Java SAX vs. DOM

  • SAX:

– you write classes that correspond to element – you write a ContentHandler that puts the xml data into instantiations of your new classes – benefit: less memory intensive than DOM

  • DOM:

– classes exist to hold the element data – classes exist to transfer the data into instantiations of those classes. – Benefit: easier to access specific data you want, and don’t have to create extra classes.

slide-10
SLIDE 10

Parsing & Creating XML with Java JDOM vs. included packages

  • Packages that comes with J2SE 1.4

– javax.xml.parsers – javax.xml.transform (and subpackages) – org.w3c.dom – org.xml.sax (and subpackages)

  • Packages in JDOM

– Take advantage of Java features, like collections frameworks – therefore easier to work with. – org.jdom (and subpackages)

slide-11
SLIDE 11

Discussed in Detail Here:

DOM: no SAX: no DOM: yes SAX: no Generating XML DOM: no SAX: yes DOM: yes SAX: no Parsing XML Included packages JDOM

slide-12
SLIDE 12

Parsing XML with org.w3c.dom SAX (Simple API for XML)

  • Write a class definition for each type of

element in the document

  • Implement org.xml.sax.ContentHandler by

extending org.xml.sax.DefaultHandler

  • Parse
slide-13
SLIDE 13

Write a class definition for each type

  • f element in the document
  • Write set methods for each type of

element that can be nested inside the type of element that this class is for

  • Write a method to set the attributes of this

element in it’s corresponding class definition

slide-14
SLIDE 14

Implement

  • rg.xml.sax.ContentHandler
  • Extend org.xml.sax.Defaulthandler
  • It will call the startElement method whenever it

encounters a new Element in the XML file.

  • Have it instantiate an Object of the type that

corresponds to the type of element that it’s creating – use reflection

  • Have it call the correct methods to set get child

elements as instance variables in the new Object – use reflection

  • Have it call the correct method to set the

attributes found in this class.

slide-15
SLIDE 15

Parsing

  • XMLReader parser =

XMLReaderFactory.createXMLReader();

  • ContentHandler handler = new [fill in the

class you created]();

  • parser.setContentHandler (handler);
  • parser.parse( new InputSource ( “[fill in

XML file name]” );

slide-16
SLIDE 16

Parsing XML with JDOM

  • Don’t have to write your own model classes
  • Don’t have to implement a content handler
  • Parsing:
  • SAXBuilder builder = new SAXBuilder();
  • Document document = builder.build(“…”)
  • Takes File, InputStream, URL, etc.
  • Then just access the data by calling methods on

the Document and it’s Elements

slide-17
SLIDE 17

JDOM Structure

  • Document

– has a rootElement – has a list of all Content

  • Some Content subclasses:

– Comment – DocType – Element – Text

  • Elements have:

– Name – List of Attributes – List of children Elements

slide-18
SLIDE 18

Generating XML with JDOM

  • Element rootElement = new Element

(“name”);

  • Document d = new Document

(rootElement);

  • rootElement.addContent(childElement);
  • rootElement.setAttribute(“name”, “value”);
  • etc.
slide-19
SLIDE 19

Validating XML

  • DTD (Document Type Definition)
  • Easier to user
  • Your XML Document in assignment 1, part b

must satisfy the given dtd

  • XML schema
slide-20
SLIDE 20

DTD (Document Type Definition)

  • <!ELEMENT element_name

(content_specification) >

  • <!ATTLIST element_name

attribute_name attribute_type value_option [attribute_name attribute_type value_option]* >

slide-21
SLIDE 21

<!ELEMENT element_name content_specification >

  • Content_specification:

– EMPTY – ANY – PCDATA (contains markup tags to be processed) – CDATA (don’t process tags – treat as plain text) – children_specification

  • Children_specification:

– Default: exactly one of the specified type – A | B means type A OR type B – + means one or more element of this type – * means zero or more elements of this type – ? means zero or one element of this type

slide-22
SLIDE 22

<!ATTLIST element_name attribute_name attribute_type value_option>

  • Common Value_types:

– CDATA – unparsed character data – (a|b|c|d|e…) – has to be one of these

  • Value_options:

– #DEFAULT value – #IMPLIED -- optional – #REQUIRED – #FIXED value

slide-23
SLIDE 23

XML Schema

  • More complex and powerful than DTD
  • See example on page 698 – fig 10-18
  • We’re using a DTD for this class - simpler
slide-24
SLIDE 24

Transforming XML with XSLT

  • Can transform the XML into something

more viewable, like HTML

  • See example on page 700 – fig 10-19
  • Use javax.xml.transform package to do

the transformation

slide-25
SLIDE 25

XSLT, continued

  • Each template matches an element type, and

then contains html to output whenever the element is matched

  • Inside the html, info from the XML Element may

be accessed:

– <xsl:value-of select=”Name” /> – <xsl:value-of select=”@attribute_name” /> – <xsl:value-of select=”child_element_name” />

  • Can even include things like for-each and if.
  • Templates may be referenced from other

templates, allowing for nesting

slide-26
SLIDE 26

Group XML Task

  • http://lsirwww.epfl.ch/courses/cis/2004ss/ex
  • Write a DTD for this XML file.
slide-27
SLIDE 27

To check DTD and XML validation:

  • <!DOCTYPE bank [

<!ELEMENT ... /> <!ATTLIST .../> etc. ]>

  • Place the DOCTYPE in the XML, just before the

<?xml version="1.0" encoding="UTF-8"?> line.

  • http://www.stg.brown.edu/service/xmlvalid/
slide-28
SLIDE 28

<!DOCTYPE bank [ <!ELEMENT bank (accounts,customers,customer_accounts) > <!ELEMENT accounts (savings_accounts, checking_accounts) > <!ELEMENT savings_accounts (savings_account)* > <!ELEMENT savings_account (balance) > <!ELEMENT balance (#PCDATA) > <!ELEMENT checking_accounts (checking_account)*> <!ELEMENT checking_account (balance) > <!ATTLIST savings_account id CDATA #REQUIRED interest CDATA #REQUIRED> <!ATTLIST checking_account id CDATA #REQUIRED> <!ELEMENT customers (customer)* > <!ELEMENT customer (name, address) > <!ELEMENT name (#PCDATA) > <!ELEMENT address (#PCDATA) > <!ATTLIST customer id CDATA #REQUIRED> <!ELEMENT customer_accounts (customer_account)*> <!ELEMENT customer_account EMPTY > <!ATTLIST customer_account c_id CDATA #REQUIRED ac_id CDATA #REQUIRED > ]>