Presentation is loading. Please wait.

Presentation is loading. Please wait.

SAX: Simple API for XML 1.0 Showing structure of XML with a java program The java program Tree.java runs the SAX parser on an XML file to display “tree”

Similar presentations


Presentation on theme: "SAX: Simple API for XML 1.0 Showing structure of XML with a java program The java program Tree.java runs the SAX parser on an XML file to display “tree”"— Presentation transcript:

1 SAX: Simple API for XML 1.0 Showing structure of XML with a java program The java program Tree.java runs the SAX parser on an XML file to display “tree” structure and can also be used to show parsing errors. Program appears in notes and in Dietel xml text, chapter 9.

2 An xml file: spacing1.xml World

3 SAX: Simple API for XML A java program to show tree structure of an XML document Tree.java. Run on command line: java Tree yes/no f.xml

4 Run Tree on notvalid.xml <!DOCTYPE test [ ]>

5 Run Tree on notvalid.xml C:\PROGRA~1\JAVA\JDK15~1.0_0\BIN>java Tree no notvalid.xml URL: file:C:/PROGRA~1/Java/JDK15~1.0_0/bin/notvalid.xml [ document root ] +-[ element : test ] +-[ ignorable ] +-[ proc-inst : test ] "message" +-[ ignorable ] +-[ element : example ] +-[ element : item ] +-[ text ] "Hello & Welcome!" +-[ ignorable ] [ document end ] C:\PROGRA~1\JAVA\JDK15~1.0_0\BIN>

6 Running again but showing SAX parse errors C:\PROGRA~1\JAVA\JDK15~1.0_0\BIN>java Tree yes notvalid.xml URL: file:C:/PROGRA~1/Java/JDK15~1.0_0/bin/notvalid.xml [ document root ] +-[ element : test ] +-[ ignorable ] +-[ proc-inst : test ] "message" +-[ ignorable ] +-[ element : example ] Parse Error: Element type "item" must be declared. C:\PROGRA~1\JAVA\JDK15~1.0_0\BIN>

7 The pastry xml file grape sour real sour chocolate

8 Running a SAX parser on this (java code in notes) C:\PROGRA~1\JAVA\JDK15~1.0_0\BIN>java MySAXApp pastry.xml Start document Start element: donuts Start element: jelly Characters: "grape" End element: jelly Start element: lemon Characters: "sour" End element: lemon Start element: lemon Characters: "real sour" End element: lemon Start element: glazed Characters: "chocolate" End element: glazed End element: donuts End document C:\PROGRA~1\JAVA\JDK15~1.0_0\BIN>

9 A day planner using the SAX parser for XML and java

10 SAX: Simple API for XML 2.0 Sax 2.0 recently released Xerces parser available at http://xml.apache.org/#xerces You may need to search the apache site, I found the latest version, zip file at http://apache.cs.utah.edu/xml/xerces-j/ This SAX v2.0 parser is needed to run PrintXML.java example, chapter 9 Dietel.

11 Sun App Server –an aside This server may be needed for the WSDP (webservicesdevelopmentpack) which contains a tutorial, xerxes and xalan parsers, and classes for SOAP C:\Sun\AppServer\docs\about.html

12 Xerces parser Dietel’s XML How to program comes with a release of the Xerces parser. You can unzip these files – you’ll need them for SOAP in any case. You can also find the Xerces parser on the Apache site.

13 Xerces Xerces-J is packaged as a ZIP file for all platforms and operating systems. You can run the Java jar command to unpack the distribution. jar xf Xerces-J-bin.1.2.0.zip jar xf Xerces-J-src.1.2.0.zip This command creates a "xerces-1_2_0" sub-directory in the current directory containing all the files. Files in the binary package release LICENSE License for Xerces-J Readme.html Web page redirect to docs/html/index.html xerces.jar Jar file containing all the parser class files xercesSamples.jar Jar file containing all sample class files data/ Directory containing sample XML data files docs/html/ Directory containing documentation docs/apiDocs/ Directory containing Javadoc API for parser framework To use Xerces-J you do not need the source files.

14 Xerces I just ran the.exe files in the release which seemed to unpack things ok.

15 Xerces Running Xerces. Xerces is a java program which comes as a jar file. You’ll need the path set to your java/bin directory and the classpath set to wherever Xerces.jar is. I created batch files to run SAXCount and DOMCount java programs displaying the parser at work on xml files.

16 The next few examples These examples come with the distribution and are at C:\Xerces\xerces- 1_2_0\docs\html\domwriter.html

17 The xml files: personal.xml Boss Big chief@foo.com Worker One one@foo.com Worker Two two@foo.com Worker Three three@foo.com Worker Four four@foo.com Worker Five five@foo.com

18 Personal.dtd

19 Batchfiles:DOMCount.bat set PATH=%PATH%;C:\Progra~1\Java\jdk15.0_0\bin set CLASSPATH=%CLASSPATH%;c:\xerces- 1_2_0\xerces.jar;c:\xerces-1_2_0\xercesSamples.jar cd c:\xerces-1_2_0 java dom.DOMCount data/personal.xml C:\xerces-1_2_0>java dom.DOMCount data/personal.xml data/personal.xml: 170 ms (37 elems, 18 attrs, 26 spaces, 242 chars)

20 Batchfiles:SAXTest.bat set PATH=%PATH%;C:\Progra~1\Java\jdk15.0_0\bin set CLASSPATH=%CLASSPATH%;c:\xerces- 1_2_0\xerces.jar;c:\xerces-1_2_0\xercesSamples.jar cd c:\xerces-1_2_0 java sax.SAXCount data/personal.xml

21 Saxcounter C:\XERCES~1>SAXTest.bat C:\XERCES~1>set PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\ PROGRA~1\COMMON~1\ADAPTE~1\System;C:\jakarta\JAKART~1.28\bin;c:\progra~1\j ava\jd k15~1.0_0\bin;c:\jakarta\jakart~1.28\common\lib;;C:\Progra~1\Java\jdk15.0_0\bin C:\XERCES~1>set CLASSPATH=c:\progra~1\java\jdk15~1.0_0\lib\tools.jar;c:\progra~1 \java\jdk15~1.0_0\bin;c:\progra~1\java\jdk15~1.0_0\bin\hello;c:\jakarta\jakart~1.28\common\lib;;c:\xerces-1_2_0\xerces.jar;c:\xerces-1_2_0\xercesSamples.jar C:\XERCES~1>cd c:\xerces-1_2_0 C:\xerces-1_2_0>java sax.SAXCount data/personal.xml data/personal.xml: 181 ms (37 elems, 18 attrs, 26 spaces, 242 chars)

22 SAXWriter.bat: writing out the xml content set PATH=%PATH%;C:\Progra~1\Java\jdk15.0_0\bin set CLASSPATH=%CLASSPATH%;c:\xerces- 1_2_0\xerces.jar;c:\xerces-1_2_0\xercesSamples.jar cd c:\xerces-1_2_0 java sax.SAXWriter data/personal.xml The next slide is the output…I cut some blank lines to get it to fit.

23 DOMWriter C:\xerces-1_2_0>java dom.DOMWriter data/personal.xml data/personal.xml: Boss Big chief@foo.com Worker One one@foo.com Worker Two two@foo.com Worker Three three@foo.com Worker Four four@foo.com Worker Five five@foo.com

24 C:\xerces-1_2_0>java sax.SAXWriter data/personal.xml data/personal.xml: Boss Big chief@foo.com <link subordinates="one.worker two.worker three.worker four.worker five.work er"> Worker One one@foo.com Worker Two two@foo.com Worker Three three@foo.com Worker Four four@foo.com Worker Five five@foo.com

25 Iterator view some classes missing org.w3c.dom. set PATH=%PATH%;C:\Progra~1\Java\jdk15.0_0\bin set CLASSPATH=%CLASSPATH%;c:\xerces- 1_2_0\xerces.jar;c:\xerces-1_2_0\xercesSamples.jar cd c:\xerces-1_2_0 java dom.traversal.IteratorView data/personal.xml --not running

26 Xalan: XSL Xalan can be downloaded as with Xerces. It also comes on the Dietel XML CD. Xalan contains software to process XSL. The distribution also contains Xerces. There are documents to help you get started at: C:\Xalan\Xalan Getting Started.htm You’ll have to set your path and class path as we did for Xerces. I’ve provided an example but your version will depend on where you unpack the zip files.

27 SimpleTransform: output C:\Xalan\xalan-j_1_2_D02\samples\SimpleTransform>java SimpleTransform Hello C:\XALAN\XALAN-~1\SAMPLES>

28 SimpleTransform xml and xsl files The xml: Hello The xsl:

29 Batch file to run SimpleTransform set PATH=%PATH%;C:\Progra~1\Java\jdk15.0_0\bin set CLASSPATH=%CLASSPATH%;c:\xerces- 1_2_0\xerces.jar;c:\xalan\xalan-j_1_2_D02\xalan.jar;c:\xalan\xalan- j_1_2_D02\samples\xalansamples.jar cd c:\xalan\xalan-j_1_2_D02\samples\SimpleTransform java SimpleTransform

30 ApplyXPath: looking for a particular item in the xml set PATH=%PATH%;C:\Progra~1\Java\jdk15.0_0\bin set CLASSPATH=%CLASSPATH%;c:\xerces- 1_2_0\xerces.jar;c:\xalan\xalan- j_1_2_D02\xalan.jar;c:\xalan\xalan- j_1_2_D02\samples\xalansamples.jar cd c:\xalan\xalan-j_1_2_D02\samples\ApplyXPath java ApplyXPath foo.xml /doc/name/@first

31 ApplyXPath: output C:\Xalan\XALAN-~1\samples>cd c:\xalan\xalan- j_1_2_D02\samples\ApplyXPath C:\Xalan\xalan-j_1_2_D02\samples\ApplyXPath>java ApplyXPath foo.xml /doc/name/@first DavidDavidDonaldEmilyJackMyriamPaulRobertScottShane

32 PureSAX The PureSAX class uses SAX DocumentHandlers and the Xerces SAX parser to produce a stylesheet tree, an XML input tree, and the transformation result tree.

33 Batch file for PureSAX set PATH=%PATH%;C:\Progra~1\Java\jdk15.0_0\bin set CLASSPATH=%CLASSPATH%;c:\xerces- 1_2_0\xerces.jar;c:\xalan\xalan-j_1_2_D02\xalan.jar;c:\xalan\xalan- j_1_2_D02\samples\xalansamples.jar cd c:\xalan\xalan-j_1_2_D02\samples\PureSAX java PureSAX

34 An Applet to transform XML into HTML The applet uses a stylesheet to transform an XML document into HTML. It displays the XML document, the stylesheet, and the HTML output. How to Use the Xalan-Java applet wrapper 1.Include XSLTProcessorApplet class in an HTML client.XSLTProcessorApplet class 2.Specify the XML source document and XSL stylesheet. You can use the DocumentURL and StyleURL PARAM tags or the XSLTProcessorApplet setDocumentURL() method and XSLTProcessorApplet setStyleURL() method. If the XML document contains a stylesheet Processing Instruction (PI), you do not need to specify an XSL stylesheet. XSLTProcessorApplet setDocumentURL() method XSLTProcessorApplet setStyleURL() method 3.Call the XSLTProcessorApplet transformToHTML() method which performs the transformation and returns the new document as a String.XSLTProcessorApplet transformToHTML() method

35 The applet: remarks on classes and running it This applet transforms XML into HTML. Given the restrictions imposed by the applet sandbox, the local copy of this applet does not load and run correctly in some environments and with some versions of IE/Netscape. Run the applet from an HTTP server, and these problems disappear. To run the applet from one of our Domino servers, click here.click here The local copy of client.html assumes that xalan.jar and xerces.jar are in the Xalan root directory, two directories above the samples/applet subdirectory. If these JAR files are located elsewhere, you must edit the applet archive attribute in client.html to point to xalan.jar and xerces.jar. To run the applet locally, click here.click here

36 The applet: before hitting button

37 Running applet (in IE window)

38 A Servlet example: delivering XML as HTML The client (which you must set up) specifies an XML document and a stylesheet. The servlet performs the transformation and returns the output to the client. You can use media.properties to specify which stylesheet is to be used depending on the client browser/device. How to run it: Configure your application server (Websphere or JServ, for example) so it can find the classes (in xalansamples.jar) as well as the stylesheets and properties file in the servlet subdirectory. Set up an HTML client to call DefaultApplyXSL with arguments as illustrated below. Examples: http://localhost/servlet/DefaultApplyXSL?URL=/data.xml&xslURL= /style.xsl –...applies the style.xsl stylesheet to the data.xml data. Both files are served from the Web server's HTTP XSLTInputSource root. http://localhost/servlet/DefaultApplyXSL?URL=/data.xml&xslURL= /style.xsl&debug=true –...ensures that XML and XSL processor messages are returned in the event of problems applying style.xsl to data.xml http://localhost/servlet/DefaultApplyXSL/data.xml?xslURL=/style.xsl –...applies the style.xsl stylesheet to the data.xml data, just like the first example. This is an alternative way of specifying the XML XSLTInputSource by utilizing the HTTP request's path information.

39 More servlet examples More examples http://localhost/servlet/DefaultApplyXSL/data.xml –...examines data.xml for an associated XSL stylesheet. If multiple XSLs are associated with the data, the stylesheet whose media attribute maps to your browser type will be chosen. If no mapping is successful, the primary associated stylesheet is used. http://localhost/servlet/data.xml –...provides the same function as the previous example, but this example assumes that /servlet/data.xml has been mapped to be executed by this servlet. The servlet engine may be configured to map all or some *.xml files to this servlet through the use of servlet aliases or filters. http://localhost/servlet/data.xml?catalog=http://www.xml.org/dtds/oag.xml –...supplements any servlet-configured XCatalog with a catalog of supply chain DTDs residing at the XML.ORG DTD repository. For more information, see the comments in DefaultApplyXSL.java.


Download ppt "SAX: Simple API for XML 1.0 Showing structure of XML with a java program The java program Tree.java runs the SAX parser on an XML file to display “tree”"

Similar presentations


Ads by Google