240 likes | 368 Vues
This advanced session from New York University's School of Continuing and Professional Studies explores the integration of Java with XML, encompassing XML fundamentals, parsing techniques, and web publishing frameworks. Participants will learn about the role of XML as a meta-language and its importance in defining other languages. The course covers parsing methodologies—both DOM and SAX—and introduces key XML-related acronyms such as XSL, XPath, and XML Schema. Attendees will also gain hands-on experience with XML document transformations using XSLT.
E N D
Advanced JavaSession 9 New York University School of Continuing and Professional Studies
Objectives • Java and XML • Introduction to XML • Parsing XML • Web publishing frameworks using XML/XSLT
Java and XML • Java – Portable Code • XML – Portable Data • XML for Presentation • XML for Communication/Integration XML data DB Server server1 db server 2
What is XML Meta language – that is used to define other languages Markup language that specifies neither the tag set, nor the grammar
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”>
XML <color>blue</color> </cushion> </chair> <chair wood=”oak”> <quantity>3</quantity> <quality>average</quality> </chair> </dining-room>
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
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
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.
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
Attributes • <!ATTLIST [Enclosing Element] [Attr name] [type] [modifiers] Attribute Types – CDATA “name=“ in HTML enumeration “align=“ in HTML #IMPLIED #REQUIRED #FIXED
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.
Document Object Model • A set of interfaces and classes • Building the document tree DOMParser 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
SAX • Event based API for parsing XML documents • Set of interfaces and classes – • ContentHandler • ErrorHandler
SAX ContentHandler • startDocument() • endDocument() • processingInstruction() • startPrefixMapping() • endPrefixMapping() • startElement() • endElement() • characters() • ignorableWhitespace()
SAX ErrorHandler • warning() • error() • fatalError()
SAX Parsing Example XMLReader parser = (XMLReader) Class.forName(parserName).newInstance(); parser.setContentHandler(counter); parser.setErrorHandler(counter); parser.parse(uri);
XSL template XSL 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>
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>
XSL elements <xsl:element name=font> <xsl:attribute name=size> 5 </xsl:attribute> hello </xsl:element> <xsl:copy-of select=“*” />
XSL Transformation XML document Transformation XML document XSL stylesheet
XSL Transformer • TransformerFactory • Transformer • StreamSource • StreamResult
Simple Transformation TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer ( new StreamSource("birds.xsl")); transformer.transform( new StreamSource("birds.xml"), new StreamResult( new FileOutputStream("birds.out")));