Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Java and XML
Modified from presentation by:
Barry Burd
Drew University
me@BarryBurd.com
Portions © 2002 Hungry Minds, Inc.
1
Outline
• Review of XML
• Discussion of some Java XML APIs
including
– SAX
– DOM
2
Review of XML
<?xml version="1.0" encoding="UTF-8"?>
<Greeting type="friendly">
Hello
</Greeting>
Characters
(text)
End tag
Processing
instruction
Start tag
3
Review of XML
<?xml version="1.0" encod
<Greeting type="friendly">
Hello
</Greeting>
Element
Attribute
value
Attribute
name
4
Review of XML
<song tempo="&Moderato;">
<measure>
<beat>
<note pitch="&G;"/>
<note pitch="&C;"/>
<note pitch="&C;"/>
</beat>
...
Entity
references
Empty
Elements
5
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.
6
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
7
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)>
8
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)>
9
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
10
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
11
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
12
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());
Calling
SAX
//...(more)
13
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
Calling
SAX
xmlReader.parse (new
File(“weather.xml").
toURL().toString());
}
}
14
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...
15
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));
}
}
16
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);
}
17
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.");
}
}
18
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>
19
Starting the document.
Start tag: WeatherReport
1 characters: $
Start tag: City
12 characters: White Plains
End tag: City
1 characters: $
Start tag: State
2 characters: NY
End tag: State
1 characters: $
Start tag: Date
15 characters: Sat Jul 25 1998
End tag: Date
1 characters: $
Start tag: Time
9 characters: 11 am EDT
End tag: Time
1 characters: $
Start tag: CurrTemp
Attribute: unit = Farenheit
2 characters: 70
End tag: CurrTemp
1 characters: $
Start tag: High
Attribute: unit = Farenheit
2 characters: 82
End tag: High
1 characters: $
Start tag: Low
Attribute: unit = Farenheit
2 characters: 62
End tag: Low
1 characters: $
End tag: WeatherReport
Ending the document.
The
Output
20
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
21
import
import
import
import
javax.xml.parsers.*;
org.xml.sax.SAXException;
java.io.*;
org.w3c.dom.Document;
Calling
DOM
public class CallDOM
{
public static void main(String args[])
throws ParserConfigurationException,
SAXException,
IOException
{
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder =
factory.newDocumentBuilder();
Document doc;
// More...
}
}
22
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
Calling
DOM
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");
}
}
23
class MyTreeTraverser
{
String indent="";
MyTreeTraverser (Node node)
{
traverse(node);
}
A
Tree Traverser
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();
}
}
24
Name: #document
Value: 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
The
Output
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:
25
Setting an Error Handler
• In SAX:
xmlReader.setErrorHandler new MyErrorHandler());
• In DOM
builder.setErrorHandler (new MyErrorHandler());
26
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();
}
An Error
Handler
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());
}
}
27
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)
28
Turning on DTD Validation
// must do this before using the factory
factory.setValidating(true);
29
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: #document
Value: null
Name: WeatherReport
Value: null
Name: WeatherReport
Value: null
…
30
<?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>
31
Using
XSL
<?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>
An XSL
Stylesheet
<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>
32
<xsl:template match="Chapter">
<b>    <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;       •
<xsl:value-of select="."/><br/>
</xsl:template>
<xsl:template match="Appendix">
<b>    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)
33
JavaAndXMLforDummies.htm
<h2>Java and XML For Dummies</h2>
<b> Introduction</b>
<br>
• About This Book<br>
• Conventions Used in This
Book<br>
<h2>Part I: The Big Picture</h2>
<b> Chapter 1:
SAX</b>
... Etc.
34
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
35
Applying XSL with Java Code
import 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...
36
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")));
}
}
37
The Usual Scenario...
http GET request
for
JavaAndXMLforDummies.jsp
HTTP server
Client
Browser
response
.htm file
Servlet container
.jsp file
.xml file
.xsl file
38