Content Handler startDocument() Receive notification of the beginning of a document. startElement (String namespaceURI, String localName, String qName, Attributes atts) Receive notification of the beginning of an element. characters (char[] ch, int start, int length) Receive notification of character data. endElement(String namespaceURI, String localName, String qName) Receive notification of the end of an element. endDocument() Receive notification of the end of a document. © Helmar Burkhart • Webtechnologies (CS211) • Lesson 8: XML Technologies 8-41 First SAX Example import org.xml.sax.helpers.DefaultHandler; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; public class SAX1 extends DefaultHandler { private String doc; public SAX1(String doc){ this.doc=doc; } public void startDocument() { System.out.println("document " + doc + " started"); } public void endDocument() { System.out.println("document ended"); } © Helmar Burkhart • Webtechnologies (CS211) • Lesson 8: XML Technologies 8-42 First SAX Example (contd.) public static void main(String args[]) throws Exception { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); sp.parse(args[0]+ ".xml", new SAX1(args[0]+ ".xml")); } <?xml version="1.0" standalone="yes"?> <sample> <step duration="1">abc</step> <step duration="2">abc</step> </sample> © Helmar Burkhart • Test document sample.xml Webtechnologies (CS211) • Lesson 8: XML Technologies 8-43 More SAX Examples • SAX2: • SAX3: • SAX4: Generation of parsing events. More detailed parsing events. Count elements using hash map. • pizza: Transform XML to HTML. © Helmar Burkhart • Webtechnologies (CS211) • Lesson 8: XML Technologies 8-44 Parser Modes Namespace awareness has explicitly to be set: • factory.setNamespaceAware(true); Check for validity has explicitly to be set: • factory.setValidating(true); © Helmar Burkhart • Webtechnologies (CS211) • Lesson 8: XML Technologies 8-45 Error Handler is Mandatory public void error(SAXParseException e) throws SAXParseException { throw e; } public void warning(SAXParseException err) throws SAXParseException { System.out.println("** Warning" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId()); System.out.println(" " + err.getMessage()); © Helmar Burkhart • Webtechnologies (CS211) • Lesson 8: XML Technologies 8-46 Reading Characters • SAX-Parsers may return all contiguous character data in a single chunk, but they may also be split into several chunks. • Application program has to collect all characters in a buffer in order to fulfill this requirement. © Helmar Burkhart • Webtechnologies (CS211) • Lesson 8: XML Technologies 8-47 Links • JAXP: java.sun.com/xml/jaxp/ • SAX: www.saxproject.org/ • Webservices Tutorial Chapter 7: SAX java.sun.com/webservices/docs/1.3/tutorial/doc/index.html © Helmar Burkhart • Webtechnologies (CS211) • Lesson 8: XML Technologies 8-48 DOM Parser 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); © Helmar Burkhart • Webtechnologies (CS211) • Lesson 8: XML Technologies 8-49 Beispiele <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE SCHACH SYSTEM "dtd/schach.dtd"> <SCHACH> <SCHACHBRETT> <WEISS> <KOENIG><POSITION SPALTE="G" REIHE="1" /></KOENIG> <LAUEFER><POSITION SPALTE="D" REIHE="6" /></LAUEFER> <TURM><POSITION SPALTE="E" REIHE="1" /></TURM> <BAUER><POSITION SPALTE="A" REIHE="4" /></BAUER> <BAUER><POSITION SPALTE="B" REIHE="3" /></BAUER> <BAUER><POSITION SPALTE="C" REIHE="2" /></BAUER> <BAUER><POSITION SPALTE="F" REIHE="2" /></BAUER> <BAUER><POSITION SPALTE="G" REIHE="2" /></BAUER> <BAUER><POSITION SPALTE="H" REIHE="5" /></BAUER> </WEISS> <SCHWARZ> <KOENIG><POSITION SPALTE="B" REIHE="6" /></KOENIG> <DAME><POSITION SPALTE="A" REIHE="7" /></DAME> <BAUER><POSITION SPALTE="D" REIHE="4" /></BAUER> </SCHWARZ> </SCHACHBRETT> </SCHACH> © Helmar Burkhart • Webtechnologies (CS211) • Lesson 8: XML Technologies 8-50 Schach DTD <!ELEMENT SCHACH (SCHACHBRETT*)> <!ELEMENT SCHACHBRETT (WEISS, SCHWARZ)> <!ENTITY % figur "KOENIG, DAME?, LAUEFER?, LAUEFER?, TURM?, TURM?, PFERD?, PFERD?, BAUER?, BAUER?, BAUER?, BAUER?, BAUER?, BAUER?, BAUER?, BAUER?" > <!ELEMENT WEISS (%figur;)> <!ELEMENT SCHWARZ (%figur;)> <!ELEMENT POSITION EMPTY> <!ATTLIST POSITION SPALTE (A|B|C|D|E|F|G|H) #REQUIRED REIHE (1|2|3|4|5|6|7|8) #REQUIRED > <!ELEMENT KOENIG (POSITION)> <!ELEMENT DAME (POSITION)> <!ELEMENT LAUEFER (POSITION)> <!ELEMENT TURM (POSITION)> <!ELEMENT PFERD (POSITION)> <!ELEMENT BAUER (POSITION)> © Helmar Burkhart • Webtechnologies (CS211) • Lesson 8: XML Technologies 8-51 Java XML Binding (JAXB) Zusammengefasst erzeugt der im JAXB enthaltene Compiler speziell angepassten Quelltext, um spezielle XML-Dateien zu lesen und zu schreiben. Der Übergang vom XML-Dokument zum Objektbaum wird als unmarshalling und der Übergang von den Objekten zu XMLDokument als marshalling bezeichnet. © Helmar Burkhart • Webtechnologies (CS211) • Lesson 8: XML Technologies 8-52 XML <----> JAVA Compile xjc Schema Classes instance of follows unmarshal Objects Document marshal © Helmar Burkhart • Webtechnologies (CS211) • Lesson 8: XML Technologies 8-53 © Helmar Burkhart • Webtechnologies (CS211) • Lesson 8: XML Technologies 8-54 Spez. Dokument -> Java Klassen © Helmar Burkhart • Webtechnologies (CS211) • Lesson 8: XML Technologies 8-55 1. Schema <xs:element name = "book"> <xs:complexType> <xs:sequence> <xs:element ref = "title"/> <xs:element ref = "contents"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name = "title" type = "xs:string"/> © Helmar Burkhart • Webtechnologies (CS211) • Lesson 8: XML Technologies 8-56 Bsp: unmarshal() // Import Teil import java.io.FileInputStream; import java.io.IOException; import java.util.Iterator; import java.util.List; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import jax.inhalt.*; public class Main { public static void main( String[] args ) { © Helmar Burkhart • Webtechnologies (CS211) • Lesson 8: XML Technologies 8-57 Bsp: unmarshal() (Fortsetzung) try { JAXBContext jc = JAXBContext.newInstance( "jax.inhalt" ); Unmarshaller u = jc.createUnmarshaller(); Book book = (Book)u.unmarshal( new FileInputStream( "xml/inhalt.xml" ) ); System.out.println( "Ship the following items to: " ); Contents contents = book.getContents(); displayContents ( contents ); } catch( JAXBException je ) { je.printStackTrace(); } catch( IOException ioe ) { ioe.printStackTrace(); } } © Helmar Burkhart • Webtechnologies (CS211) • Lesson 8: XML Technologies 8-58 Bsp: unmarshal() (Fortsetzung) public static void displayContents( Contents contents ) { List chapterList = contents.getChapter(); for( Iterator iter = chapterList.iterator(); iter.hasNext(); ) { ContentsType.ChapterType Chapter = (ContentsType.ChapterType)iter.next(); System.out.println( "\t" + " Chapter: \"" +Chapter.getTitle() + "\"" ); List topicList = Chapter.getTopic(); for( Iterator iter2 = topicList.iterator(); iter2.hasNext(); ) { ContentsType.ChapterType.TopicType Topic = (ContentsType.ChapterType.TopicType)iter2.next(); System.out.println( "\t" + " --- Topic: \"" + Topic.getName()+"\"" ); } } } } © Helmar Burkhart • Webtechnologies (CS211) • Lesson 8: XML Technologies 8-59 Bsp: marshal() ...try { JAXBContext jc = JAXBContext.newInstance( "jax.inhalt" ); ObjectFactory objFactory = new ObjectFactory(); // create an empty Book Book book = objFactory.createBook(); Title title = objFactory.createTitle(); book.setTitle("test jax buch"); Contents contents = objFactory.createContents(); book.setContents(contents); ... Marshaller m = jc.createMarshaller(); m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT Boolean.TRUE); m.marshal( book, System.out ); ... © Helmar Burkhart • Webtechnologies (CS211) • Lesson 8: XML Technologies 8-60 Links • JAXB: java.sun.com/xml/jaxb/ • Webservices Tutorial Chapter 2: JAXB http://java.sun.com/webservices/docs/1.5/tutorial/doc/index.html © Helmar Burkhart • Webtechnologies (CS211) • Lesson 8: XML Technologies 8-61