Upload
ophelia-mitchell
View
214
Download
0
Tags:
Embed Size (px)
Citation preview
Advanced JavaSession 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
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
4
What is XMLMeta language – that is used to define other
languages
Markup language that specifies neither the tag set, nor the grammar
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”>
6
XML<color>blue</color>
</cushion>
</chair>
<chair wood=”oak”>
<quantity>3</quantity>
<quality>average</quality>
</chair>
</dining-room>
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
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
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.
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 &• ? -- oncr or not at all• + -- 1 to n times• * -- 0 to n times
11
Attributes• <!ATTLIST [Enclosing Element] [Attr name] [type] [modifiers]Attribute Types –
CDATA “name=“ in HTMLenumeration “align=“ in HTML
#IMPLIED#REQUIRED#FIXED
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.
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
14
SAX• Event based API for parsing XML
documents
• Set of interfaces and classes –– ContentHandler– ErrorHandler
15
SAX ContentHandler• startDocument()• endDocument()• processingInstruction()• startPrefixMapping()• endPrefixMapping()• startElement()• endElement()• characters()• ignorableWhitespace()
16
SAX ErrorHandler• warning()
• error()
• fatalError()
17
SAX Parsing ExampleXMLReader parser = (XMLReader)
Class.forName(parserName).newInstance();
parser.setContentHandler(counter);
parser.setErrorHandler(counter);
parser.parse(uri);
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>
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>
20
XSL elements<xsl:element name=font>
<xsl:attribute name=size>
5
</xsl:attribute>
hello
</xsl:element>
<xsl:copy-of select=“*” />
21
XSL Transformation
XMLdocument
XSLstylesheet
TransformationXML
document
22
XSL Transformer• TransformerFactory
• Transformer
• StreamSource
• StreamResult
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")));
24
Thank you