Handouts from Web Services 1 (Dave Stampf)
DesignPatterns.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
Document : DesignPatterns.xml
Created on : September 7, 2003, 12:52 PM
Author : Dave Stampf
Description:
Purpose of the document follows.
-->
<book isbn="0-201-633511-2">
<title>Design Patterns</title>
<subtitle>Elements of Reusable Object-Oriented Software</subtitle>
<author>Eric Gamma</author>
<author>Richard Helm</author>
<author>Ralph Johnson</author>
<author>John Vlissides</author>
<publisher>Addison-Wesley Publishing Company</publisher>
<copyright>1995</copyright>
<hardcover />
</book>
DesignPatternsWithDTD.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book SYSTEM "c:/WebServicesTutorial/Book.dtd">
<!--
Document : DesignPatterns.xml
Created on : September 7, 2003, 12:52 PM
Author : Dave Stampf
Description:
Purpose of the document follows.
-->
<book isbn="0-201-633511-2">
<title>Design Patterns</title>
<subtitle>Elements of Reusable Object-Oriented Software</subtitle>
<author>Eric Gamma</author>
<author>Richard Helm</author>
<author>Ralph Johnson</author>
<author>John Vlissides</author>
<publisher>Addison-Wesley Publishing Company</publisher>
<copyright>1995</copyright>
<hardcover />
</book>
Book.dtd
<?xml version='1.0' encoding='UTF-8'?>
<!--
An example how to use this DTD from your XML document:
<?xml version="1.0"?>
<!DOCTYPE book SYSTEM "Book.dtd">
<book>
...
</book>
-->
<!--- Put your DTDDoc comment here. -->
<!ELEMENT hardcover EMPTY>
<!--- Put your DTDDoc comment here. -->
<!ELEMENT copyright (#PCDATA)>
<!--- Put your DTDDoc comment here. -->
<!ELEMENT publisher (#PCDATA)>
<!--- Put your DTDDoc comment here. -->
<!ELEMENT author (#PCDATA)>
<!--- Put your DTDDoc comment here. -->
<!ELEMENT subtitle (#PCDATA)>
<!--- Put your DTDDoc comment here. -->
<!ELEMENT title (#PCDATA)>
<!--- Put your DTDDoc comment here. -->
<!ELEMENT book ( title, subtitle?, author+, publisher, copyright, hardcover? ) >
<!ATTLIST book
isbn CDATA #IMPLIED
>
Book.java
import java.io.*;
import javax.xml.parsers.*;
import org.xml.sax.InputSource;
import org.w3c.dom.*;
public class Book {
public Book(Reader r) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
DocumentBuilder db = dbf.newDocumentBuilder();
System.out.println(db.getClass());
InputSource is = new InputSource(r);
Document doc = db.parse(is);
process(doc);
}
private void process(Document doc) {
System.out.println("Have the document");
Node book = doc.getDocumentElement();
// find the title, first author, publisher and copyright date
String title=null, author = null, pub=null, copy=null;
NodeList nl = book.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node n = nl.item(i);
String s = n.getNodeName();
if (s.equals("title")) {
title = n.getFirstChild().getNodeValue();
} else if (s.equals("author")) {
if (author == null) {
author = n.getFirstChild().getNodeValue();
} else if (! author.endsWith(", et al")) {
author += ", et al";
}
} else if (s.equals("publisher")) {
pub = n.getFirstChild().getNodeValue();
} else if (s.equals("copyright")) {
copy = n.getFirstChild().getNodeValue();
}
}
System.out.println(title + " by " + author + " published by " + pub +
" in " + copy);
}
public static void main(String[] args) throws Exception{
Reader r = new FileReader("C:/WebServicesTutorial/DesignPatternsWithDTD.xml");
Book b = new Book(r);
}
}
Shorten.java
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.xml.sax.InputSource;
import org.w3c.dom.*;
public class Shorten {
public Shorten(Reader r) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource(r);
Document doc = db.parse(is);
process(doc);
}
private void process(Document doc) throws Exception {
Node book = doc.getDocumentElement();
int numAuthors = 0;
NodeList nl = book.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node n = nl.item(i);
String s = n.getNodeName();
if (s.equals("author")) {
numAuthors++;
if (numAuthors == 1) continue;
else if (numAuthors == 2) {
n.getFirstChild().setNodeValue("et al");
} else {
book.removeChild(n);
}
}
}
// ok - output new XML - you've seen this pattern b4
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
t.transform(new DOMSource(doc), new StreamResult(System.out));
}
public static void main(String[] args) throws Exception{
Reader r = new FileReader("C:/WebServicesTutorial/DesignPatternsWithDTD.xml");
Shorten b = new Shorten(r);
}
}