200 likes | 331 Vues
This document explains the drawbacks of HTML and introduces XML, an Extensible Markup Language that allows users to define custom tags for better data representation. Learn about key components such as Document Type Declarations (DTD), Extensible Stylesheet Language (XSL), and the fundamental differences between XML and HTML in data presentation. With simple APIs like SAX and DOM for XML processing, developers can facilitate improved business transactions and data exchanges over the web, ensuring well-formed and valid document structures.
E N D
IA307 - Data Exchange with XML Christophe Coenraets Manager, Technical Evangelists Internet Applications Division ccoenrae@sybase.com
Topics • Drawbacks of HTML • What is XML? • Document Type Declaration (DTD) • Extensible Style Language (XSL) • XML and Java • Simple API for XML (SAX) • Document Object Model (DOM)
TripFinder.htm Page <center><h1>Trip Finder</h1> <form action="TripFinder.jsp"> <table border=0> <tr><td>Origin:</td><td><input name="origin" value="bos"></td></tr> <tr><td>Destination:</td><td><input name="destination" value="ARU"></td></tr> <tr><td>Date:</td><td><input name="date" value="01/01/1900"></td></tr> </table><br><input type="submit" value="Search"></form> <p><table border=1 cellpadding=4> <tr><td><a href=Payment.jsp?trip=2&amount=2200>Book it</a></td> <td>TNT</td><td>Grand Hyatt Aruba</td><td>2000-05-01</td><td>7</td><td>2200</td></tr> <tr><td><a href=Payment.jsp?trip=8&amount=1900>Book it</a></td> <td>TNT</td><td>Plaza Hotel</td><td>2000-06-01</td><td>7</td><td>1900</td></tr> <tr><td><a href=Payment.jsp?trip=9&amount=2100>Book it</a></td> <td>GWV</td><td>Marriott Aruba</td><td>2000-06-01</td><td>7</td><td>2100</td></tr> </table></center>
Drawbacks of HTML • HTML document contains both data and formatting • HTML tags • Describe how page elements should look • Don't contain any information about what the data is • Issues • Difficult to identify and extract data from the document • Difficult to change the presentation style
What is XML? • Extensible Markup Language • World Wide Web Consortium standard that lets you create your own tags • Universal data description mechanism • XML separates the data and presentation • Makes documents suitable for data interchange • Allows alternative presentation styles • Simplifies business-to-business transactions on the web
XML DocumentTrip.xml <?xml version="1.0" ?> <trip> <trip_id>2</trip_id> <origin>BOS</origin> <destination>ARU</destination> <hotel>Grand Hyatt Aruba</hotel> <arrival_date>2000-05-01</arrival_date> <days>7</days> <price>2200</price> </trip>
XML Document Characteristics • No presentation information • No specification of type style or color • No specification of format: table, list, bold, etc • Tags are application-oriented • <destination>, <hotel>, etc, instead of just <p> • Easy to distinguish “123” as TripId vs Price
Enabling Business-to-Business (B2B) <TripRequest> </TripRequest> Tour Operator <TripList></TripList> Travel Agent TripList.xmlTripList.xsl <TripList></TripList> Tour Operator <TripRequest> </TripRequest>
Well Formed XML documents • An XML document is well-formed if it is syntactically correct • Document starts with <?xml…> • Tags are strictly nested • Each open <tag> has a corresponding close </tag>
Valid XML documents • An XML document is valid if it specifies and conforms to the document rules defined in a DTD • Valid XML documents are well-formed • Well-formed XML documents may be valid
Document Type Declaration (DTD) • Specifies a Document Type • Defines set of rules, grammar document must comply to • Allows for predictable data
Vacation DTDExample <!ELEMENT trip (origin, destination, hotel, arrival, nights, price)> <!ELEMENT origin (#PCDATA)> <!ELEMENT destination (#PCDATA)> <!ELEMENT hotel (#PCDATA)> <!ELEMENT arrival (#PCDATA)> <!ELEMENT nights (#PCDATA)> <!ELEMENT price (single, double)> <!ELEMENT single (#PCDATA)> <!ELEMENT double (#PCDATA)> <!ATTLIST hotel rating (na|1|2|3|4) "na">
XML DocumentTrips.xml <?xml version="1.0" ?> <!DOCTYPE vacation SYSTEM "vacation.dtd"> <trips> <trip> <trip_id>2</trip_id> <origin>BOS</origin> <destination>ARU</destination> <hotel>Grand Hyatt Aruba</hotel> <arrival_date>2000-05-01</arrival_date> <days>7</days> <price>2200</price> </trip> </trips>
Presentation Using XSL • XML documents omit all formatting • Formatting is supplied by XSL • XSL: “Extensible Style Language” • Specified separate from the XML document • Allows multiple XSL specs for one XML document • An XSL spec maps • An XML document type to HTML, or • One XML document type to another
XML and Java • Java and XML are complementary Technologies • Java = Code Portability • XML = Data Portability • Java Parsers facilitate XML document manipulation • Validate an XML document • Handle character set issues, etc • Return a Java object (a tree) for the document • Also used to build such a tree and generate a document
XML Parsers • Many XML parsers are available • Often with free license or public domain • XML parsers use two standard interfaces • SAX and DOM • Applications using SAX & DOM • Can be portable across XML parsers
Simple API for XML (SAX) • Event-driven interface to XML parsers • org. xml. sax.* package Event <?xml version="1.0" ?> <trip> … </trip> Event SAX Event
Document Object Model (DOM) • Tree data structure interface to XML parser • Models document structure as objects • Defines a Java programming language binding: • org. w3c. dom.* package • DOM is also used to build XML documents <?xml version="1.0" ?> <trip> … </trip> DOM
Creating TripListXML.jspDemo Tour Operator <TripList></TripList> Travel Agent TripList.xmlTripList.xsl <TripList></TripList> Tour Operator