Skip to content

Commit d17f480

Browse files
author
Thomas Weise
committed
Added Examples for XML Processing with Java and Updated Documentation
1 parent b9de5e7 commit d17f480

15 files changed

+404
-3
lines changed

.travis.yml

+7
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,26 @@ cache:
1919
# custom build step
2020
script:
2121
- currentDir=`pwd`
22+
# compile the sockets examples for Java and Linux
2223
- cd "$currentDir/sockets/java/src"
2324
- javac *.java
2425
- cd "$currentDir/sockets/c"
2526
- chmod 777 make_linux.sh
2627
- ./make_linux.sh
28+
# compile the Java Servlets
2729
- cd "$currentDir/javaServlets"
2830
- mvn clean compile war:war
31+
# compile the JavaServer Pages
2932
- cd "$currentDir/javaServerPages/examples"
3033
- mvn clean compile war:war
3134
- cd "$currentDir/javaServerPages/standAloneJSPsWithJetty"
3235
- mvn clean compile package
36+
# compile the Java RMI examples
3337
- cd "$currentDir/javaRMI/src"
3438
- javac *.java
39+
# compile the Java examples for XML processing
40+
- cd "$currentDir/xml/java/src"
41+
- javac *.java
3542

3643
before_install:
3744
# make sure GCC is installed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Here I provide the source codes of the examples that I use in my distributed com
1818

1919
5. [Java RMI](http://github.com/thomasWeise/distributedComputingExamples/tree/master/javaRMI/)
2020

21+
6. [XML](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/)
22+
1. [examples for XML documents and related standards](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/xml)
23+
1. [examples for XML processing with Java](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/java)
24+
2125

2226
Since I also use the same code in my slides, there are some special comments such as `//(*@\serverBox{2)}@*)` for formatting in my codes ... you can safely ignore them ^_^
2327

xml/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Examples for XML and XML Processing
2+
3+
The Extensible Markup Language ([XML](https://en.wikipedia.org/wiki/Xml)) is a way to represent data in a structured text format. There actually exist a lot of standards around XML, for specifying how a document of a specific type is structured and how to transform a document to another document of different type. Additionally, there exists an extremely broad software support for processing XML documents. In Java, you get most of this out-of-the-box. Here we provide
4+
5+
1. [examples for XML documents and related standards](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/xml)
6+
1. [examples for XML processing with Java](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/java)

xml/java/.gitignore

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Java
2+
*.class
3+
4+
# Mobile Tools for Java (J2ME)
5+
.mtj.tmp/
6+
7+
# Package Files #
8+
*.jar
9+
*.war
10+
*.ear
11+
12+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
13+
hs_err_pid*
14+
/target/
15+
16+
# other files, potentially created by build tools
17+
/.classpath
18+
/bin
19+
/classes
20+
/build
21+
/temp
22+
/tmp
23+
/out
24+
/.gradle
25+
/dependency-reduced-pom.xml

xml/java/.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>xml</name>
4+
<comment>Examples for XML Processing with Java.</comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#Wed Apr 11 17:14:29 CST 2012
2+
eclipse.preferences.version=1
3+
org.eclipse.jdt.core.compiler.doc.comment.support=disabled
4+
org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=ignore
5+
org.eclipse.jdt.core.compiler.problem.autoboxing=ignore
6+
org.eclipse.jdt.core.compiler.problem.comparingIdentical=ignore
7+
org.eclipse.jdt.core.compiler.problem.deadCode=ignore
8+
org.eclipse.jdt.core.compiler.problem.deprecation=ignore
9+
org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=enabled
10+
org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=enabled
11+
org.eclipse.jdt.core.compiler.problem.discouragedReference=ignore
12+
org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore
13+
org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore
14+
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=enabled
15+
org.eclipse.jdt.core.compiler.problem.fieldHiding=ignore
16+
org.eclipse.jdt.core.compiler.problem.finalParameterBound=ignore
17+
org.eclipse.jdt.core.compiler.problem.finallyBlockNotCompletingNormally=ignore
18+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=ignore
19+
org.eclipse.jdt.core.compiler.problem.hiddenCatchBlock=ignore
20+
org.eclipse.jdt.core.compiler.problem.includeNullInfoFromAsserts=enabled
21+
org.eclipse.jdt.core.compiler.problem.incompatibleNonInheritedInterfaceMethod=ignore
22+
org.eclipse.jdt.core.compiler.problem.incompleteEnumSwitch=ignore
23+
org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=ignore
24+
org.eclipse.jdt.core.compiler.problem.invalidJavadoc=error
25+
org.eclipse.jdt.core.compiler.problem.invalidJavadocTags=enabled
26+
org.eclipse.jdt.core.compiler.problem.invalidJavadocTagsDeprecatedRef=enabled
27+
org.eclipse.jdt.core.compiler.problem.invalidJavadocTagsNotVisibleRef=enabled
28+
org.eclipse.jdt.core.compiler.problem.invalidJavadocTagsVisibility=private
29+
org.eclipse.jdt.core.compiler.problem.localVariableHiding=ignore
30+
org.eclipse.jdt.core.compiler.problem.methodWithConstructorName=ignore
31+
org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=ignore
32+
org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=ignore
33+
org.eclipse.jdt.core.compiler.problem.missingJavadocComments=error
34+
org.eclipse.jdt.core.compiler.problem.missingJavadocCommentsOverriding=disabled
35+
org.eclipse.jdt.core.compiler.problem.missingJavadocCommentsVisibility=private
36+
org.eclipse.jdt.core.compiler.problem.missingJavadocTagDescription=all_standard_tags
37+
org.eclipse.jdt.core.compiler.problem.missingJavadocTags=error
38+
org.eclipse.jdt.core.compiler.problem.missingJavadocTagsMethodTypeParameters=disabled
39+
org.eclipse.jdt.core.compiler.problem.missingJavadocTagsOverriding=disabled
40+
org.eclipse.jdt.core.compiler.problem.missingJavadocTagsVisibility=private
41+
org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=ignore
42+
org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotationForInterfaceMethodImplementation=enabled
43+
org.eclipse.jdt.core.compiler.problem.missingSerialVersion=ignore
44+
org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=ignore
45+
org.eclipse.jdt.core.compiler.problem.noEffectAssignment=ignore
46+
org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=ignore
47+
org.eclipse.jdt.core.compiler.problem.nonExternalizedStringLiteral=ignore
48+
org.eclipse.jdt.core.compiler.problem.nullReference=ignore
49+
org.eclipse.jdt.core.compiler.problem.overridingPackageDefaultMethod=ignore
50+
org.eclipse.jdt.core.compiler.problem.parameterAssignment=ignore
51+
org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=ignore
52+
org.eclipse.jdt.core.compiler.problem.potentialNullReference=ignore
53+
org.eclipse.jdt.core.compiler.problem.rawTypeReference=ignore
54+
org.eclipse.jdt.core.compiler.problem.redundantNullCheck=ignore
55+
org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments=ignore
56+
org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=ignore
57+
org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic=ignore
58+
org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic=ignore
59+
org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=enabled
60+
org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=ignore
61+
org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled
62+
org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled
63+
org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore
64+
org.eclipse.jdt.core.compiler.problem.typeParameterHiding=ignore
65+
org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems=disabled
66+
org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=ignore
67+
org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=ignore
68+
org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=ignore
69+
org.eclipse.jdt.core.compiler.problem.unnecessaryElse=ignore
70+
org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=ignore
71+
org.eclipse.jdt.core.compiler.problem.unqualifiedFieldAccess=ignore
72+
org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException=ignore
73+
org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionExemptExceptionAndThrowable=enabled
74+
org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionIncludeDocCommentReference=enabled
75+
org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionWhenOverriding=disabled
76+
org.eclipse.jdt.core.compiler.problem.unusedImport=ignore
77+
org.eclipse.jdt.core.compiler.problem.unusedLabel=ignore
78+
org.eclipse.jdt.core.compiler.problem.unusedLocal=ignore
79+
org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation=ignore
80+
org.eclipse.jdt.core.compiler.problem.unusedParameter=ignore
81+
org.eclipse.jdt.core.compiler.problem.unusedParameterIncludeDocCommentReference=enabled
82+
org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract=disabled
83+
org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disabled
84+
org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=ignore
85+
org.eclipse.jdt.core.compiler.problem.unusedWarningToken=ignore
86+
org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=ignore

xml/java/README.md

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Examples for Java XML Processing
2+
3+
Under Java, there exist several different ways to process streams formatted in the Extensible Markup Language ([XML](https://en.wikipedia.org/wiki/Xml)) and related standards. Here you can find examples for them.
4+
5+
## 1. DOMCreateAndPrintDocument
6+
7+
An XML document is created in memory using the Document Object Model ([DOM](http://docs.oracle.com/javase/tutorial/jaxp/dom/)) and then serialized to the standard output. Instead of stdout, we could as well have used a file. So now we can create XML documents.
8+
9+
1. [DOMCreateAndPrintDocument.java](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/java/src/DOMCreateAndPrintDocument.java)
10+
11+
## 2. DOMReadAndPrintDocument
12+
13+
We load the file [course.xml](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/xml/course.xml) as DOM document from a file into memory. In memory, we modify it. Then we serialize it to the standard output.
14+
15+
1. [DOMReadAndPrintDocument.java](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/java/src/DOMReadAndPrintDocument.java)
16+
1. [course.xml](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/xml/course.xml)
17+
18+
19+
## 3. SAXReaderExample
20+
21+
The [Simple API for XML](http://www.saxproject.org/) ([SAX](https://en.wikipedia.org/wiki/Simple_API_for_XML)) is another way to load XML documents. While DOM loads the whole document contents into memory, which allows for you to edit it but is also memory consuming, SAX treats an XML document more like a stream of events. A [SAX parser](http://docs.oracle.com/javase/tutorial/jaxp/sax/parsing.html) accepts a [listener](download.oracle.com/javase/tutorial/uiswing/events/index.html)-like handler which it notifies whenever it encounters a new element or text or processing instructions in an XML stream.
22+
23+
A SAX parser is a [push parser](http://docs.oracle.com/cd/E19159-01/819-3669/bnbdy/index.html), meaning that the parser invokes the methods of your provided handler, i.e., the parser makes the decision when your application is notified about events and you have little control over this. You can imagine SAX something like a [visitor pattern](https://en.wikipedia.org/wiki/Visitor_pattern) for XML documents. Obviously, this does not require loading the whole document into memory, instead, it can be processed more efficiently, in a stream-like fashion. This has another advantage: While processing a DOM document requires the whole document to be loaded first, a SAX handler can receive the first events already after only a few bytes have been read from the source document.
24+
25+
Here we apply a SAX parser to the [courseWithNamespace.xml](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/xml/courseWithNamespace.xml) example file. The class [SAXReaderExample.java](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/java/src/SAXReaderExample.xml) not only executes the SAX parser in its `main` method, it also extends the class [`DefaultHandler`](http://docs.oracle.com/javase/7/docs/api/org/xml/sax/helpers/DefaultHandler.html), i.e., the default implementation of the SAX event listener interfaces. It therefore can receive and print SAX events.
26+
27+
1. [SAXReaderExample.java](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/java/src/SAXReaderExample.java)
28+
1. [courseWithNamespace.xml](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/xml/courseWithNamespace.xml)
29+
30+
31+
## 4. SAXReaderExampleValidating
32+
33+
While the above example can recognize namespaces in our XML document, it may not validate the document. This has two reasons:
34+
35+
1. Validation is a feature which must be turned on via `setValidating(true)`.
36+
2. The parser may not know where to find the XML schema for a given namespace. For this purpose, a `schemaLocation` attribute can be added to the XML file, as we do in [courseWithNamespaceAndSchemaLocation.xml](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/xml/courseWithNamespaceAndSchemaLocation.xml).
37+
38+
Here we unite these two steps into a new version of the SAX parsing example:
39+
40+
1. [SAXReaderExampleValidating.java](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/java/src/SAXReaderExampleValidating.java)
41+
1. [courseWithNamespaceAndSchemaLocation.xml](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/xml/courseWithNamespaceAndSchemaLocation.xml)
42+
1. [courses.xsd](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/xml/courses.xsd)
43+
44+
45+
## 5. StAXReaderExample
46+
47+
The Streaming API for XML ([StAX](https://en.wikipedia.org/wiki/StAX)) is complement to SAX: Like SAX, it presents an XML document as stream of events. However, different from SAX, it is a pull parser. Here, the user has to actively query the reader for events, process them, and then query for the next event. You can imagine this like an [iterator pattern](https://en.wikipedia.org/wiki/Iterator_pattern) for XML document elements.
48+
49+
1. [StAXReaderExample.java](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/java/src/StAXReaderExample.java)
50+
1. [courseWithNamespace.xml](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/xml/courseWithNamespace.xml)
51+
52+
53+
## 6. StAXWriterExample
54+
55+
This iterator pattern can also be "turned around": An `XMLStreamWriter` provides methods such as `writeStartElement` and `writeAttribute` which allow us to, well, build an XML document in a stream-like fashion. We do not need to build the document completely in memory and then serialize it, as in DOM, but now can build it step-by-step. This does, of course, require significantly fewer resources than DOM-based processing. Also, combine this with either StAX or SAX parsing and you get something very fast: Our process may already begin to write an output document while it is still reading its input document.
56+
57+
1. [StAXWriterExample.java](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/java/src/StAXWriterExample.java)
58+
1. [courses.xsd](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/xml/courses.xsd)
59+
60+
61+
## 7. XSLTTransform
62+
63+
With Extensible Stylesheet Language Transformations ([XSLT]](https://en.wikipedia.org/wiki/XSLT)), we can transform one XML document to another document, which does not even necessarily need to be an XML document. In the file [courses2html.xslt](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/xml/courses2html.xslt) we specify how to transform XML documents conforming to our [courses.xsd](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/xml/courses.xsd) schema to [HTML](https://en.wikipedia.org/wiki/HTML). Here you can find how we can actually perform this application, i.e., apply [courses2html.xslt](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/xml/courses2html.xslt) to [courseWithNamespace.xml](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/xml/courseWithNamespace.xml) in Java.
64+
65+
1. [XSLTTransform.java](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/java/src/XSLTTransform.java)
66+
1. [courses2html.xslt](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/xml/courses2html.xslt)
67+
1. [courses.xsd](http://github.com/thomasWeise/distributedComputingExamples/tree/master/xml/xml/courses.xsd)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import org.w3c.dom.Document; import javax.xml.parsers.DocumentBuilder;
2+
import org.w3c.dom.Element; import javax.xml.transform.stream.StreamResult;
3+
import org.w3c.dom.Node; import javax.xml.transform.TransformerFactory;
4+
import org.w3c.dom.NodeList; import javax.xml.parsers.DocumentBuilderFactory;
5+
import java.io.File; import javax.xml.transform.dom.DOMSource;
6+
7+
public class DOMCreateAndPrintDocument {
8+
public static void main(final String argv[]) throws Throwable {
9+
Document document; Node n, m;
10+
11+
document = DocumentBuilderFactory.newInstance()
12+
.newDocumentBuilder()
13+
.newDocument(); // create an empty document
14+
15+
n = document.createElement("myNewElement"); // create and add a new element
16+
document.appendChild(n);
17+
18+
m = document.createElement("myNewSubElement"); // create and add a sub-element
19+
n.appendChild(m);
20+
21+
n = document.createAttribute("myAttribute"); // create a new attribute
22+
n.setNodeValue("myAttributeValue"); // add the attribute
23+
m.getAttributes().setNamedItem(n);
24+
25+
TransformerFactory.newInstance()
26+
.newTransformer()
27+
.transform(new DOMSource(document),
28+
new StreamResult(System.out)); // print to stdout
29+
}
30+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import org.w3c.dom.Document; import javax.xml.parsers.DocumentBuilder;
2+
import org.w3c.dom.Element; import javax.xml.transform.stream.StreamResult;
3+
import org.w3c.dom.Node; import javax.xml.transform.TransformerFactory;
4+
import org.w3c.dom.NodeList; import javax.xml.parsers.DocumentBuilderFactory;
5+
import java.io.File; import javax.xml.transform.dom.DOMSource;
6+
7+
public class DOMReadAndPrintDocument {
8+
public static void main(final String argv[]) throws Throwable {
9+
Document document; Node n, m;
10+
11+
document = DocumentBuilderFactory.newInstance() // read the course example
12+
.newDocumentBuilder()// document as DOM tree
13+
.parse("../xml/course.xml");
14+
15+
n = document.createElement("myNewElement"); // create and add a new element
16+
document.getDocumentElement().appendChild(n);
17+
18+
m = document.createElement("myNewSubElement"); // create and add a sub-element
19+
n.appendChild(m);
20+
21+
n = document.createAttribute("myAttribute"); // create a new attribute
22+
n.setNodeValue("myAttributeValue"); // add the attribute
23+
m.getAttributes().setNamedItem(n);
24+
25+
26+
TransformerFactory.newInstance()
27+
.newTransformer()
28+
.transform(new DOMSource(document),
29+
new StreamResult(System.out)); // print to stdout
30+
}
31+
}

0 commit comments

Comments
 (0)