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.
Advanced Java Class XML
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
XML Syntax • <tagname></tagname> • <tagname/> • <tagname attribute_name=”value”.. /> <!-- Comments go here --> • Special Characters - like html: – & = & – ‘ = ' – > = > – < = < – “ = "
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 …/>
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
HTML, XHTML and XML • HTML is not necessarily XML – unclosed tags – overlapping tags – case-insensitivity • HTML that is XML is XHTML
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>
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.
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)
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
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
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
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.
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]” );
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
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
Generating XML with JDOM • Element rootElement = new Element (“name”); • Document d = new Document (rootElement); • rootElement.addContent(childElement); • rootElement.setAttribute(“name”, “value”); • etc.
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
DTD (Document Type Definition) • <!ELEMENT element_name (content_specification) > • <!ATTLIST element_name attribute_name attribute_type value_option [attribute_name attribute_type value_option]* >
<!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
<!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
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
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
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
Group XML Task • http://lsirwww.epfl.ch/courses/cis/2004ss/ex • Write a DTD for this XML file.
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/
<!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 > ]>
Recommend
More recommend