XML And Its UsesUPE Dearborn, 2/9/2004 ParsersCopyright 2004 by Blair Schneider McKaySlide 1 Agenda Introduction - "Why XML?" Section 1: XML Basics Section 2: Parsers and SAX and DOM (oh, my!) Section 3: XPath and XSLT Section 4: SOAP and WebServices Questions, Answers, and Evasions
XML And Its UsesUPE Dearborn, 2/9/2004 ParsersCopyright 2004 by Blair Schneider McKaySlide 2 Role of the Parser The parser does the “grunt work” of deconstructing the XML. Calling code communicates with the parser via an API (Application Program Interface).
XML And Its UsesUPE Dearborn, 2/9/2004 ParsersCopyright 2004 by Blair Schneider McKaySlide 3 Two Main Types of Parser APIs Event-Driven: (SAX, SAX2, etc.) - Callback-functions for generic events, like: "Start Element" "End Element" "Received Character Data" (etc..) - Fast. Small memory footprint. - Caller responsible for a lot of work. Tree-Based: (DOM, JDOM, etc.) - Entire document is loaded into memory at once, and traversed via specialized methods (getChildNodes()) or via XPath. - Slower and more resource-intensive than SAX, - Easier to use in most cases.
XML And Its UsesUPE Dearborn, 2/9/2004 ParsersCopyright 2004 by Blair Schneider McKaySlide 4 Aside: Callback Functions Callbacks are “reminders” that are passed from function A to function B, that tell B to call A when the right conditions occur. Unfortunately, in languages like C and C++, the syntax of callbacks can look very scary: typedef void (*XML_StartElementHandler)( void *userData, const XML_Char *name, const XML_Char **atts); (Function prototype from Expat, an outstanding C SAX parser by James Clark). RegistrationCallback
XML And Its UsesUPE Dearborn, 2/9/2004 ParsersCopyright 2004 by Blair Schneider McKaySlide 5 Event-Driven Parsers \\server01\active_files... startElement(... “ mirror ”...) startElement(... “ source ”...) charHandler( “ \\server01... ” ) endElement( “ source ” ) endElement( “ mirror ” )
XML And Its UsesUPE Dearborn, 2/9/2004 ParsersCopyright 2004 by Blair Schneider McKaySlide 6 Tree-Based Parsers \\server01\active_files... DOM_NodeList* list = domDoc->getChildNodes(); DOM_Node* node = list->item(0); string text = node->nodeValue();
XML And Its UsesUPE Dearborn, 2/9/2004 ParsersCopyright 2004 by Blair Schneider McKaySlide 7 Validation – DTDs Goal: Allow only one top-level element, called “link”, which must have exactly two child-elements called “host”, each of which has a “name” attribute. <!ATTLIST host name CDATA #REQUIRED > “Error at (file sample2.xml, line 4, column 28): Required attribute 'name' was not provided” (Apache’s Xalan, v. 1.3)
XML And Its UsesUPE Dearborn, 2/9/2004 ParsersCopyright 2004 by Blair Schneider McKaySlide 8 Validation – W3C Schema Language Goal: Allow only one top-level element, called “link”, which must have exactly two child-elements called “host”, each of which has a “name” attribute. <xsd:schema xmlns:xsd=" targetNamespace="UPE3" xmlns="UPE3" elementFormDefault="unqualified">
XML And Its UsesUPE Dearborn, 2/9/2004 ParsersCopyright 2004 by Blair Schneider McKaySlide 9 Why Use XML-Based Validation? To avoid writing program-based validation! Compare this (pseudo-)code: if (topElem != “mirror”) then reportError(...) end if while (topElem has children) child = get next child if (child != “source” AND child != “dest”) then reportError(...) end if do something end if To this DTD:
XML And Its UsesUPE Dearborn, 2/9/2004 ParsersCopyright 2004 by Blair Schneider McKaySlide 10 Agenda Introduction - "Why XML?" Section 1: XML Basics Section 2: Parsers and SAX and DOM (oh, my!) > Demonstration: "SysAdmin Dashboard" Section 3: XPath and XSLT Section 4: SOAP and WebServices Questions, Answers, and Evasions
XML And Its UsesUPE Dearborn, 2/9/2004 ParsersCopyright 2004 by Blair Schneider McKaySlide 11 Typical Log Output Task #8 started at |DELETED 10 rows from table 'employees' to db 'offsite_archival'. (2272 msec elapsed) |UPDATED 22 rows from table 'checks' to db 'offsite_archival'. ( 2748 msec elapsed) |XFERRED 8 rows from table 'holidays' to db 'offsite_archival'. (1871 msec elapsed). **** ERROR! **** |DELETED failed on table 'holidays' at db 'offsite_archival'.Erro r reported: 10324("access denied"). (3449 msec elapsed). **** END OF ERROR **** |XFERRED 50 rows from table 'sites' to db 'offsite_archival'. (2 704 msec elapsed)....
XML And Its UsesUPE Dearborn, 2/9/2004 ParsersCopyright 2004 by Blair Schneider McKaySlide 12 Run "admin_dashboard.htm"... XML And Its UsesUPE Dearborn, 2/9/2004 Copyright 2004 by Blair Schneider McKayIntroductionCopyright 2004 by Blair Schneider McKay