Skip to content

Commit c8455d0

Browse files
author
Thomas Weise
committed
Added XML Example (and fixed small Documentation Issues)
I added examples for XML data. Later I will add examples for processing this data.
1 parent 0a02a7e commit c8455d0

9 files changed

+181
-3
lines changed

.gitignore

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,12 @@ hs_err_pid*
8888
/javaRMI/temp/
8989
/javaRMI/tmp/
9090
/javaRMI/bin/
91-
/javaRMI/.classpath
91+
/javaRMI/.classpath
92+
93+
94+
## XML
95+
/xml/java/target/
96+
/xml/java/temp/
97+
/xml/java/tmp/
98+
/xml/java/bin/
99+
/xml/java/.classpath

javaRMI/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
Here you can find examples on how to perform [Java Remote Method Invocation](https://en.wikipedia.org/wiki/Java_remote_method_invocation).
44

5-
# 1. RemotePrintServer / RemotePrintClient
5+
## 1. RemotePrintServer / RemotePrintClient
66

77
The interface [RemotePrintInterface](http://github.com/thomasWeise/distributedComputingExamples/tree/master/javaRMI/src/RemotePrintInterface.java) provides a method `print` which receives one `String`. The RMI Server [RemotePrintServer](http://github.com/thomasWeise/distributedComputingExamples/tree/master/javaRMI/src/RemotePrintServer.java) implements this method in a straightforward fashion: it prints the `String` to `System.out`. An instance of this server is created and registerd as RMI server object under the name `server` in a registry listening for requests on port `9999`. The [RemotePrintClient](http://github.com/thomasWeise/distributedComputingExamples/tree/master/javaRMI/src/RemotePrintClient.java) wants to make use of this very valuable print service: It first obtains the registry and then, from this registry, the object names `server`. This object must be an instance of [RemotePrintInterface](http://github.com/thomasWeise/distributedComputingExamples/tree/master/javaRMI/src/RemotePrintInterface.java), so we can call its `print` method. When doing so, the string we pass in is sent to the server object. Since the object actually resides in a different Java virtual machine in a different process, the string is marshalled and sent over a socket to that other process, read and unmarshalled from the socket, handed to the server object, and then printed. This would work over the network as well, we can call a method which will be executed on a different computer. And we can do so relatively conveniently without bothering too much about details.
88

99
1. [RemotePrintInterface.java](http://github.com/thomasWeise/distributedComputingExamples/tree/master/javaRMI/src/RemotePrintInterface.java)
1010
1. [RemotePrintServer.java](http://github.com/thomasWeise/distributedComputingExamples/tree/master/javaRMI/src/RemotePrintServer.java)
1111
1. [RemotePrintClient.java](http://github.com/thomasWeise/distributedComputingExamples/tree/master/javaRMI/src/RemotePrintClient.java)
1212

13-
# 2. RemotePrintServer / RemotePrintClientErroneous
13+
## 2. RemotePrintServer / RemotePrintClientErroneous
1414

1515
As the name [RemotePrintClientErroneous](http://github.com/thomasWeise/distributedComputingExamples/tree/master/javaRMI/src/RemotePrintClientErroneous.java) implies, this is a "wrong" version of the above example. The error is located in an incorrect typecast in the [RemotePrintClientErroneous](http://github.com/thomasWeise/distributedComputingExamples/tree/master/javaRMI/src/RemotePrintClientErroneous.java) program: The program incorrectly assumes that the object returned from the registry can be cast to [RemotePrintServer](http://github.com/thomasWeise/distributedComputingExamples/tree/master/javaRMI/src/RemotePrintServer.java), which it cannot. It can only be cast to [RemotePrintInterface](http://github.com/thomasWeise/distributedComputingExamples/tree/master/javaRMI/src/RemotePrintInterface.java), as done in [RemotePrintClient](http://github.com/thomasWeise/distributedComputingExamples/tree/master/javaRMI/src/RemotePrintClient.java).
1616

xml/xml/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Examples for XML
2+
3+
Here you can find some examples for the Extensible Markup Language ([XML](https://en.wikipedia.org/wiki/Xml)] and related standards.
4+
5+
## 1. course.xml
6+
7+
`course.xml` is a simple XML file which describes basic data of a course, such as
8+
the number of units, the teachers, and the participating students. It shows how
9+
data can be arranged hierarchical in an XML file and has both elements and attributes.
10+
11+
1. [course.xml](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/xml/course.xml)
12+
13+
## 2. courses.xsd
14+
15+
The example above showed a basic use case for XML, but left away a few crucial issues. For instance, how does someone who receives the `course.xml` file know whether it is [valid](https://en.wikipedia.org/wiki/XML_validation)? In other words, how does she know that the `teachers` element is allowed to contain arbitrarily many `teacher` elements? If a person interprets the data, that might not be an issue. But we want to interpret data *automatically*. We may assume that hundreds of courses XML files are generated an processed. If we want to do that, we need some sort of mechanism to perform a sanity check, ideally already when loading the files, before feeding their data into the actual processing step.
16+
17+
For this purpose, [XML Schemas](https://en.wikipedia.org/wiki/XML_Schema_%28W3C%29) exist. With an XML Schema, we can specify a blueprint for certain type of XML files, for XML files belonging to a certain [namespace](https://en.wikipedia.org/wiki/XML_namespace). Think that a XML Schema corresponds to a [`typedef`](https://en.wikipedia.org/wiki/Typedef) of a `struct` in `C`, while the XML file is the actual variable instance of it.
18+
19+
After having defined an XML namespace via a Schema, we can now use it in an XML file by declaring it via `xmlns`.
20+
21+
However, this declaration itself does not ensure that the document is actually validated. For this purpose, an [XML parser](https://en.wikipedia.org/wiki/XML#Programming_interfaces) needs to know where to find the schema. This can be done by adding a `schemaLocation` attribute to the element declaration inside the XML file. Notice that an XML namespace is identified by a [URI](https://en.wikipedia.org/wiki/Uniform_Resource_Identifier). This URI may or may not be a [URL](https://en.wikipedia.org/wiki/Uniform_Resource_Locator) and even if it is a URL, it does not necessarily point to an existing resource. In our example, I intentionally did not use a URL but `ustc:courses` as namespace. Even if I had used `http://www.test.com/xyz.xsd`, there would have been no guarantee that this file actually existed. With the `schemaLocation`, we can link the namespace URI `ustc:courses` to a link to the location of the schema in the internet, right here on GitHub [https://raw.githubusercontent.com/thomasWeise/distributedComputingExamples/master/xml/xml/courses.xsd](https://raw.githubusercontent.com/thomasWeise/distributedComputingExamples/master/xml/xml/courses.xsd). Now a validating XML parser can always download the schema and use it to check the document.
22+
23+
1. [courses.xsd](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/xml/courses.xsd)
24+
1. [courseWithNamespace.xml](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/xml/courseWithNamespace.xml)
25+
1. [courseWithNamespaceAndSchemaLocation.xml](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/xml/courseWithNamespaceAndSchemaLocation.xml)
26+
27+
## 3. courses2html.xslt
28+
29+
With Extensible Stylesheet Language Transformations ([XSLT]](https://en.wikipedia.org/wiki/XSLT)), we can transform one XML document to another - not necessarily XML - document. XSLT is basically a language which tells a transformation process what output to produce for which element and attribute. Sometimes, there might be two different XML dialects / schemas / namespaces for similar domains, e.g., there could be a `tsignhua:courses` namespace with similar purpose than our declaration in the above example. With XSLT, we could translate one document from `ustc:courses` to `tsinghua:courses`. Or we could translate our document to [HTML](https://en.wikipedia.org/wiki/HTML), i.e., we could translate our raw data to a web site. This is what we do with this example:
30+
31+
1. [courses2html.xslt](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/xml/courses2html.xslt)
32+
1. [courseWithNamespace.xml](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/xml/courseWithNamespace.xml)
33+
1. [course.html](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/xml/course.html) (the result of the transformation)

xml/xml/course.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html xmlns:c="http://www.test.com/courses.xsd">
2+
<body>
3+
<h1>Distributed Computing (60)</h1>
4+
<ul>
5+
<li>Weise, Thomas</li>
6+
<li>Chen, Xianglan</li>
7+
</ul>
8+
</body>
9+
</html>

xml/xml/course.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<course courseName="Distributed Computing">
4+
5+
<units>60</units>
6+
7+
<teachers>
8+
<teacher familyName="Weise" personalName="Thomas" />
9+
<teacher familyName="Chen" personalName="Xianglan" />
10+
</teachers>
11+
12+
<students>
13+
<student studentid="SA11111111" score="85.5" />
14+
<student studentid="SA22222222" score="73.0" />
15+
<student studentid="SA33333333" score="90.0" />
16+
</students>
17+
18+
</course>

xml/xml/courseWithNamespace.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<course courseName="Distributed Computing"
4+
xmlns="ustc:courses">
5+
6+
<units>60</units>
7+
8+
<teachers>
9+
<teacher familyName="Weise" personalName="Thomas" />
10+
<teacher familyName="Chen" personalName="Xianglan" />
11+
</teachers>
12+
13+
<students>
14+
<student studentid="SA11111111" score="85.5" />
15+
<student studentid="SA22222222" score="73.0" />
16+
<student studentid="SA33333333" score="90.0" />
17+
</students>
18+
19+
</course>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<course courseName="Distributed Computing"
4+
xmlns="ustc:courses"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:schemaLocation="ustc:courses https://raw.githubusercontent.com/thomasWeise/distributedComputingExamples/master/xml/xml/courses.xsd">
7+
8+
<units>60</units>
9+
10+
<teachers>
11+
<teacher familyName="Weise" personalName="Thomas" />
12+
<teacher familyName="Chen" personalName="Xianglan" />
13+
</teachers>
14+
15+
<students>
16+
<student studentid="SA11111111" score="85.5" />
17+
<student studentid="SA22222222" score="73.0" />
18+
<student studentid="SA33333333" score="90.0" />
19+
</students>
20+
21+
</course>

xml/xml/courses.xsd

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
3+
targetNamespace = "ustc:courses"
4+
xmlns = "ustc:courses"
5+
elementFormDefault = "qualified">
6+
7+
<xs:element name="course">
8+
<xs:complexType>
9+
<xs:sequence>
10+
11+
<xs:element name="units" type="xs:int"/>
12+
13+
<xs:element name="teachers">
14+
<xs:complexType>
15+
<xs:sequence>
16+
<xs:element name="teacher" minOccurs="1" maxOccurs="unbounded">
17+
<xs:complexType>
18+
<xs:attribute name="familyName" type="xs:string"/>
19+
<xs:attribute name="personalName" type="xs:string"/>
20+
</xs:complexType>
21+
</xs:element>
22+
</xs:sequence>
23+
</xs:complexType>
24+
</xs:element>
25+
26+
<xs:element name="students">
27+
<xs:complexType>
28+
<xs:sequence>
29+
<xs:element name="student" minOccurs="1" maxOccurs="unbounded">
30+
<xs:complexType>
31+
<xs:attribute name="studentid" type="xs:string"/>
32+
<xs:attribute name="score" type="xs:float"/>
33+
</xs:complexType>
34+
</xs:element>
35+
</xs:sequence>
36+
</xs:complexType>
37+
</xs:element>
38+
39+
</xs:sequence>
40+
41+
<xs:attribute name="courseName" type="xs:string" />
42+
</xs:complexType>
43+
</xs:element>
44+
45+
</xs:schema>

xml/xml/courses2html.xslt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
4+
xmlns:c ="ustc:courses"
5+
exclude-result-prefixes="c xsl">
6+
<xsl:output method="html" indent="yes"/>
7+
<xsl:strip-space elements="*"/>
8+
9+
<xsl:template match="c:course">
10+
<html>
11+
<body>
12+
<h1><xsl:value-of select="@courseName" /> (<xsl:value-of select="c:units" />)</h1>
13+
<xsl:apply-templates select="c:teachers"/>
14+
</body>
15+
</html>
16+
</xsl:template>
17+
18+
<xsl:template match="c:teachers">
19+
<ul>
20+
<xsl:for-each select="c:teacher">
21+
<li><xsl:value-of select="@familyName" />, <xsl:value-of select="@personalName" /></li>
22+
</xsl:for-each>
23+
</ul>
24+
</xsl:template>
25+
</xsl:stylesheet>

0 commit comments

Comments
 (0)