You are on page 1of 13

© 2001 First Hop

Java and XML


Janne Kalliola
Director, Product Development
First Hop Ltd

Java and XML

ƒ About XML
ƒ Basic XML manipulation
ƒ DOM
ƒ SAX
ƒ Transforming and outputting XML documents
ƒ XSLT
ƒ XSL-FO
ƒ New generation technologies
ƒ Java XML Bindings
ƒ Java XML Messaging
ƒ JAX-RPC
ƒ Case – configuration information

1
About XML

ƒ Extensible Markup Language (XML) is a W3C


recommendation for representing information in
electronical form
ƒ XML is a meta language; it does not provide any
semantics, only the basic syntax and conformance
tests
ƒ everything else can be decided by the provider of
the XML document
ƒ There are dozens of initiatives to create various
languages over XML
ƒ MathML
ƒ XHTML
ƒ SOAP

Using XML

ƒ In the beginning XML was strictly thought as a textual


document
ƒ the representation of the document structure was
a file containing elements and body text
ƒ Nowadays XML is used programmatically, thus
documents can also be
ƒ data structures
ƒ events
ƒ streams
ƒ In this presentation, the word "document" is used to
cover all of these aspects

2
XML as Objects

ƒ XML documents can be manipulated in object


oriented software using Document Object Model
(DOM) interfaces
ƒ DOM is a W3C (www.w3.org) recommendation
ƒ programming language and implementation
independent
ƒ interfaces available for several languages
including Java, C++, Python, and IDL
ƒ DOM is also used in browsers to manipulate
HTML content
ƒ The basic idea in DOM is to read XML documents into
trees and manipulate the tree

DOM Levels and Implementations

ƒ DOM contains currently three levels:


1. basic manipulation interfaces
2. event based XML document manipulation, tree
travelsal and style sheets (CSS)
3. XML Schemas, Xpath
ƒ There are several implementations available, such as
JAXP from Sun Microsystems, and Xerces from Apache
Foundation
ƒ the implementations are usually called XML
parsers
ƒ contains implementations for the DOM
interfaces and supporting code
ƒ the support of different levels varies from
implementation to another

3
Event-based Manipulation

ƒ If the XML documents are large or only a part of the


document should be read in, DOM can consume too
much memory and resources
ƒ the solution is to read the XML document as a
serie of events
ƒ every XML element triggers an event
ƒ the program reading in the document catches the
events and reacts accordingly
ƒ the document is read element by element and
thus no large data structures are created
ƒ The event-based manipulation is done with SAX
(Simple API for XML)
ƒ the SAX implementations are usually bundled
with DOM implementations

Transforming XML

ƒ If the source XML document is outside the system


using the document, the source document may be in
a format that is not optimal or usable for the system
ƒ The document has to be transformed into another
XML syntax
ƒ the transformation is hard to program with
normal programming languages
ƒ instead, a special programming language
Extensible Style Language Transformations (XSLT)
has been created for solving this problem

4
XSLT

ƒ XSLT is rule-based language


ƒ the XSLT syntax is based on XML, i.e. XSLT programs
(stylesheets) are XML documents
ƒ the XSLT contains rules to transform elements,
attributes and subdocuments to another form
ƒ The XSLT stylesheet is run inside a XSLT processor
ƒ the processor gets the source document and the
stylesheet as input and produces another XML
documents as output
ƒ the processor reads in part of the source document
and tries to find matching rules
ƒ if a rule is found, it is executed and the rule
produces a part of the output document
ƒ there are default rules for ruleless situations
ƒ the process goes on recursively until the source
document has been completely processed

XSL Formatting Objects

ƒ XSL-FO is an XML-based language to describe layout


information
ƒ the XSL-FO document contains instructions to
render itself, like DTP program documents
ƒ the document is rendered using an XSL-FO
processor
ƒ the processor outputs a document in some
format such as PDF or PostScript
ƒ XSL-FO is a young recommendation, finalised on
October 2001
ƒ programmatic support is still in beta level

5
Outputting Human-readable
Documents

ƒ If the XML document has to be presented to a person,


there are two basic ways to proceed:
ƒ convert the document to XHTML and show the
output document with a WWW browser
ƒ transformation is done using XSLT and the
output is readily available for presentation
ƒ convert the document to XSL-FO and format the
document to some desired output format
ƒ transformation is done using XSLT and the
output is rendered to the final output format
by XSL-FO processor

Transformations in Java

ƒ Both XSLT and XSL-FO can be used inside Java


programs
ƒ XSLT interfaces are included in JAXP
ƒ XSLT processors are available for example from
IBM, Sun Microsystems and Apache Foundation
ƒ XSL-FO is currently still in beta phase
ƒ the best Java-based processor is FOP, provided by
Apache Foundation, xml.apache.org/fop
ƒ FOP provides output as PDF, PS, PCL, SVG
ƒ FOP can also be used to render the
documents inside a Java program, using the
supplied AWT component

6
New Generation Technologies

ƒ The first uses of XML in Java were very document-


oriented
ƒ reading in and writing out documents
ƒ using XML in WWW applications for content
presentation, or as a configuration file format
ƒ transforming documents to other forms
ƒ New APIs are geared towards using XML in a
programmatic manner
ƒ the programmers are shielded from the bare XML
documents
ƒ APIs are provided instead and XML formats are
kept on the background

Java XML Bindings

ƒ If XML documents contain only computer readable


information, DOM and SAX are cumbersome
technologies to read the documents in
ƒ semantics has to be provided by the software
itself
ƒ Sun Microsystems has proposed a solution for the
problem
ƒ Java XML Bindings (JAXB) is an API and collection
of tools to automate mappings between XML
documents and Java classes
ƒ JAXB is currently in development, for more
information:
http://java.sun.com/xml/jaxb/

7
JAXB Mechanisms

ƒ JAXB provides a compiler that creates Java classes


from XML DTDs (document type definition)
ƒ XML Schema support will be available shortly
ƒ DTD is converted to a Binding Schema that is
used to create the classes
ƒ Generated classes contain error and validity checks as
stated in DTDs
ƒ Classes can both read in and generate XML
documents

JAXB Benefits & Problems

ƒ Easier programming model


ƒ some parts of program logic are written with a
description language
ƒ Smaller footprint for XML parsing
ƒ no need to keep redundant information in the
memory during parsing
ƒ Program logic is divided into two locations
ƒ there are some versioning problems, when the
DTDs change
ƒ Automatisation may generate poor code

8
Java XML Messaging

ƒ Java XML Messaging (JAXM) is a set of APIs that


enable sending and receiving XML formatted
messages
ƒ implements Simple Object Access Protocol
(SOAP) 1.1 with attachments
ƒ JAXM is used to exchange XML business documents
over the Internet
ƒ Transportation is usually done over HTTP
ƒ FTP and SMTP can be used, too
ƒ messages can be both synchronous and
asynchronous (with or without
acknowledgements)
ƒ one message can be sent to several recipients
ƒ For more information: http://java.sun.com/xml/jaxm/

JAXM Benefits

ƒ Help the developers to concentrate on the core


features of the program
ƒ messaging details are left to JAXM components
ƒ provides several transports to interchange
messages
ƒ Messages can be exchanged with non-Java
applications, too

9
JAX-RPC

ƒ Java API for XML-Based Remote Procedure Calls


enables Java applications to communicate with other
programs using RPC mechanism
ƒ the API is based on SOAP 1.1
ƒ Usually, JAX-RPC is used in a client program to
connect to a remote server
ƒ client initiates a remote procedure call in the
server
ƒ server has defined a set of calls that are available
for the clients (remote API of the server)
ƒ JAX-RPC is thus similar to RMI (remote method
invocation) or CORBA (common object request broker
architecture)

JAX-RPC Functionality

ƒ The remote procedure call is represented using an


XML based protocol (for instance SOAP 1.1)
ƒ The server can define, describe and export a web
service as an RPC based service
ƒ the service is described using Web Service
Description Language (WDSL)
ƒ XML based specification to describe service as
a set of endpoints that operate on messages
ƒ WDSL is a World Wide Web Consortium
(W3C) specification
ƒ For more information:
http://java.sun.com/xml/jaxrpc/

10
Java XML Packs

ƒ Sun Microsystems has collected all the available APIs


to a Java XML Pack
ƒ the pack contains a set of interoperable Java XML
technologies
ƒ Sun Microsystems releases a new version of the
pack quarterly
ƒ http://java.sun.com/xml/javaxmlpack.html
ƒ Packs should not be confused with JAXP (Java API for
XML Processing)
ƒ contains DOM and SAX parsers and XSLT
processor
ƒ JAXP forms the base of Java XML Pack

Case – Description

ƒ First Hop products' configuration information is stored


in an XML file
ƒ the file is read in by the main controller of the
product
ƒ the configurations are grouped by the
components of the product
ƒ eases maintaining of the configuration
ƒ the contents of the file are dissected to several
parts and given to the appropriate components
ƒ every component gets only its own
configuration
ƒ configurations can be embedded, if a
component uses another component
internally

11
Case – Benefits

ƒ The configuration is kept in a single file


ƒ reduces number of files to be manipulated
ƒ The configuration is flexible
ƒ the syntax of the component configuration is
specified by the component
ƒ the configurations can be nested, if required
ƒ The configuration is automatically checked
ƒ if the syntax of the configuration is wrong, DOM
raises errors
ƒ no need to create own configuration parsers

Summary

ƒ There are two levels of XML manipulation


ƒ the first one is based on the document aspects of
XML
ƒ older standards, more implementations
ƒ generic solutions
ƒ the second one is based on programmatic use of
XML
ƒ new standards, usually one or few
implementations
ƒ some are still in a draft phase
ƒ specific solutions
ƒ Java and XML provide a wide range of possibilities for
application programmers

12
Questions & Comments?

ƒ For more information about First Hop:


www.firsthop.com
ƒ For information about XML:
ƒ http://java.sun.com/xml/
ƒ http://www.w3.org/
ƒ http://www.xml.com/
ƒ You can reach me at janne.kalliola@firsthop.com

13

You might also like