Practical:
4
PROF. KIRIT J. MODI
XML Programming
1.
To create, open, modify, validate and transform XML
documents via DTD, XML Schema (XSD), XSLT.
2.
To use XML parser to parse and generate XML documents.
GANPAT UNIVERSITY
(Part I): XML, DTD, XSD and XSLT
In this lab, we will learn how to create, open, modify, validate and transform XML documents via DTD, XML Schema (XSD), XSLT using the professional
edition of XMLSpy. Note that examples used in this lab can be downloaded
from our Subject website and so is XMLSpy.
1. Testing and validating using XMLSpy
Download the XMLSpy setup file from our course website and install
XMLSpy. After the installation, start the XMLSpy and click Request a FREE
Evaluation Key to get the key.
1.1 The workspace of XMLSpy
1.2 Steps for validating an XML document
1.2.1 Validating an XML document with an associated DTD file
Download lab1-source.zip from our course website. Unzip the file and open
note.xml and note.dtd (keep them under the same folder). Then in the
workspace of XMLSpy, open the note.xml and note.dtd. Check their contents.
Note the second line of note.xml associates its to the file note.dtd.
Next, we validate this .xml file as follows: Go to the menu, XMLÆValidate.
You could see the output appearing at the bottom of the workspace. It tells us that note.xml is valid according to the W3C XML validation standard.
Please study the .dtd file and explain why the .xml file is valid.
1.2.2 Validating an XML document with an associated XML Schema file
Similarly open note_xsd.xml and note_xsd.xsd and keep them in the same
folder. Check their contents and validate them. You should get the output as follows.
Please study the .xsd file and explain why the .xml file is valid.
1.3 Exercise
1.3.1 Modifying and validating an XML file according to its corresponding DTD file
Open book.xml and book.dtd from the course website. Due to some errors
within book.xml, it can’t be validated. Modify book.xml according to book.dtd
and try to validate book.xml.
Open cdlist.xml and cdlist.dtd from the course website. Due to some errors
within cdlist.xml, it can’t be validated. Modify cdlist.dtd (not the xml file this
time!!) so that cdlist.xml can be validated.
1.3.2 Modifying and validating an XML file according to its corresponding XSD file
Open shiporder.xml and shiporder.xsd from the course website. Due to some
errors within shiporder.xml, it can’t be validated. Modify shiporder.xml this time
according to shiporder.xsd and try to validate shiporder.xml.
2. Transforming XML docs. via XSLT using XMLSpy
2.1 An example
After the cdlist.xml is validated, open the XSLT file called cdlist.xsl and save it
to the directory, in which cdlist.xml lies. Open cdlist.xml and cdlist.xsl in
XMLSpy and check the content of cdlist.xsl. Note that the second line of
Select the window of cdlist.xml and click XSL/XQUERYÆXSL Transformation.
You should see the transformation output, the html file, looks like this:
2.2 Exercise
Study why cdlist.xsl can result in the output html page and try to modify the .xsl file to make different displaying effects.
3. Coding and validating using XMLSpy
3.1 Create a DTD file and an XML file to describe a flower shop and validate
them using XMLSpy.
3.2 Create an XSD file and an XML file to describe a flower shop and validate
them using XMLSpy.
3.3 Try to create an XSLT file based on the result of 3.1 to extract and display
information contained in the XML file.
4. Useful online resource
W3Schools Online Web Tutorials: http://www.w3schools.com/default.asp
(Part II): Parsing XML Documents
Objective:
In this lab, you will learn 1) how to use XML parser to parse and generate XML documents and 2) how to create Web Services that exchange messages taking the form of XML documents.
1. Introduction to XML parser
XML parser is a component of the JDK 1.5 distribution. So you don’t need to download the parser separately, as what you need to do when using the previous versions of Java JDK.
1.1 Parsing XML documents
Analogous to writing the "hello world" program for beginners in
programming, one of the most fundament tasks to learn for beginners in XML programming would be to learn how to parse an XML file, create a corresponding Java objects and manipulate them. In this section, the main idea is to write a program which can read and parse an XML document with employee information, create a list of Employee objects and print out selected contents with format to the console.
Input (employees.xml):
<Age>28</Age> </Employee> </Personnel>
Output (console):
No of Employees ‘3’.
Employee Details - Name:Seagull, Type:permanent, Id:3674, Age:34. Employee Details - Name:Robin, Type:contract, Id:3675, Age:25. Employee Details - Name:Crow, Type:permanent, Id:3676, Age:28.
Note: In some real world applications, other than printing the output to console, you might need to obtain an xml file from a third party vendor which you need to parse and update your database accordingly.
In the following, a sample program is provided. It uses a Document Object
Model (DOM) parser to parse employees.xml and create a corresponding
DOM Document object, get the information of each employee and store it into an Employee value object, and then add it to a list. The DOM parser implements the DOM API and creates a DOM tree (i.e., tree representation of XML document that we covered in the lecture) in the memory for an XML document.
1. Create the folder d:\XMLParser
2. Download the files DomParserExample.java, employees.xml, and
Employee.java from the course website and save it into the folder. Study the program and try to understand how it works. Actually there are four main steps to achieve the task:
a) Get a document builder using document builder factory and parse the xml file to create a DOM document object
b) Get a list of employee elements from the DOM document
c) For each employee element, get the id, name, age and type, store the information into an employee value object, and then add it to a list.
d) At the end, iterate through the list and print the employees to verify we parsed it right.
3. To compile, launch the Command Prompt (Start>Run> [“cmd”]) and go to the path d:\XMLParser [“cd d:\XMLParser”]. Then, execute the command
javac DomParserExample.java
4. To run, type
java DomParserExample
1.2 Generating XML documents
The previous subsection illustrates how to parse an existing XML file using DOM Parser. But generating an XML file from scratch is a different story. For instance you might like to generate an xml file based on the data extracted from a database for later on display or message exchange. To keep the example simple, the sample program XMLCreatorExample.java here provided generates XML documents (called book.xml) from hard coded data, with the XML format shown as follow:
<?xml version="1.0" encoding="UTF-8"?> <Books>
<Book Subject="Java 1.5">
<Author>Kathy Sierra .. etc</Author> <Title>Head First Java</Title> </Book>
<Book Subject="Java Architect">
<Author>Kathy Sierra .. etc</Author> <Title>Head First Design Patterns</Title> </Book>
</Books>
1. Download XMLCreatorExample.java, Book.java to d:\XMLParser.
Study the program and try to understand how it works. Actually, there are four main steps:
a) Load data
b) Get an instance of Document object using document builder factory c) Create the root element Books
d) For each item in the list create a Book element and attach it to Books element
e) Serialize DOM to FileOutputStream to generate the xml file "book.xml"
2. Go to command prompt and go to d:\XMLParser
3. To compile, type javac XMLCreatorExample.java
4. To run, type java XMLCreatorExample
5. Check the output file book.xml
1.3 Exercise
Create a java program (with filename DomParserExercise.java) to convert
the input XML file (employees.xml) to an output XML (with filename
2IT104: SERVICE ORIENTED COMPUTING
Output (employees_output.xml)
<?xml version="1.0" encoding="UTF-8"?>
<Personnel>
<Employee type="permanent">
<details Age="34" Id="3674" Name="Seagull"/> </Employee>
<Employee type="permanent">
<details Age="28" Id="3676" Name="Crow"/> </Employee>