jaxp java api for xml processing
play

JAXP ( Java API for XML Processing ) Dr. Kanda Runapongsa Dr. - PDF document

Java Web Services, Software Park Thailand, 2004 JAXP ( Java API for XML Processing ) Dr. Kanda Runapongsa Dr. Kanda Runapongsa Department of Computer Engineering Department of Computer Engineering Khon Kaen University Khon Kaen University


  1. Java Web Services, Software Park Thailand, 2004 JAXP ( Java API for XML Processing ) Dr. Kanda Runapongsa Dr. Kanda Runapongsa Department of Computer Engineering Department of Computer Engineering Khon Kaen University Khon Kaen University 1 Overview Overview � What are XML Parsers? What are XML Parsers? � � What is JAXP ? What is JAXP ? � � SAX : Simple API for XML SAX : Simple API for XML � � DOM : Document Object Model DOM : Document Object Model � � SAX vs. DOM SAX vs. DOM � � When to Use DOM ? When to Use DOM ? � � When to Use SAX ? When to Use SAX ? � � Transforming with XSLT Transforming with XSLT � 2 Dr. Kanda Runapongsa, Khon Kaen University 1

  2. Java Web Services, Software Park Thailand, 2004 What are XML Parsers ? ? What are XML Parsers � In order to process XML data, every program or In order to process XML data, every program or � server process needs an XML parser server process needs an XML parser � The parser extracts the actual data out of the The parser extracts the actual data out of the � textual representation textual representation � It is essential for the automatic processing of It is essential for the automatic processing of � XML documents XML documents 3 What are XML Parsers? (Cont.) What are XML Parsers? (Cont.) � Parsers also check whether documents conform Parsers also check whether documents conform � to the XML standard and have a correct to the XML standard and have a correct structure structure � There are two types of XML parsers There are two types of XML parsers � � Validating: check documents against a DTD or an Validating: check documents against a DTD or an � XML Schema XML Schema � Non Non- -validating: do not check documents against a validating: do not check documents against a � DTD or an XML Schema. DTD or an XML Schema. 4 Dr. Kanda Runapongsa, Khon Kaen University 2

  3. � � � � � � � � � � � � � � Java Web Services, Software Park Thailand, 2004 What is JAXP ? What is JAXP ? JAXP : The Java API for XML Processing The Java API for XML Processing JAXP : JAXP = SAX SAX + + DOM DOM + + XSTL XSTL (Java API) (Java API) JAXP = Now (24/ 09/ 2004) Now (24/ 09/ 2004) , JAXP , JAXP v.1.2.6 v.1.2.6 JAXP allows you t JAXP allows you to o u use any XML se any XML � � compliant parser from within compliant parser from within your application your application A thin and lightweight Java API for parsing and transforming A thin and lightweight Java API for parsing and transforming XML documents XML documents Allows for pluggable parsers and transformers Allows for pluggable parsers and transformers Allow passing of XML document using : Allow passing of XML document using : >> Event Driven (SAX 2.0) >> Event Driven (SAX 2.0) >> Three Bases (DOM level 2) >> Three Bases (DOM level 2) 5 JAXP � Pluggable Framework for Parsers and Transformers User Application JAXP Interface Reference Parsers Other Parser 6 Dr. Kanda Runapongsa, Khon Kaen University 3

  4. Java Web Services, Software Park Thailand, 2004 Packages in JAXP Packages in JAXP � The JAXP API model is quite easy to The JAXP API model is quite easy to � understand and simple to use understand and simple to use � javax.xml.parsers javax.xml.parsers � � Provide a common interface for different Provide a common interface for different � vendor vendor’ ’s SAX and DOM parsers s SAX and DOM parsers � org.w3c.dom org.w3c.dom � � Define the Document class (DOM) as well as Define the Document class (DOM) as well as � classes for all of the components of a DOM classes for all of the components of a DOM 7 Packages in JAXP Packages in JAXP � org.xml.sax org.xml.sax � � Define the basic SAX APIs Define the basic SAX APIs � � javax.xml.transform javax.xml.transform � � Define the XSLT APIs that let you transform Define the XSLT APIs that let you transform � XML into other forms XML into other forms 8 Dr. Kanda Runapongsa, Khon Kaen University 4

  5. Java Web Services, Software Park Thailand, 2004 Current Parsing Approaches Current Parsing Approaches � SAX (Simple API for XML) and DOM SAX (Simple API for XML) and DOM � (Document Object Model) allow programmers (Document Object Model) allow programmers to access their information stored in XML to access their information stored in XML documents documents � Using any programming language and a parser for Using any programming language and a parser for � that language that language � Both of them take very different approaches to Both of them take very different approaches to � giving you access to your information giving you access to your information 9 Overview Overview � XML Parsers XML Parsers � � What is JAXP ? What is JAXP ? � � SAX : Simple API for XML SAX : Simple API for XML � � DOM : Document Object Model DOM : Document Object Model � � SAX vs. DOM SAX vs. DOM � � When to Use DOM ? When to Use DOM ? � � When to Use SAX ? When to Use SAX ? � � Transforming with XSLT Transforming with XSLT � 10 Dr. Kanda Runapongsa, Khon Kaen University 5

  6. Java Web Services, Software Park Thailand, 2004 SAX SAX ( Simple API for XML ) ( Simple API for XML ) 11 Overview Overview � What is SAX ? What is SAX ? � � SAX Operational Model SAX Operational Model � � Processing XML with JAXP SAX Processing XML with JAXP SAX � � Callback Interfaces Callback Interfaces � � Handling SAX events Handling SAX events � � startDocument startDocument, , endDocument endDocument, characters , characters � � startElement startElement, , endElement endElement � � What the What the ContentHandler ContentHandler Doesn Doesn’ ’t Tell You t Tell You � 12 Dr. Kanda Runapongsa, Khon Kaen University 6

  7. Java Web Services, Software Park Thailand, 2004 SAX SAX ( Simple API for XML ) ( Simple API for XML ) � SAX API is based on an event SAX API is based on an event- -driven processing driven processing � model where model where � The data elements are interpreted on a sequential The data elements are interpreted on a sequential � basis basis � The callbacks are called based on selected The callbacks are called based on selected � constructs constructs � It uses a sequential read It uses a sequential read- -only approach and does only approach and does � not support random access to the XML not support random access to the XML elements elements 13 SAX Operational Model SAX Operational Model Events Provided XML Input Parser Document Handler 14 Dr. Kanda Runapongsa, Khon Kaen University 7

  8. Java Web Services, Software Park Thailand, 2004 Processing XML with JAXP SAX SAX Processing XML with JAXP � Major steps for parsing using JAXP: Major steps for parsing using JAXP: � � Getting Factory and Parser classes to perform XML Getting Factory and Parser classes to perform XML � parsing parsing � Setting options such as namespaces, validation, and Setting options such as namespaces, validation, and � features features � Creating a Creating a defaultHandler defaultHandler implementation class implementation class � 15 Getting a Factory Class Getting a Factory Class � Obtain a factory class using the Obtain a factory class using the � SAXParserFactory’ SAXParserFactory ’s s static static newInstance newInstance() () method method � SAXParserFactory SAXParserFactory factory = factory = � SAXParserFactory.newInstance SAXParserFactory.newInstance(); (); 16 Dr. Kanda Runapongsa, Khon Kaen University 8

  9. Java Web Services, Software Park Thailand, 2004 Getting and Using a Getting and Using a SAXParser SAXParser Class Class � Obtain the SAX parser class from the factory by Obtain the SAX parser class from the factory by � calling the newSAXParser calling the newSAXParser() static method () static method � SAXParser SAXParser parser = parser = factory.newSAXParser factory.newSAXParser(); (); � � Parse the XML data by calling the parse method Parse the XML data by calling the parse method � � parser.parse( parser.parse(“ “methodCall.xml methodCall.xml” ”, handler); , handler); � � The second argument is the handler with type The second argument is the handler with type � ContentHandler ContentHandler 17 Callback Interfaces Callback Interfaces � SAX uses the Observer design pattern to tell SAX uses the Observer design pattern to tell � client applications what’ client applications what ’s in a document s in a document � Java developers are most familiar with this Java developers are most familiar with this � pattern from the event architecture of the AWT pattern from the event architecture of the AWT and Swing and Swing � MouseListener MouseListener as the observer as the observer � � Button as the Subject Button as the Subject � 18 Dr. Kanda Runapongsa, Khon Kaen University 9

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend