xml and its usesupe dearborn, 2/9/2004 parserscopyright 2004 by blair schneider mckayslide 1 agenda...

12
Parsers Copyright 2004 by Blair Schneid er McKay Slide 1 XML And Its Uses UPE Dearborn, 2/9/2004 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

Upload: lindsey-wilkins

Post on 27-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: XML And Its UsesUPE Dearborn, 2/9/2004 ParsersCopyright 2004 by Blair Schneider McKaySlide 1 Agenda Introduction - "Why XML?" Section 1: XML Basics Section

Parsers Copyright 2004 by Blair Schneider McKay Slide 1

XML And Its Uses UPE Dearborn, 2/9/2004

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

Page 2: XML And Its UsesUPE Dearborn, 2/9/2004 ParsersCopyright 2004 by Blair Schneider McKaySlide 1 Agenda Introduction - "Why XML?" Section 1: XML Basics Section

Parsers Copyright 2004 by Blair Schneider McKay Slide 2

XML And Its Uses UPE Dearborn, 2/9/2004

Role of the Parser

My Program

My Code

XML Parser

API

• The parser does the “grunt work” of deconstructing the XML.

• Calling code communicates with the parser via an API (Application Program Interface).

Page 3: XML And Its UsesUPE Dearborn, 2/9/2004 ParsersCopyright 2004 by Blair Schneider McKaySlide 1 Agenda Introduction - "Why XML?" Section 1: XML Basics Section

Parsers Copyright 2004 by Blair Schneider McKay Slide 3

XML And Its Uses UPE Dearborn, 2/9/2004

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.

Page 4: XML And Its UsesUPE Dearborn, 2/9/2004 ParsersCopyright 2004 by Blair Schneider McKaySlide 1 Agenda Introduction - "Why XML?" Section 1: XML Basics Section

Parsers Copyright 2004 by Blair Schneider McKay Slide 4

XML And Its Uses UPE Dearborn, 2/9/2004

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).

A B

Registration CallbackA B

Page 5: XML And Its UsesUPE Dearborn, 2/9/2004 ParsersCopyright 2004 by Blair Schneider McKaySlide 1 Agenda Introduction - "Why XML?" Section 1: XML Basics Section

Parsers Copyright 2004 by Blair Schneider McKay Slide 5

XML And Its Uses UPE Dearborn, 2/9/2004

Event-Driven Parsers

<mirror>

<source>\\server01\active_files</source>

. . .

</mirror>

startElement(... “mirror” ...)

startElement(... “source” ...)

charHandler(“\\server01...”)

endElement(“source”)

endElement(“mirror”)

Page 6: XML And Its UsesUPE Dearborn, 2/9/2004 ParsersCopyright 2004 by Blair Schneider McKaySlide 1 Agenda Introduction - "Why XML?" Section 1: XML Basics Section

Parsers Copyright 2004 by Blair Schneider McKay Slide 6

XML And Its Uses UPE Dearborn, 2/9/2004

Tree-Based Parsers

<mirror>

<source>\\server01\active_files</source>

. . .

</mirror>

DOM_NodeList* list = domDoc->getChildNodes();

DOM_Node* node = list->item(0);

string text = node->nodeValue();

Page 7: XML And Its UsesUPE Dearborn, 2/9/2004 ParsersCopyright 2004 by Blair Schneider McKaySlide 1 Agenda Introduction - "Why XML?" Section 1: XML Basics Section

Parsers Copyright 2004 by Blair Schneider McKay Slide 7

XML And Its Uses UPE Dearborn, 2/9/2004

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.

<!ELEMENT link (host,host)><!ELEMENT host EMPTY><!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)

<link> <host name="server1"> <host name="server2"></link>

Page 8: XML And Its UsesUPE Dearborn, 2/9/2004 ParsersCopyright 2004 by Blair Schneider McKaySlide 1 Agenda Introduction - "Why XML?" Section 1: XML Basics Section

Parsers Copyright 2004 by Blair Schneider McKay Slide 8

XML And Its Uses UPE Dearborn, 2/9/2004

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="http://www.w3.org/2001/XMLSchema" targetNamespace="UPE3" xmlns="UPE3" elementFormDefault="unqualified"> <xsd:element name="link"> <xsd:complexType> <xsd:sequence> <xsd:element ref="host" minOccurs="2" maxOccurs="2"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="host"> <xsd:complexType> <xsd:attribute name="name" type="xsd:string" use="required"/> </xsd:complexType> </xsd:element></xsd:schema>

Page 9: XML And Its UsesUPE Dearborn, 2/9/2004 ParsersCopyright 2004 by Blair Schneider McKaySlide 1 Agenda Introduction - "Why XML?" Section 1: XML Basics Section

Parsers Copyright 2004 by Blair Schneider McKay Slide 9

XML And Its Uses UPE Dearborn, 2/9/2004

Why Use XML-Based Validation?

To avoid writing program-based validation!

Compare this (pseudo-)code:if (topElem != “mirror”) then reportError(...)end ifwhile (topElem has children) child = get next child if (child != “source” AND child != “dest”) then reportError(...) end if do somethingend if

To this DTD:<!ELEMENT mirror (source+, dest)>

Page 10: XML And Its UsesUPE Dearborn, 2/9/2004 ParsersCopyright 2004 by Blair Schneider McKaySlide 1 Agenda Introduction - "Why XML?" Section 1: XML Basics Section

Parsers Copyright 2004 by Blair Schneider McKay Slide 10

XML And Its Uses UPE Dearborn, 2/9/2004

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

Page 11: XML And Its UsesUPE Dearborn, 2/9/2004 ParsersCopyright 2004 by Blair Schneider McKaySlide 1 Agenda Introduction - "Why XML?" Section 1: XML Basics Section

Parsers Copyright 2004 by Blair Schneider McKay Slide 11

XML And Its Uses UPE Dearborn, 2/9/2004

Typical Log OutputTask #8 started at 20040208.123921........

20040208.123923|DELETED 10 rows from table 'employees' to db 'offsite_archival'.

(2272 msec elapsed).

20040208.123925|UPDATED 22 rows from table 'checks' to db 'offsite_archival'. (

2748 msec elapsed).

20040208.123927|XFERRED 8 rows from table 'holidays' to db 'offsite_archival'.

(1871 msec elapsed).

**** ERROR! ****

20040208.123928|DELETED failed on table 'holidays' at db 'offsite_archival'.Erro

r reported: 10324("access denied"). (3449 msec elapsed).

**** END OF ERROR ****

20040208.123931|XFERRED 50 rows from table 'sites' to db 'offsite_archival'. (2

704 msec elapsed).

...

Page 12: XML And Its UsesUPE Dearborn, 2/9/2004 ParsersCopyright 2004 by Blair Schneider McKaySlide 1 Agenda Introduction - "Why XML?" Section 1: XML Basics Section

Parsers Copyright 2004 by Blair Schneider McKay Slide 12

XML And Its Uses UPE Dearborn, 2/9/2004

Run "admin_dashboard.htm" ...

XML And Its Uses UPE Dearborn, 2/9/2004

Copyright 2004 by Blair Schneider McKayIntroduction Copyright 2004 by Blair Schneider McKay