2002 XML 10.1XML I (Parsing) 17.1XML II (XLST,XPATH) (keinPraktikum) 24.1XML III FOP 31.1Cocoon2, XSP 7.2Struts, Turbine, Velocity 14.2Testat / Evaluation / Prüfung
Wie weiter? Bis jetzt: -einfache dynamische Anwendungen mit Servlet und JSP Technologie Wunsch: QuellDok verschiedene Ausgabeformate kein out.println(….) Trennung Inhalt, Präsentation Technologie XML
XML Daten aus verschiedenen heterogenen Systemen, z.B. Datenbanken oder Directory Services, müssen ohne Verlust einer einzelnen Dezimalstelle transformiert werden. XML -> Datenaustausch B2B -> DTD Verschieden Ausgabeformate müssen unterstützt werden. (HTML,WAP,PDF,…) Smart Agents -> Strukturierte Dokumente
Was ist XML? XML ist die Extensible Markup Language Eine Metasprache zur Definition anderer Sprachen. Plattform und Sprach unabhängig Java + XML = Portable Code + Portable Data
Was ist XML? XML besitzt keine Grammatik und keine Tag Sets. vollständig Erweiterbar. Bsp: In HTML ist definiert, hat keine Bedeutung. weitere Akronyme: PI Processing Instruction DTD Document Type Definition (def. Gramatik) XSL Extensible Stylesheet Language XPATHXML Path Language FOFormating objects Namespaces Abbildung eines prefix auf eine URI
Grundkonzept I XML Dokumente müssen wohlgeformt (well-formed) sein. bla bla Errror !!
Grundkonzept II XML Dokumente können gültig (valid) sein. Ein gültiges Dokument erfüllt eine document type definition (DTD). Oder ein Shema XML basiert auf dem universellen Unicode. !! Unicode Editor !! ä &x00e4;
Interpretieren und Parsen Document Object Model (DOM) Dokument Baum im Speicher Simple API for XML (SAX) serielles parsen des Dokuments und abfeuern von Events SAX oder DOM? DOM -> Modifizieren SAX -> einmaliges Parsen
Xerces XML-Parser Implementation
Begriffe Tags und Attribute Element Typ Deklaration
Los gehts 1 XML-Dokument für Schach 2 DTD erstellen 3 Parsen mit SAX 4 Parsen mit DOM Referenzen
XML Schach
Schach DTD <!ENTITY % figur "KOENIG, DAME?, LAUEFER?, LAUEFER?, TURM?, TURM?, PFERD?, PFERD?, BAUER?, BAUER?, BAUER?, BAUER?, BAUER?, BAUER?, BAUER?, BAUER?" > <!ATTLIST POSITION SPALTE (A|B|C|D|E|F|G|H) #REQUIRED REIHE (1|2|3|4|5|6|7|8) #REQUIRED >
SAX Parser I import org.xml.sax.*; import org.xml.sax.helpers.*; import javax.xml.parsers.*; import java.io.*; public class SchachSAXPrinter { static private SAXParser parser; private PrintStream out; //Konstruktor public SchachSAXPrinter(boolean validating) throws Exception{ } public void print(String fileName) throws SAXException, IOException { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(true);
SAX Parser II try { DefaultHandler handler = new MyHandler(); parser = factory.newSAXParser(); parser.parse(fileName, handler); } catch (SAXException se) {System.out.println(se); // handle error } catch (IOException ioe) {System.out.println(ioe);// handle error } catch (ParserConfigurationException pce) {System.out.println(pce);// handle error } return; } public static void main(String[] args) throws Exception{ boolean validating = true; SchachSAXPrinter saxPrinter = new SchachSAXPrinter(true); long time = System.currentTimeMillis(); saxPrinter.print("file:spiel1.xml"); System.err.print((((double) (System.currentTimeMillis() - time)) / 1000 ) + "\t"); }
Handler I import org.xml.sax.*; import org.xml.sax.helpers.*; import javax.xml.parsers.*; public class MyHandler extends DefaultHandler { private boolean whitePiece = false; protected int fIndent; public void startElement(String namespaceURI, String localName, String name, Attributes attrs)throws SAXException { if (name.equals("WEISS")) { whitePiece = true; } else if (name.equals("SCHWARZ")) { whitePiece = false; } else if (name.equals("KOENIG") || name.equals("DAME") || name.equals("LAUEFER") || name.equals("TURM") || name.equals("PFERD") || name.equals("BAUER")) { System.out.print((whitePiece ? "weiss" : "schwarz") + " "+ name.toLowerCase() + ": ");
Handler II } else if (name.equals("POSITION")) { if (attrs != null) { System.out.print(attrs.getValue("SPALTE")); System.out.println(attrs.getValue("REIHE")); } return; } public void error(SAXParseException exception) throws SAXException { System.out.println("**Parsing Error**\n" + " Line: " + exception.getLineNumber() + "\n" + " URI: " + exception.getSystemId() + "\n" + " Message: " + exception.getMessage()); //throw new SAXException("Error encountered"); }
Weitere Events package schach; import org.xml.sax.*; import org.xml.sax.helpers.*; class MyContentHandler implements ContentHandler { private Locator locator; private boolean whitePiece = false; public void setDocumentLocator(Locator locator) { System.out.println(" * setDocumentLocator() called"); // We save this for later use if desired. this.locator = locator; } public void startDocument() throws SAXException { System.out.println("Parsing begins..."); } public void endDocument() throws SAXException { System.out.println("...Parsing ends."); } public void processingInstruction(String target, String data) throws SAXException { System.out.println("PI: Target:" + target + " and Data:" + data); } public void startPrefixMapping(String prefix, String uri) { System.out.println("Mapping starts for prefix " + prefix + " mapped to URI " + uri); }
DOM Parser I import org.w3c.dom.*; import org.xml.sax.*; import javax.xml.parsers.*; import java.io.*; public class SchachDOMPrinter { static private DocumentBuilder builder; public SchachDOMPrinter() throws Exception{ } public void print(String fileName) throws SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true); try { builder = factory.newDocumentBuilder(); builder.setErrorHandler(new MyErrorHandler()); Document document = builder.parse(fileName);
DOM Parser II NodeList nodes_i = document.getDocumentElement().getChildNodes(); for (int i = 0; i < nodes_i.getLength(); i++) { Node node_i = nodes_i.item(i); if (node_i.getNodeType() == Node.ELEMENT_NODE && ((Element) node_i).getTagName().equals("SCHACHBRETT")) { Element chessboard = (Element) node_i; NodeList nodes_j = chessboard.getChildNodes(); for (int j = 0; j < nodes_j.getLength(); j++) { Node node_j = nodes_j.item(j); if (node_j.getNodeType() == Node.ELEMENT_NODE) { Element pieces = (Element) node_j; NodeList nodes_k = pieces.getChildNodes(); for (int k = 0; k < nodes_k.getLength(); k++) { Node node_k = nodes_k.item(k); if (node_k.getNodeType() == Node.ELEMENT_NODE) { Element piece = (Element) node_k; Element position = (Element) piece.getChildNodes().item(0); System.out.println((pieces.getTagName().equals("WEISS")? "weiss " : "schwarz ") + piece.getTagName().toLowerCase() + ": " + position.getAttribute("SPALTE") + position.getAttribute("REIHE")); } … … … …
DOM Parser III } catch (SAXException se) {System.out.println(se); // handle error } catch (IOException ioe) {System.out.println(ioe);// handle error } catch (ParserConfigurationException pce) {System.out.println(pce);// handle error } return; } public static void main(String[] args) throws Exception{ boolean validating = true; SchachDOMPrinter DOMPrinter = new SchachDOMPrinter(); long time = System.currentTimeMillis(); DOMPrinter.print("file:spiel1.xml"); System.err.print((((double) (System.currentTimeMillis() - time)) / 1000 ) + "\t"); }
Legi XML Praktikum 1.XML Dokument 2.DTD 3.SAX Parser 4.DOM Parser Beising Edith Weil Deutschland Phil. II W.S. 2001/02