24
Advanced Java Session 9 New York University School of Continuing and Professional Studies

Advanced Java Session 9 New York University School of Continuing and Professional Studies

Embed Size (px)

Citation preview

Page 1: Advanced Java Session 9 New York University School of Continuing and Professional Studies

Advanced JavaSession 9

New York University

School of Continuing and Professional Studies

Page 2: Advanced Java Session 9 New York University School of Continuing and Professional Studies

2

Objectives

• Java and XML– Introduction to XML– Parsing XML– Web publishing frameworks using XML/XSLT

Page 3: Advanced Java Session 9 New York University School of Continuing and Professional Studies

3

Java and XML• Java – Portable Code

• XML – Portable Data

• XML for Presentation

• XML for Communication/Integration

db

DB Server server1

server 2

XML data

Page 4: Advanced Java Session 9 New York University School of Continuing and Professional Studies

4

What is XMLMeta language – that is used to define other

languages

Markup language that specifies neither the tag set, nor the grammar

Page 5: Advanced Java Session 9 New York University School of Continuing and Professional Studies

5

XML<?xml version=”1.0”?> <dining-room><table type=”round” wood=”maple”>

<manufacturer>The Wood Shop</manufaturer>

<price>$1999.99</price></table>

 <char wood=”maple”>

<quantity>2</quantity><quality>excellent</quality><cushion included=”true”>

Page 6: Advanced Java Session 9 New York University School of Continuing and Professional Studies

6

XML<color>blue</color>

</cushion>

</chair>

 

<chair wood=”oak”>

<quantity>3</quantity>

<quality>average</quality>

</chair>

</dining-room>

Page 7: Advanced Java Session 9 New York University School of Continuing and Professional Studies

7

Related Acronyms• PI – Processing Instruction

– Provide information to the application – parsers just pass them on to the application

– <?xml: --- is for the parser

• DTD – Document Type Definition– Establishes a set of constraints– Defines valid elements – and valid attributes

• Namespaces – scope– Provides a way to distinguish tags by the same

name

Page 8: Advanced Java Session 9 New York University School of Continuing and Professional Studies

8

Related Acronyms• XSL and XSLT – Extensible Stylesheet

Language, Transformation– Provides a mechanism to convert an XML

document to other formats such as HTML, PDF, WML etc.

• XPath – XML Path Language– A specification to locate a specific item within

an XML document – used heavily in XSL

Page 9: Advanced Java Session 9 New York University School of Continuing and Professional Studies

9

Related Acronyms• XML Schema

– Upcoming replacement for DTD

• XQL– Query language with functionality of SQL

• XSP– Similar to Java Server pages except that the

xsps are “xml” documents with embedded java code.

Page 10: Advanced Java Session 9 New York University School of Continuing and Professional Studies

10

Constraining XML• Document Type Definition – DTD• Specify elements• Specify element hierarchy (nesting)• Specify textual data - #PCDATA • Keyword ANY says anything• Keyword EMPTY says “empty”• Entity references with <!ENTITY &amp• ? -- oncr or not at all• + -- 1 to n times• * -- 0 to n times

Page 11: Advanced Java Session 9 New York University School of Continuing and Professional Studies

11

Attributes• <!ATTLIST [Enclosing Element] [Attr name] [type] [modifiers]Attribute Types –

CDATA “name=“ in HTMLenumeration “align=“ in HTML

#IMPLIED#REQUIRED#FIXED

Page 12: Advanced Java Session 9 New York University School of Continuing and Professional Studies

12

Parsing XML• Two models for parsing XML

– DOM – Document Object Model – creates a tree view of the XML document. Entire document is loaded.

– SAX – Simple API for XML – much more popular.

Page 13: Advanced Java Session 9 New York University School of Continuing and Professional Studies

13

Document Object Model• A set of interfaces and classes

– Building the document treeDOMParser parser = new DOMParser();

parser.parse(uri);

Document doc = parser.getDocument();

– Traverse the tree recursively based on node type – • DOCUMENT_NODE• ELEMENT_NODE• TEXT_NODE, • CDATA_SECTION_NODE• PROCESSING_INSTRUCTION_NODE• ENTITY_REFERENCE_NODE• DOCUMENT_TYPE_NODE

Page 14: Advanced Java Session 9 New York University School of Continuing and Professional Studies

14

SAX• Event based API for parsing XML

documents

• Set of interfaces and classes –– ContentHandler– ErrorHandler

Page 15: Advanced Java Session 9 New York University School of Continuing and Professional Studies

15

SAX ContentHandler• startDocument()• endDocument()• processingInstruction()• startPrefixMapping()• endPrefixMapping()• startElement()• endElement()• characters()• ignorableWhitespace()

Page 16: Advanced Java Session 9 New York University School of Continuing and Professional Studies

16

SAX ErrorHandler• warning()

• error()

• fatalError()

Page 17: Advanced Java Session 9 New York University School of Continuing and Professional Studies

17

SAX Parsing ExampleXMLReader parser = (XMLReader)

Class.forName(parserName).newInstance();

parser.setContentHandler(counter);

parser.setErrorHandler(counter);

parser.parse(uri);

Page 18: Advanced Java Session 9 New York University School of Continuing and Professional Studies

18

XSL templateXSL is XML

Locate a particular element or set of elements within the input XML document and apply a rule or set of rules

<xsl:template match=“Book”>

<!---- rules for formatting the book --</xsl:template>

Page 19: Advanced Java Session 9 New York University School of Continuing and Professional Studies

19

XSL control structures<xsl:apply-templates [select=“e”]/>

<xsl:value-of select=“element”/>

<xsl:for-each select=“element”>

</xsl:for-each>

<xsl:if test=“@attr=‘value’”>

</xsl:if>

<xsl:choose>

<xsl:when test=“@attr=‘value’”>

<xsl:otherwise>

Page 20: Advanced Java Session 9 New York University School of Continuing and Professional Studies

20

XSL elements<xsl:element name=font>

<xsl:attribute name=size>

5

</xsl:attribute>

hello

</xsl:element>

<xsl:copy-of select=“*” />

Page 21: Advanced Java Session 9 New York University School of Continuing and Professional Studies

21

XSL Transformation

XMLdocument

XSLstylesheet

TransformationXML

document

Page 22: Advanced Java Session 9 New York University School of Continuing and Professional Studies

22

XSL Transformer• TransformerFactory

• Transformer

• StreamSource

• StreamResult

Page 23: Advanced Java Session 9 New York University School of Continuing and Professional Studies

23

Simple TransformationTransformerFactory tFactory = TransformerFactory.newInstance();

Transformer transformer = tFactory.newTransformer ( new StreamSource("birds.xsl"));

transformer.transform( new StreamSource("birds.xml"), new StreamResult( new FileOutputStream("birds.out")));

Page 24: Advanced Java Session 9 New York University School of Continuing and Professional Studies

24

Thank you