200 likes | 290 Views
Service Computing. Prof. Dr. Ramin Yahyapour IT & Medien Centrum 22 . Oktober 2009. HTTP Response. HTTP/1.0 200 OK Date: Fri, 31 Dec 2008 23:59:59 GMT Content-Type: text/html Content-Length: 1354 <html> <body> <h1>Welcome to my new Homepage!</h1> (more file contents) . . .
E N D
Service Computing Prof. Dr. Ramin YahyapourIT & Medien Centrum22. Oktober 2009
HTTP Response HTTP/1.0 200 OK Date: Fri, 31 Dec 2008 23:59:59 GMT Content-Type: text/html Content-Length: 1354 <html> <body> <h1>Welcome to my new Homepage!</h1> (more file contents) . . . </body> </html>
Sending Message via SMTP Connecting to mailhost via ether... Trying 140.252.1.54... connected 220 noao.edu Sendmail 4.1/SAG-Noao.G89 ready at Mon, 19 Jul 2008 12:47:34 MST >>> HELO sun.tuc.noao.edu 250 noao.edu Hello sun.tuc.noao.edu., pleased to meet you >>> MAIL From:<rstevens@sun.tuc.noao.edu> 250 <rstevens@sun.tuc.noao.edu>... Sender ok >>> RCPT To:<rstevens@noao.edu> 250 <rstevens@noao.edu>... Recipient ok >>> DATA 354 Enter mail, end with “.” on a line by itself >>> . 250 Mail accepted >>> QUIT 221 noao.edu delivering mail
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>
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; …
SAX Parser (2)Install Parser and Input Stream public class MySAXParser { public void parseFile(String uri) { } public static void main(String[] args) { MySAXParsermySAX = new MySAXParser(); mySAX.parseFile("booklist.xml"); } }
SAX Parser (3)Install ContentHandler and Exceptions public void parseFile(String uri) { XMLReader parser = new SAXParser(); // Create an instance of MyContentHandler ContentHandlermyHandler = 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!"); }
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 {…} … }
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 {…} … }
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 {…} }
ValidationExample DTD <!ELEMENT Book ( Title, Author, ISBN)> <!ATTLIST Book available CDATA #REQUIRED > <!ELEMENT Title (#PCDATA)> <!ELEMENT Author (#PCDATA)> <!ELEMENT ISBN (#PCDATA)>
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>
XML Document Tree XHTML Book Content Price ISBN HEAD BODY Title Author Chapter Title Chapter Transforming Document Trees
Transformations with XSL <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > …Rules… </xsl:stylesheet>
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>
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
Transformations with XSLIterations <xsl:template match=“Book"> <xsl:for-each select=“Chapter"> <li> <xsl:value-of select=“ChapterName" /> </li> </xsl:for-each> </xsl:template>
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]; DOMParserDemoparserDemo = new DOMParserDemo(); parserDemo.performDemo(uri); } }
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 (inti=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: } }
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 (inti=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 (inti=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: } }