31 signs that technology has taken over your life 6 when
play

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'


  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.

  2. Advanced Java Class XML

  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

  4. XML Syntax • <tagname></tagname> • <tagname/> • <tagname attribute_name=”value”.. />  <!-- Comments go here --> • Special Characters - like html: – & = &amp; – ‘ = &apos; – > = &gt; – < = &lt; – “ = &quot;

  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 …/>

  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

  7. HTML, XHTML and XML • HTML is not necessarily XML – unclosed tags – overlapping tags – case-insensitivity • HTML that is XML is XHTML

  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>

  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.

  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)

  11. Discussed in Detail Here: JDOM Included packages Parsing XML DOM: yes DOM: no SAX: no SAX: yes Generating XML DOM: yes DOM: no SAX: no SAX: no

  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

  13. Write a class definition for each type of 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

  14. Implement org.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.

  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]” );

  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

  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

  18. Generating XML with JDOM • Element rootElement = new Element (“name”); • Document d = new Document (rootElement); • rootElement.addContent(childElement); • rootElement.setAttribute(“name”, “value”); • etc.

  19. Validating XML o DTD (Document Type Definition) o Easier to user o Your XML Document in assignment 1, part b must satisfy the given dtd o XML schema

  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]* >

  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

  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

  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

  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

  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

  26. Group XML Task • http://lsirwww.epfl.ch/courses/cis/2004ss/ex • Write a DTD for this XML file.

  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/

  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 > ]>

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend