1 Java and XML Modified from presentation by: Barry Burd Drew University [email protected] Portions...

38
1 Java and XML Modified from presentation by: Barry Burd Drew University [email protected] Portions © 2002 Hungry Minds, Inc.

Transcript of 1 Java and XML Modified from presentation by: Barry Burd Drew University [email protected] Portions...

Page 1: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

1

Java and XML

Modified from presentation by:Barry Burd

Drew [email protected]

Portions © 2002 Hungry Minds, Inc.

Page 2: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

2

Outline

• Review of XML

• Discussion of some Java XML APIs including– SAX– DOM

Page 3: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

3

Review of XML

<?xml version="1.0" encoding="UTF-8"?><Greeting type="friendly"> Hello</Greeting>

Start tagEnd tag

Processing instructionCharacters

(text)

Page 4: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

4

Review of XML

<?xml version="1.0" encod<Greeting type="friendly"> Hello</Greeting>

Element

Attribute name

Attribute value

Page 5: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

5

Review of XML

<song tempo="&Moderato;"> <measure> <beat> <note pitch="&G;"/> <note pitch="&C;"/> <note pitch="&C;"/> </beat> ...

Empty Elements

Entity references

Page 6: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

6

Document Type Definition (DTD)

<?xml version="1.0" encoding="UTF-8"?><!-- Song.dtd -->

<!ELEMENT song (measure*)> <!ATTLIST song tempo CDATA "&Moderato;"><!ELEMENT measure (beat*)><!ELEMENT beat (note*)><!ATTLIST note pitch CDATA #REQUIRED><!ENTITY C "60">...Etc.

Page 7: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

7

A Doc that’s not well-formed

<Greeting> Hello world! <Question> How are you? <Question> <!-- Oh, no! This start tag should be an end tag! --> </Greeting>

Comment

Page 8: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

8

An Invalid Document<!DOCTYPE Greeting SYSTEM "InvalidDoc.dtd"><Greeting> Hello world! <Question> <!-- No Question element in the DTD --> How are you? </Question> </Greeting>

<?xml version="1.0" encoding="UTF-8"?> <!-- InvalidDoc.dtd --><!ELEMENT Greeting (#PCDATA)>

Page 9: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

9

A valid document

<!DOCTYPE Greeting SYSTEM "Greeting.dtd"><Greeting> Hello world! <Question> How are you? </Question></Greeting>

<!-- Greeting.dtd --> <!ELEMENT Greeting (#PCDATA | Question)*><!ELEMENT Question (#PCDATA)>

Page 10: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

10

Outline

• Review of XML

• Discussion of some Java XML APIs– What APIs are available– SAX– DOM– Validation, namespaces, etc.– Creating a new XML document using DOM– Using XSL

Page 11: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

11

Some of the Java APIs

• JAXP (Java API for XML Processing)– SAX (Simple API for XML)– DOM (Document Object Model)

– JAXP comes standard with J2SE 1.4

– It is installed on cerebro if you use the path /usr/local/linux-jdk1.3.1/bin

Page 12: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

12

SAX

• Event driven

• Deals with start tags, end tags, etc.

• No inherent notion of an element

• No inherent notion of nesting

• No in-memory copy of the whole document

Page 13: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

13

import javax.xml.parsers.*;import org.xml.sax.*;import java.io.*;class CallSAX{ static public void main(String[] args) throws SAXException, ParserConfigurationException, IOException { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); XMLReader xmlReader = saxParser.getXMLReader(); xmlReader.setContentHandler (new MyContentHandler());

//...(more)

Calling SAX

Page 14: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

14

import javax.xml.parsers.*;import org.xml.sax.*;import java.io.*;class CallSAX{ static public void main(String[] args) throws SAXException, ParserConfigurationException, IOException { //...Stuff from previous slide

xmlReader.parse (new File(“weather.xml"). toURL().toString()); }}

Calling SAX

Page 15: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

15

A Content Handler

import org.xml.sax.helpers.DefaultHandler;import org.xml.sax.Attributes;

class MyContentHandler extends DefaultHandler{ public void startDocument() { System.out.println ("Starting the document."); }

// More...

Page 16: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

16

A Content Handler (cont’d) public void startElement(String uri, String localName, String qualName, Attributes attribs) { System.out.print("Start tag: "); System.out.println(qualName); for (int i=0; i<attribs.getLength(); i++) { System.out.print("Attribute: "); System.out.print(attribs.getQName(i)); System.out.print(" = "); System.out.println(attribs.getValue(i)); } }

Page 17: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

17

A Content Handler (cont’d) public void characters (char[] charArray, int start, int length) { String charString = new String(charArray, start, length);

charString = charString.replaceAll("\n", "[cr]"); charString = charString.replaceAll(" ", "[blank]");

System.out.print(length + " characters: "); System.out.println(charString); }

Page 18: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

18

A Content Handler (cont’d) public void endElement(String uri, String localName, String qualName) { System.out.print("End tag: "); System.out.println(qualName); }

public void endDocument() { System.out.println("Ending the document."); }}

Page 19: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

19

A Sample Document

<?xml version="1.0"?><WeatherReport><City>White Plains</City><State>NY</State><Date>Sat Jul 25 1998</Date><Time>11 am EDT</Time><CurrTemp Unit="Farenheit">70</CurrTemp>

<High Unit="Farenheit">82</High><Low Unit="Farenheit">62</Low></WeatherReport>

Page 20: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

20

The Output

Starting the document.Start tag: WeatherReport1 characters: $Start tag: City12 characters: White PlainsEnd tag: City1 characters: $Start tag: State2 characters: NYEnd tag: State1 characters: $Start tag: Date15 characters: Sat Jul 25 1998End tag: Date1 characters: $Start tag: Time9 characters: 11 am EDTEnd tag: Time1 characters: $Start tag: CurrTempAttribute: unit = Farenheit2 characters: 70End tag: CurrTemp1 characters: $Start tag: HighAttribute: unit = Farenheit2 characters: 82End tag: High1 characters: $Start tag: LowAttribute: unit = Farenheit2 characters: 62End tag: Low1 characters: $End tag: WeatherReportEnding the document.

Page 21: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

21

DOM

• Not event driven• Creates an in-memory copy of the

document• Deals with document nodes• Nodes are nested• Elements are nodes, but so are attributes,

text, processing instructions, comments, the entire document itself

• No inherent notion of a tag

Page 22: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

22

Calling DOM

import javax.xml.parsers.*;import org.xml.sax.SAXException;import java.io.*;import org.w3c.dom.Document;

public class CallDOM{ public static void main(String args[]) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc;

// More... }}

Page 23: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

23

Calling DOM

import javax.xml.parsers.*;import org.xml.sax.SAXException;import java.io.*;import org.w3c.dom.Document;public class CallDOM{ public static void main(String args[]) throws ParserConfigurationException, SAXException, IOException { //... Stuff from previous slide

if (args.length == 1) { doc = builder.parse (new File(args[0]).toURL().toString()); new MyTreeTraverser (doc); } else System.out.println ("Usage: java CallDOM file-name.xml"); }}

Page 24: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

24

class MyTreeTraverser{ String indent="";

MyTreeTraverser (Node node) { traverse(node); } void traverse(Node node){ displayName(node); displayValue(node); if (node.getNodeType() == Node.ELEMENT_NODE) displayAttributes(node);

indent=indent.concat(" "); displayChildren(node); indent=indent.substring(0,indent.length()-3); } // …stuff void displayChildren(Node node) { Node child = node.getFirstChild(); while (child != null) { traverse(child); child = child.getNextSibling(); } }

A Tree Traverser

Page 25: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

25

The Output

Name: #documentValue: null Name: WeatherReport Value: null Name: #text Value: Name: City Value: null Name: #text Value: White Plains Name: #text Value: Name: State Value: null Name: #text Value: NY Name: #text Value: Name: Date Value: null Name: #text Value: Sat Jul 25 1998 Name: #text Value:

Name: Time Value: null Name: #text Value: 11 am EDT Name: #text Value: Name: CurrTemp Value: null

Attribute: Unit = Farenheit Name: #text Value: 70 Name: #text Value: Name: High Value: null

Attribute: Unit = Farenheit Name: #text Value: 82 Name: #text Value: Name: Low Value: null

Attribute: Unit = Farenheit Name: #text Value: 62 Name: #text Value:

Page 26: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

26

Setting an Error Handler

• In SAX: xmlReader.setErrorHandler new MyErrorHandler());

• In DOMbuilder.setErrorHandler (new MyErrorHandler());

Page 27: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

27

An Error Handler

import org.xml.sax.helpers.DefaultHandler;import org.xml.sax.SAXParseException;class MyErrorHandler extends DefaultHandler{ public void error(SAXParseException e) { System.out.println("Error:"); showSpecifics(e); System.out.println(); } public void showSpecifics(SAXParseException e) { System.out.println(e.getMessage()); System.out.println (" Line " + e.getLineNumber()); System.out.println (" Column " + e.getColumnNumber()); System.out.println (" Document " + e.getSystemId()); }}

Page 28: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

28

import org.xml.sax.helpers.DefaultHandler;import org.xml.sax.SAXParseException;class MyErrorHandler extends DefaultHandler{ public void warning(SAXParseException e) { System.out.println("Warning:"); showSpecifics(e); System.out.println(); } public void fatalError(SAXParseException e) { System.out.println("Fatal error:"); showSpecifics(e); System.out.println(); }

// ...}

An Error Handler (cont’d)

Page 29: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

29

Turning on DTD Validation

// must do this before using the factory

factory.setValidating(true);

Page 30: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

30

The Output

Error:Attribute value for "Unit" is #REQUIRED. Line 9 Column -1 Document file:/usr/local/www/data/csci380/02s/examples/xml/DOMvalid/weather2.xml

Name: #documentValue: null Name: WeatherReport Value: null Name: WeatherReport Value: null…

Page 31: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

31

<?xml-stylesheet type="text/xsl" href="JavaAndXMLforDummies.xsl"?><Book title="Java and XML For Dummies"> <Chapter title="Introduction"> <Section>About This Book</Section> <Section>Conventions Used in This Book</Section> </Chapter> <Part number="I" title="The Big Picture"> <Chapter number="1" title="SAX"> <Section>A "Hello" Example</Section> <Section>Doing Validation</Section> </Chapter> <Chapter number="2" title="DOM"> <Section>A "Hello" Example</Section> <Section>Creating a new document</Section> </Chapter> </Part> <Part title="Appendixes"> <Appendix number="A" title="Things to Remember about Java"> </Appendix> <Appendix number="B" title="Things to Remember about XML"> </Appendix> </Part></Book>

Using XSL

Page 32: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

32

<?xml version="1.0"?><xsl:stylesheet xmlns:xsl= http://www.w3.org/1999/XSL/Transform version="1.0"> <xsl:output method="html"/>

<xsl:template match="Book"> <h2><xsl:value-of select="@title"/></h2> <xsl:apply-templates /> </xsl:template> <xsl:template match="Part"> <h2> <xsl:if test="@number">Part <xsl:value-of select="@number"/>: </xsl:if><xsl:value-of select="@title"/> </h2> <xsl:apply-templates /> </xsl:template>

An XSL Stylesheet

Page 33: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

33

<xsl:template match="Chapter"> <b>&#160;&#160;&#160;&#160;<xsl:if test="@number">Chapter <xsl:value-of select="@number"/>: </xsl:if><xsl:value-of select="@title"/></b><br/> <xsl:apply-templates /> </xsl:template> <xsl:template match="Section"> #160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#8226;

<xsl:value-of select="."/><br/> </xsl:template> <xsl:template match="Appendix"> <b>&#160;&#160;&#160;&#160;Appendix <xsl:value-of select="@number"/>: <xsl:value-of select="@title"/></b><br/> <xsl:apply-templates /> </xsl:template>

</xsl:stylesheet>

An XSL Stylesheet

(cont’d)

Page 34: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

34

JavaAndXMLforDummies.htm<h2>Java and XML For Dummies</h2> <b>&nbsp;&nbsp;&nbsp;&nbsp;Introduction</b><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&bull; About This Book<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&bull; Conventions Used in This Book<br>

<h2>Part I: The Big Picture</h2> <b>&nbsp;&nbsp;&nbsp;&nbsp;Chapter 1: SAX</b>

... Etc.

Page 35: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

35

Java and XML For Dummies     Introduction

        • About This Book        • Conventions Used in This Book

Part I: The Big Picture     Chapter 1: SAX

        • A "Hello" Example        • Doing Validation    Chapter 2: DOM         • A "Hello" Example        • Creating a new document

Appendixes     Appendix A: Things to Remember about Java

    Appendix B: Things to Remember about XML

Page 36: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

36

Applying XSL with Java Codeimport javax.xml.transform.TransformerFactory;import javax.xml.transform.Transformer;import javax.xml.transform.stream.StreamSource;import javax.xml.transform.stream.StreamResult;import javax.xml.transform.TransformerException;import javax.xml.transform.TransformerConfigurationException;import java.io.FileOutputStream;import java.io.FileNotFoundException;import java.io.IOException;

More...

Page 37: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

37

Applying XSL with Java Code (cont’d)

public class MyTransform{ public static void main(String[] args) throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException {

TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer (new StreamSource("JavaAndXMLforDummies.xsl")); transformer.transform (new StreamSource("JavaAndXMLforDummies.xml"), new StreamResult (new FileOutputStream("JavaAndXMLforDummies.htm"))); }}

Page 38: 1 Java and XML Modified from presentation by: Barry Burd Drew University me@BarryBurd.com Portions © 2002 Hungry Minds, Inc.

38

The Usual Scenario...

Client Browser

http GET requestfor

JavaAndXMLforDummies.jsp

HTTP server

Servlet container

.xml file.xml file .xsl file.xsl file

.jsp file.jsp file

.htm file.htm file

response