1 / 24

Advanced Java Session 9

Advanced Java Session 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

hali
Download Presentation

Advanced Java Session 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 XML data DB Server server1 db server 2

  4. What is XML Meta 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 &amp • ? -- 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 HTML enumeration “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 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

  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 Example XMLReader parser = (XMLReader) Class.forName(parserName).newInstance(); parser.setContentHandler(counter); parser.setErrorHandler(counter); parser.parse(uri);

  18. 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>

  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 XML document Transformation XML document XSL stylesheet

  22. XSL Transformer • TransformerFactory • Transformer • StreamSource • StreamResult

  23. 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")));

  24. Thank you

More Related