CS642 Project

Implementation notes


1)  Some sample code in Java will be made available which implements a simple XML client/server application.  You are welcome to copy this code, and study it or use it as a starting point if you want to.  You are not required to use this code, it is simply provided as a convenience.

View the code

Download the code

The sample application implements a 4 function calculator as a client/server protocol.  The client reads expressions from the command line, parses the arithmetic expression into an XML message, and sends the message to the server.  The server parses the XML message and validates the XML against a DTD, computes the value of the expression, and sends the answer in another XML message back to the client.

The example is intended to demonstrate the basics of sending and receiving validated XML over sockets in a client/server protocol.  You should be able to figure out the following things by studying this code:
Detailed documentation about the Xerces-J XML library can be found at the website:
http://xml.apache.org/xerces2-j/index.html

Some more XML resource you may find helpful:

A Technical Introduction to XML
XML DTD Tutorial


2) The sample code also includes build files.  The Java version of the code uses the "ant" build system from the apache project.  The build file is called "ant.xml"  To build the Java classes, simple type "ant", which is installed on the CSL machines in /s/ant/


Here is an simple interaction with the calculator program:

Start the server in Window 1:

$ java Server

Start the client in Window 2:

$ java Client
Enter an arithmetic expression

> (4+5)*(8-6)
sending to server

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE calc PUBLIC "http://localhost/calc.dtd"
                      "calc.dtd">
<calc>
    <mult>
        <plus>
            <number val="4.0"/>
            <number val="5.0"/>
        </plus>
        <minus>
            <number val="8.0"/>
            <number val="6.0"/>
        </minus>
    </mult>
</calc>
received from server

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE answer PUBLIC "http://localhost/answer.dtd"
                        "answer.dtd">
<answer>18.0</answer>


Observe how the XML document representing the calculation request is structured hierarchially, like a tree.  There is some boilerplate at the top which identifies it as an XML document, and specifies that it will conform to a DTD called "calc.dtd".  Then, there is a single outermost element called <calc>...</calc> that is the root of the document.  Inside the <calc>...</calc> the calculation is represented as a series of embedded <plus>, <minus>, <mult>, and <div> elements.  Each element must have exactly 2 children inside of it.  These children can either be another nested operation, or the empty <number> element, which takes a numeric attribute called "val".  This attribute is required.  The <number> element is called empty because it has no other elements nested inside of it.

These rules about what a calculation request may contain is codified in the DTD, presented below:

DTD for calculator request

<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT calc (plus|minus|mult|div)>
<!ELEMENT plus ((plus|minus|mult|div|num), (plus|minus|mult|div|num))>
<!ELEMENT minus ((plus|minus|mult|div|num), (plus|minus|mult|div|num))>
<!ELEMENT mult ((plus|minus|mult|div|num), (plus|minus|mult|div|num))>
<!ELEMENT div ((plus|minus|mult|div|num), (plus|minus|mult|div|num))>
<!ELEMENT num EMPTY>
<!ATTLIST num
    val #PCDATA #REQUIRED
>




DTD for calculator response

<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT answer (#PCDATA)>