Title: Java web services and XML
1Java web services and XML
2Why use XML in Java?
- XML and Java work very well together.
- Portability Java is a platform independent
development language. XML is an architecture and
language independent data format. - Extensive development support in Java for XML.
3Application areas of XML
- Presentation Oriented Publishing (POP).
- Message Oriented Middleware (MOM) B2B.
- Exchanging database contents.
- Example of an online shopping system
- The owner of the online shop wants to provide an
online service so that it also works on mobile
phones. - The owner wants to automate order processing.
- The marketing department would like to extract
the data from the online shop so they can
organise product promotions and sales.
4Setting up the environment for XML
and Java.
- To use XML you need a XML parser.
- However you must first have a Java JDK
http//java.sun.com. - You can download XML parser from
http//xml.apache.org - Most errors happens because of a wrong CLASSPATH.
- You should be familiar with Java including I/O,
classes, objects, polymorphism, etc.
5JAVA XML API
- The main Java XML APIs are
- JAXP Parsing API.
- Apache have produced the JAXP reference
implementation. - JAXM Messaging API.
- Get an XML document from one place to another.
- JAXB Binding API.
- It takes an XML DTD as inputs and produces a Java
classes that map to the DTD. - Used when data validation is required.
6JAVA XML API Cont
- Java API for XML Processing.
- As a developer you would develop special
interfaces. - Different APIs use different approaches to access
the information in the XML document. - SAX and DOM are languages independent interfaces.
7Simple API for XML - SAX
- SAX is a standard interface for event based XML
parsing. - SAX defines a number of events, the documents are
accessed serially and an event is triggered at
different parts of the documents. - Some common events includes
- Start of the document.
- Start elements.
- Characters.
- End elements.
- End of the document.
- SAX is fast and has a low memory requirement, but
is hard to setup. - Versions of used SAX
- SAX1
- SAX2
8Document Object Model
- Designed to be a portable interface for
manipulating document structures. - DOM application builds a tree structure of the
XML document in memory. - The different parts of the XML file are stored in
nodes in the DOM document. - Version of used DOM
- DOM Level 0
- DOM Level 1
- DOM Level 2
9SAX or DOM
- SAX looks for startElement events, then looks at
each character event. - SAX is good when
- You only need to go through the document once.
- You are looking for a few items.
- There is shortage in memory.
10SAX or DOM Cont
- DOM builds Java objects for everything in the
document, then walks through the tree looking for
the name therefore we create a lot of Java
objects we never use. - DOM is good when
- You need to go through a document more than once.
- You want to manipulate lots of things in the
document. - Memory is not an issue.
11Writing XML Documents with Java
- XML documents are text, any Writer can produce an
XML document. - Java's InputStreamReader and OutputStreamWriter
classes are very helpful. - URL u new
- URL("http//www.test.com/week3.xml")
- InputStream in u.openStream()
- InputStreamReader reader new InputStreamReader(i
n, "UTF-8") - int c
- while ((c in.read) ! -1) System.out.write(c)
12A Java program that writes Fibonacci numbers into
a text file
import java.math. import java.io. public
class FibonacciText public static void
main(String args) try
FileOutputStream fout new FileOutputStream("fibo
nacci.txt") OutputStreamWriter out
new OutputStreamWriter(fout, "8859_1")
BigInteger low BigInteger.ZERO
BigInteger high BigInteger.ONE int
i 0 do out.write(low.toString()
"\r\n") BigInteger temp high high
high.add(low) low temp i
while (i lt 100) out.write(high.toStr
ing() "\r\n") out.close()
catch (IOException e) System.err.println(e)
13fibonacci.txt
0 1 1 2 3 5 8 13 21 34 55 89 144 233
377 610 987 1597 2584 4181 6765 ..
14A Java program that writes Fibonacci numbers into
an XML file
import java.math. import java.io. public
class FibonacciXML public static void
main(String args) try
FileOutputStream fout new FileOutputStream("fibo
nacci.xml") OutputStreamWriter out new
OutputStreamWriter(fout)
BigInteger low BigInteger.ZERO
BigInteger high BigInteger.ONE
out.write("lt?xml version\"1.0\"?gt\r\n")
out.write("ltFibonacci_Numbersgt\r\n")
for (int i 0 i lt 101 i)
out.write(" ltfibonacci index\"" i "\"gt")
out.write(low.toString())
out.write("lt/fibonaccigt\r\n") BigInteger
temp high high high.add(low)
low temp out.write("lt/Fibonacci_Numbe
rsgt") out.close() catch
(IOException e) System.err.println(e)
15fibonacci.xml
lt?xml version"1.0"?gt ltFibonacci_Numbersgt
ltfibonacci index"0"gt0lt/fibonaccigt ltfibonacci
index"1"gt1lt/fibonaccigt ltfibonacci
index"2"gt1lt/fibonaccigt ltfibonacci
index"3"gt2lt/fibonaccigt ltfibonacci
index"4"gt3lt/fibonaccigt ltfibonacci
index"5"gt5lt/fibonaccigt ltfibonacci
index"6"gt8lt/fibonaccigt ltfibonacci
index"7"gt13lt/fibonaccigt ltfibonacci
index"8"gt21lt/fibonaccigt ltfibonacci
index"9"gt34lt/fibonaccigt ltfibonacci
index"10"gt55lt/fibonaccigt ltfibonacci
index"11"gt89lt/fibonaccigt . lt/Fibonacci_Nu
mbersgt
16The SAX1 Process
- A ParserFactory makes a parser.
- Your code registers a DocumentHandler with the
parser. - An InputSource feeds the document into the
parser. - As the document is read, the parser calls back to
the methods of the methods of the DocumentHandler
to tell it what it's seeing in the document. - Making a Parser
- org.xml.sax.helpers.ParserFactory.makeParser()
returns an instance of the class named in the
org.xml.sax.parser system property. - org.xml.sax.helpers.ParserFactory.makeParser(Strin
g className) requires you to know the name of an
installed parser.
17Event Based API
- You do not always have all the information you
need at the time of a given callback. - You may need to store information in various data
structures (stacks, queues,vectors, arrays, etc.)
and act on it at a later point. - For example the characters() method is not
guaranteed to give you the maximum number of
contiguous characters. It may split a single run
of characters over multiple method calls.
18InputSource
- Encapsulates access to data so that it looks the
same whether it's coming from a - URL
- file
- stream
- reader
- database
- something else
- Used in SAX1 and SAX2.
- Allows the source to be changed.
19Example of InputSource
import org.xml.sax import java.io. import
java.util.zip. ... try URL u new
URL("http//metalab.com/xml/examples/validstats.xm
l.gz") InputStream raw u.openStream()
InputStream decompressed new GZIPInputStream(in)
InputSource in new InputSource(decompressed)
// read the document... catch
(IOException e) System.err.println(e) catch
(SAXException e) System.err.println(e)
20What SAX1 doesn't do
- Completely ignores document type declaration.
- Validation and other optional results of DTD
(attribute defaulting, external entities, etc.)
are at parser default. - Comments.
- XML Declaration.
- Does not report CDATA sections, entity
references, and other non-canonical information
from the document. - No explicit support for namespaces.
21SAX2
- Adds
- Namespace support.
- Optional Validation.
- Optional Lexical events for comments, CDATA
sections, entity references. - A lot more configurable.
- Adapter classes convert between parsers.
22Errors
- Three Levels of Errors, in increasing order of
severity - A warning e.g. ambiguous content model, a
constraint for compatibility. - A recoverable error typically a validity error.
- A fatal error typically a well-formedness error.
- The ErrorHandler interface
- package org.xml.sax
- public interface ErrorHandler
- public abstract void warning(SAXParseException
exception) throws SAXException - public abstract void error(SAXParseException
exception) throws SAXException - public abstract void fatalError(SAXParseExcepti
on exception) throws SAXException -
23DOM Trees
- Entire document is represented as a grove of
trees. - Each XML document should contain exactly one
tree. - A tree contains nodes.
- Some nodes may contain other nodes (depending on
node type). - Each document node contains
- zero or one doctype nodes.
- one root element node.
- zero or more comment and processing instruction
nodes.
24XML-RPC
- Remote Procedure Calls like CORBA or RMI.
- Transport Protocol is HTTP.
- Data format is XML.
- Language independent.
- Platform independent.
- Uses
- Syndication
- Web Page Editors
- Applets that store state on server
- and more...
25Sample XML-RPC Response with HTTP Header
HTTP/1.1 200 OK Connection close Content-Length
150 Content-Type text/xml Date Tue, 29 Feb 2000
110742 GMT Server AddressFinder 2.3 lt?xml
version"1.0"?gt ltmethodResponsegt ltparamsgt
ltparamgt ltvaluegtltstringgt1332 East Fork Road,
Lexington KY 11238lt/stringgtlt/valuegt lt/paramgt
lt/paramsgt lt/methodResponsegt