Download presentation
Presentation is loading. Please wait.
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 Special Characters - like html: –& = & –‘ = ' –> = > –< = < –“ = "
5
XML Namespaces Defined as a attribute (usually of the root tag) xmlns=http://www.defaulttags.com/tags [default]http://www.defaulttags.com/tags xmlns:myTags=”…” [custom tag library] –
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 Ottawa ON municipality -75.7 45.3 300
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: JDOMIncluded packages Parsing XMLDOM: yes SAX: no DOM: no SAX: yes Generating XML DOM: yes SAX: no DOM: 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) <!ATTLIST element_name attribute_name attribute_type value_option [attribute_name attribute_type value_option]* >
21
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
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: – 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/e xercise1/bank.xmlhttp://lsirwww.epfl.ch/courses/cis/2004ss/e xercise1/bank.xml Write a DTD for this XML file.
27
To check DTD and XML validation: <!DOCTYPE bank [ etc. ]> Place the DOCTYPE in the XML, just before the line. http://www.stg.brown.edu/service/xmlvalid/
28
<!DOCTYPE bank [ <!ATTLIST savings_account id CDATA #REQUIRED interest CDATA #REQUIRED> <!ATTLIST customer_account c_id CDATA #REQUIRED ac_id CDATA #REQUIRED > ]>
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.