technische universität dortmund
Service Computing Prof. Dr. Ramin Yahyapour
IT & Medien Centrum22. Oktober 2009
technische universität dortmund
2
HTTP ResponseHTTP/1.0 200 OKDate: Fri, 31 Dec 2008 23:59:59 GMTContent-Type: text/htmlContent-Length: 1354
<html><body><h1>Welcome to my new Homepage!</h1>(more file contents) . . .</body></html>
technische universität dortmund
3
Sending Message via SMTP
Connecting to mailhost via ether...Trying 140.252.1.54... connected220 noao.edu Sendmail 4.1/SAG-Noao.G89 ready at Mon, 19 Jul 2008 12:47:34
MST >>> HELO sun.tuc.noao.edu250 noao.edu Hello sun.tuc.noao.edu., pleased to meet you >>> MAIL From:<[email protected]>250 <[email protected]>... Sender ok >>> RCPT To:<[email protected]>250 <[email protected]>... Recipient ok >>> DATA354 Enter mail, end with “.” on a line by itself >>> .250 Mail accepted >>> QUIT221 noao.edu delivering mail
technische universität dortmund
4
XML Example<?xml version="1.0"?><booklist> <book available="yes"> <title> Java and XML </title> <author> Brett McLaughlin </author> <isbn> 3-89721-280-3 </isbn> </book> <book available="yes"> <title> Java 2 Micro Edition </title> <author> Eric Giguere </author> <isbn> 0-471-39065-8 </isbn> </book> <book available="no"> <title> Core Jini </title> <author> W. Keith Edwards </author> <isbn> 3-8272-9592-0 </isbn> </book></booklist>
technische universität dortmund
5
SAX Parser (1)Import SAX Classes
import org.xml.sax.Attributes;import org.xml.sax.ContentHandler;import org.xml.sax.Locator;import org.xml.sax.SAXException;import org.xml.sax.XMLReader;import org.apache.xerces.parsers.SAXParser;
…
technische universität dortmund
6
SAX Parser (2)Install Parser and Input Stream
public class MySAXParser {public void parseFile(String uri) {}
public static void main(String[] args) { MySAXParser mySAX = new MySAXParser(); mySAX.parseFile("booklist.xml"); }}
technische universität dortmund
7
SAX Parser (3)Install ContentHandler and Exceptions
public void parseFile(String uri) {XMLReader parser = new SAXParser();
// Create an instance of MyContentHandler ContentHandler myHandler = new MyContentHandler(); parser.setContentHandler(myHandler);
try { parser.parse(uri); } catch (IOException e) { System.out.println("Error reading file: " +
e.getMessage()); System.exit(1); } catch (SAXException e) { System.out.println("Error parsing file: " +
e.getMessage()); System.exit(1); } System.out.println("File parsed successfully!"); }
technische universität dortmund
8
SAX Parser (4)ContentHandler
class MyContentHandler implements ContentHandler { private Locator locator;
public void setDocumentLocator(Locator locator) {…}
public void startDocument() throws SAXException {…}
public void endDocument() throws SAXException {…}
public void startPrefixMapping(String prefix, String uri){…}
public void endPrefixMapping(String prefix) throws SAXException {…}
…}
technische universität dortmund
9
SAX Parser (5) ContentHandler
class MyContentHandler implements ContentHandler {
public void startElement( String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {…}
public void endElement( String namespaceURI, String localName, String qName) throws SAXException {…}
…}
technische universität dortmund
10
SAX Parser (6) ContentHandler
class MyContentHandler implements ContentHandler {
public void characters(char[] ch, int start, int length){…}
public void ignorableWhitespace(char[] ch, int start, int length)
throws SAXException {…}
public void processingInstruction(String target, String data)
throws SAXException {…}
public void skippedEntity(String name) throws SAXException {…}
}
technische universität dortmund
11
ValidationExample DTD<!ELEMENT Book ( Title,
Author,ISBN)>
<!ATTLIST Book available CDATA #REQUIRED>
<!ELEMENT Title (#PCDATA)><!ELEMENT Author (#PCDATA)><!ELEMENT ISBN (#PCDATA)>
technische universität dortmund
12
ValidationExample XML Schema<?xml version="1.0"?>
<schema targetNamespace="http://www.oreilly.com/catalog/javaxml/">
<element name="Book" type="BookType" />
<complexType name="BookType"> <element name=“Title" type="string" /> <element name=“Author" type=“string" /> <element name=“ISBN" type="string" />
<attribute name=“available" default=“yes"> <simpleType base="string"></simpleType>
</attribute>
</complexType></schema>
technische universität dortmund
13
XML Document Tree
Book
Title PriceAuthor ISBNContent
Chapter
XHTML
HEAD BODY
Title Chapter
Transforming Document Trees
technische universität dortmund
14
Transformations with XSL
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
…Rules…
</xsl:stylesheet>
technische universität dortmund
15
Transformations with XSLRules<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:template match=“Book">…Action…
</xsl:template>
</xsl:stylesheet>
technische universität dortmund
16
Transformations with XSLExample of Transformation<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:template match=“Book"> <html>
<head> <title> <xsl:value-of select="title"/> </title> </head> <body> <xsl:apply-templates select=“*" /> </body> </html> </xsl:template>
</xsl:stylesheet>
XPath-Referencing
XPath-Filtering
technische universität dortmund
17
Transformations with XSLIterations<xsl:template match=“Book">
<xsl:for-each select=“Chapter"> <li>
<xsl:value-of select=“ChapterName" /> </li>
</xsl:for-each>
</xsl:template>
technische universität dortmund
18
Using DOM
public class DOMParserDemo {
public void performDemo(String uri) { System.out.println("Parsing XML File: " + uri + "\n\n");
// Instantiate your vendor's DOM parser implementation DOMParser parser = new DOMParser();
parser.setFeature("http://xml.org/sax/features/validation", true); parser.parse(uri); Document doc = parser.getDocument();
… do something with DOM …
}}
public static void main(String[] args) {
String uri = args[0];
DOMParserDemo parserDemo = new DOMParserDemo(); parserDemo.performDemo(uri);}}
technische universität dortmund
19
Using DOM (2)Example for Accessing DOM Nodes public void printNode(Node node, String indent) { switch (node.getNodeType()) { case Node.DOCUMENT_NODE: System.out.println("<xml version=\"1.0\">\n"); // recurse on each child NodeList nodes = node.getChildNodes(); if (nodes != null) { for (int i=0; i<nodes.getLength(); i++) { printNode(nodes.item(i), ""); } } break;
case Node.ELEMENT_NODE: case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: case Node.PROCESSING_INSTRUCTION_NODE: case Node.ENTITY_REFERENCE_NODE: case Node.DOCUMENT_TYPE_NODE: }}
technische universität dortmund
20
Using DOM (2)Example for Accessing DOM Nodes public void printNode(Node node, String indent) { switch (node.getNodeType()) { case Node.DOCUMENT_NODE: case Node.ELEMENT_NODE: String name = node.getNodeName(); System.out.print(indent + "<" + name); NamedNodeMap attributes = node.getAttributes(); for (int i=0; i<attributes.getLength(); i++) { Node current = attributes.item(i); System.out.print(" " + current.getNodeName() + "=\"" + current.getNodeValue() + "\""); } System.out.println(">");
// recurse on each child NodeList children = node.getChildNodes(); if (children != null) { for (int i=0; i<children.getLength(); i++) { printNode(children.item(i), indent + " "); } }
System.out.println(indent + "</" + name + ">"); break;
case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: case Node.PROCESSING_INSTRUCTION_NODE: case Node.ENTITY_REFERENCE_NODE: case Node.DOCUMENT_TYPE_NODE: }}