WWDC2000 Session 413

Transcript

Kind: captions
Language: en
good afternoon everyone can I ask you
all a favor could you those of you who
have large empty spaces next you could
you please all kind of stand up and
shuffle over a bit so a lot of people
want to come in and see this and it's
not nearly as much fun being in the
overflow room thank you very much so
people in the middle been very helpful
here some of you on the edges can try
and squeeze in a little bit one or two
seats on the edge of makes a big
difference helps people grab a seat and
feel like they're part of the crowd here
one big happy family right
okay so we're talk about one of my
favorite topics XML
just to clarify some of you've been
coming up and asking me about you know
bug fix orders and engineering standards
I am the product marketing manager okay
I'm the marketing we need for web
objects and development platforms I
don't know the in-depth guts of all the
engineering stuff Tony or the
engineering teams can give you much
better answers on that I handle all the
warm fuzzy stuff so feel free to come
ask me about those things but just
direct your questions appropriately I'm
also responsible for what we do with
general development platforms including
open source java and open standards
which is what I'm very excited about
this session on XML how many of you are
doing things with XML today okay how do
you think you're going to do stuff with
XML in the next year or so okay so it's
definitely coming down the pike we want
to make sure that we have the best
platform web objects Mac os10
open source as you know we does have
already a sea-based XML parser that
we've open sourced as part of the darwin
project for mac OS 10
for people running into Mac OS 10 or
core foundation api's we also have some
very cool Java stuff integrated with web
objects which is what Michael will be
telling you about 9 now so introduce
sorry Tim Johnson from web objects thank
you very much
thanks Ernie so what we're going to talk
about today is web objects and XML and
Ernie Ernie kind of stole my questions
cuz everyone seems to be starting out
with questions so I'm going to ask it
again except slightly different I saw a
number of hands go up to people doing
XML stuff
so just raise those hands again people
doing XML today okay
and those of you don't keep them up came
up just for a second and those of you
doing it with web objects okay so about
half half the number of people okay
thanks so what we're going to do is talk
about XML today and we're going to start
out with the basics of XML so most of
you I think looking out there probably
know the basics we just want to kind of
cover that real quick now we're going to
talk about the mechanics of XML with web
objects now what what kind of facilities
are web objects to deal with LEC's XML
and kind of the typical scenarios I'm
going to talk about encoding and
decoding XML another way of saying is
perhaps archiving and parsing reading
and bending consuming and bending we're
gonna talk about XML being used for
server server communication there are
lots of uses of XML but what we're going
to kind of talk about here is really
server to server that's really where web
objects has a big strong role and can
can do a lot and we're going to use
content syndication kind of a very
simplified version of that as an example
to illustrate using XML with web objects
so first of the basics of XML back with
when the web started it was all about
content the tagging was really to
represent the different areas of content
it wasn't about format what XML brings
back to the table is is the content it
really is all about the content and of
course there are other ways ironically
using XML to describe the format of
formatting the XML over here but
fundamentally it's about content it is a
tag system much like HTML we have tags
accepts a little bit stricter than HTML
more akin to
have sgml in its in its structured 'no
sand rigidity does use plaintext as
you're probably aware and the good thing
of all this the good part of all this
there really is pretty easy to learn the
basics lots of books on XML and a lot of
them don't even talk about programming
at all they really treat it more like
you know just kind of a structured ascii
document and and how you how you can
interact with this document and have
this document represent meaningful and
useful information and it is absolutely
gaining a widespread acceptance for for
data interchange which is really one of
the reasons why we're going to focus a
little bit on on that server-to-server
communication so first a little sample
XML file just so we all understand what
we see looks we have tags here you can
see the very top level is a model tag
and everything else has been closed in
that we clearly have tags and attributes
the one thing I will point out for those
who are kind of from the HTML arena is
if we look at this first property list
here where it says the third line down
property name equals quantity at the
very end the far right you'll see that
that tag actually has a slash before the
the end of it this is in an empty tag so
this is something that that XML kind of
brings to the table and something you
don't haven't seen in your typical HTML
document there's just a sample bit of
XML so now let's talk about web objects
and XML traditionally web objects is a
server and it's used to vend information
out and typically it's vending
information out for webpages
so there's this notion of a request and
a response and in the server role the
web object application takes in requests
and sends out responses we start talking
about server to server communication all
of a sudden your web objects application
server is also a client to some other
location server so in a single app web
office application may play the role of
both the server and a client and so what
web objects four five adds is a
factoring out of the the parts of the
requested response that were common and
so we now have in web batteries four
five introduced into the class hierarchy
a whoa message and will request and will
response our subclasses of low message
so when you look at when you're looking
for access to certain things that used
to be in a request or response you might
have to look in low message to actually
get taxes those methods the good news of
all this is that your request in your
response have can now take on a
different role they no longer have this
predefined notion of I'm a server I'm a
client in forw
oftentimes you would have to do some
some socket connections to go out and
grab requests maybe you were in
Objective C use NS strings little go
grab this you you know go get me a
string from this URL or or something
like that well now we have a also have a
structure in the in the mechanics of
what objects to do HTTP connections and
so we can actually have that client
piece of an HTTP connection in your web
apps application so what about is four
or five ads that so it adds low message
well requests or response or subclasses
of that and we also have HTTP connection
to do the encoding and decoding of the
XML from the object world to the flat
XML text world we've added will XML
coder and will XML decoder
these are two classes that really try to
simplify the in and outbound XML data
and all those those two classes are
actually built on top of ibm's XML
parser I will make a note that in what
water is four or five to use these
classes you actually have to be using
Java as your programming language
let's talk now about encoding when you
encode you go from an object and you
want to get out an XML file and we can
actually do that with a single method
so it's encode route objects with key
that you passed an object and you pass
it what XML tag you want associated with
that top-level route and what you get
back is an XML document this this XML
coder will XML coder object can actually
do this one method just kind of it
magically works for any of the common
types NS array and s dictionary
Enterprise objects string number date
and maybe a few others it's in the
documentation I would definitely
recommend that you take a look at the
XML documentation that's in the what
values four or five there's a PDF that
describes all this as I'm going through
spree reference so with this single
method you can create these this XML
representation of these objects if you
have a data type that's not supported by
that what you can do is you can write a
custom method it's basically in code
objects for key that will your past your
past the object and your responsibility
is to to return the XML representation
of that object one of the things that
you can this is all good and great okay
I have a bunch of I have an EO and I
just want to get an XML representation
of that EO I just send this I send this
object to this to the coder let it go to
work the one thing to be aware of is
that by default it'll it'll take that
object and code it and then follow all
the relationships all the related
objects and encode them as well so the
downside of this is that as you navigate
relationships it goes to the next object
so you have let's say you have an
employee you archive the it codes the
employee it then goes to the depart
codes it but then there's an inverse
relationship going back to employees so
then it codes all the employees for that
department so you can imagine that in a
well connected object model everything
your entire database gets encoded not
exactly what you want
in most situations so ways of avoiding
this one is on your EO you can actually
implement the custom encoder method and
say which attributes in which
relationships to actually encode and not
you can also use a mapping file which
I'm going to show in just a second or
the other way is actually if it's
possible you can actually turn off the
class property of the inverse
relationships it's not a class property
it doesn't actually follow it to encode
so let's take a look at this method the
top one is kind of a single don't do any
work throw in an object and get a get an
XML out XML document out and it's
basically you create a coder and you
send it the encode route object for key
message and the bottom one is using a
mapping file you basically create a
coder with the mapping file and then
send that same message so what does a
mapping file look like using XML we
describe the mapping file it's this is
actually the same one I showed earlier
today what it says is what your entity
name is and it says the texan alt tag to
use and which attributes of that to
actually encode with some flags to to
deal with special cases
now decoding again single method you can
actually decode your object into your
exit you can decode your XML into an
object so you have this XML file and you
want an object from it with the single
method what you generally get back is
either an array or a dictionary or an
array of dictionaries and we're going to
show this in the demo a little later you
can also use a mapping file to specify
how that encoding or decoding coding
actually
happens from the encoding standpoint
you're typically going to do that use a
mapping file in order to adhere to a
certain DTD and decoding you're going to
use that to basically map the XML
someone's giving you into your your
objects your enterprise objects and the
attributes that are there unless you've
convinced the other person to use the
exact same names that you are you're
using your Yeo's this is a side note for
decoding and kind of excuse me parsing
the documents you also have access the
IBM parsers there so you can use the sax
API to deal with that and the response
object you can actually if it's an XML
document you can actually ask it
give me a Dom document from this from
the request from the response content
so there's HTTP connection kind of thing
you get a document back if it's XML you
can just ask that response object give
me that thing as a Dom document and
manipulate it with that API an example
of of this is related links which goes
out and uses the Netscape related links
service on the web to you give it a
website and it comes back with all the
related sites that it's all done using
mapping files so you can get a sense for
that and it's a really good example of
kind of all the little ins and outs and
different ways of using the encoder and
decoder one pitfall to avoid when you're
decoding occasionally the decoder will
look and if you have for say say three
things that have name so you have three
tags right after each other that are
named or named Bob named Jane named Tim
when you'll get us an array of three
strings well there's only one what
you're going to get is basically a
single string back instead of an array
of one size so there's a 1/4 list option
in the mapping file that will always
make it an array
even if there's only one so I've seen
that that bite a few customers so now
let's go on to kind of uses of XML and
the one we're going to focus on here is
really server to server communication
used often for data interchange and
transferring and communicating between
two applications going from the previous
discussion you might actually use XML in
this mechanism to the exchange snapshots
even if someone thinks that's a flurry
of activity but that's an example of
some kind of thing
you might you might use you also might
use this for for remote method
invocation there are a number of
protocols out there XML RPC soap is
another one that's that's gaining wide
acceptance electronic data interchange
is another example of server server just
completing a purchase order or something
like that content management it's
generally a popular use for XML and
dealing with putting content from one
place to another there are number of
people actually using this today with
web objects there's a company down in
Florida net Commerce Corp using all
sorts of XML for content management
there are a number of others that are
that I'm sure here today doing similar
kinds of things content syndication this
is what we're going to talk about in
just a second I have an article I'm a
publisher I'm an author a site that has
a bunch of authors creating content and
I want to sell that or push that out to
a number of different sites and these
sites want to get kind of the top
headline news so if I'm writers and I
want all the I want to sell my content
out we might use content syndication to
push that out or for having them have
them pull for that information
and so their number of standards all the
most of these are all based on on XML
the bottom line is they're kind of two
flows you either push the content to the
subscriber or the subscriber
periodically pulls for new content so in
the push model a new article goes in and
it gets pushed out to the servers and
they're just sitting there waiting to
accept it in the Polston scenario the
subscriber periodically ask for requests
do you have new articles and the
publisher pushes out those new articles
when asked this is actually the model
that we're going to use for our little
simple demonstration of putting this in
action so what I would like to do is
bring Michael Young on stage to give me
a hand in creating a little Simple
Syndication
client and server so what we've done
here is before we we started we created
a direct web application director web is
not a toy we create a simple direct web
application here and what we're going to
do is add syndication to this this is
going to be our publisher so I'll use
direct website add an article very
simply so what we did was create a
simple direct action class and called
syndication and we're going to add a few
methods to create this XML document on
request so the first thing we want to do
is kind of a general good coding a good
programming style we want to create one
coder object so whoa one whoa XML coder
and we're going to save that as an Ivar
on the top and we've got a bunch of code
in the middle that goes out and given a
certain client ID we'll fetch the
articles that they have not seen yet
have not received yet and those methods
simply call this one method it's called
encode article array so given an array
of articles it's going to encode it into
XML and return the response so we want
to do is we have that coder we want to
lazily initialize it so we'd want to
initialize it all the you know in the
beginning we want to wait till we
actually need it and so we check if it's
there yet and if it's not we go ahead
and and create one the next thing we
want to do is call that one method
encode root object for key passing in
the array of articles and giving it a
key in this case the key we're going to
give it is
articles
the next thing that we want to do is
create a well response so we are going
to be the vendor of this XML document
and so we're going to create a well
response that we're going to append this
XML document to so we create our new low
response and the first thing we're going
to do is create the add into that
response the XML kind of identifier for
the actual document itself seriously the
less than question mark XML version kind
of thing here and since this is a hard
one to remember we actually could just
copy and paste this just to make sure we
get it right so we now have the response
we've added kind of this XML initial
part and now we're going to go ahead and
and append that content to the response
and this is our XML string we did with
the one line in code root object for key
and finally what we want to do is set
the mime type so that the receiver of
this will actually get the understand it
as an XML document as opposed to trying
to interpret it as an HTML document and
finally see that we return the response
so we're going to do is go ahead and
build this and if someone we haven't
made any typos compile we're going to go
ahead and run it and if we could switch
to machine for I've machine five sorry
so if we bring up our browser now what
I'm going to do is access that site that
application as if I'm a Content
subscriber a syndication subscriber so
this is pretending I just want looking
for all the XML the XML version of the
doc of the article listing of all the
messages so I have that bookmarked here
and what we get now is produced by that
direct web application with that little
direct action class we'll add in we now
have an array of articles in this case
three articles you see our XML version
up here and each article has inside it
published state the text and you'll
notice that it also has references to in
this case because we encoded AOS
it has relationships in this case it to
one so on the when we get to the
building the server that sorry the
syndication client the subscriber
because we have two web obvious
applications we're going to take
advantage of that and we're actually
going to get a yo generic records back
on the other side so we'll be able to do
all of our key value coding very easily
so we have that now what we want to do
is build so if we switch back to machine
for now we want to do is build that
syndication client and add the decoding
so take this XML string that we just saw
and make objects out of it what does
that take and so we have is we have a
little application that's really just
designed to kind of show news articles
and again the first thing we want to do
is create an Ivar for the decoder just
kind of have that around and reuse it
creating a decoder or an encoder is a
little bit of a heavyweight operation we
don't want to do that for every single
request and now we want to do is create
use that web objects as a client
functionality that's been added and
create a whoa HTTP connection object and
what this takes is a host and a port
number and then we're going to go
through the process of sending a request
over the line
so we're we could start chanting faster
faster faster so now what we're going to
do is take that HTTP connection that we
have and we're actually going to send a
request through that connection and the
request we send it's actually going to
be a whoa request object so again you
get to use the same classes you've been
using before the difference is you're
using them in the the opposite manner
that you have been accustomed to in this
case we actually create well requests
and get back well responses as opposed
to the traditional role of getting
requests and making responses and after
we send that request what we want to do
is actually we have the request now we
want to do is pull the response out of
that out of that connection request so
we're going to read the response into a
response object and again do that lazy
initialization since we haven't want to
make sure we actually have a decoder
already and if we don't we need to
create one
so at this point we have the XML
document in a will request a wrote will
response and we have a decoder so now we
get to call that one method decode route
object and decode route object given
this this NS data will give us our
objects back and in this particular case
what we're getting back every time is an
array an array of articles
it may be an empty array if there were
no new articles but we're always going
to get back an array
so we decode it and we have these new
articles now what we want to do is
simply add these articles to an existing
array of articles that we have so the
very first time we run the application
it goes out and fetches all the articles
and then every other time it requests
them if there are new ones
we'll simply add that to the list of
available articles we have now there are
a whole bunch of we very simply made
this very simple we simplified the
problem and in fact we're going to ask
for new articles on every request that
comes into the application we're doing
this pretty much entirely from a demo
perspective because having a time we're
sitting there you know we sit even
longer than we have been watching
waiting for Michael to type faster
faster faster faster okay so we're done
so we're going to go ahead and run this
application so it's built and we're
going to run it now and what we're going
to do at this point is bring up both
we're going to split the screen so for
the overflow room I apologize if it gets
a little difficult to follow we're going
to have is on the screen to my left your
right we're going to have the servers
and we have both servers running there
and on the other screen we're going to
have the browser so if you want to
switch so we have the both of them
looking okay so what I'm going to do is
simply access this this got this site so
what we have here is the three articles
so when I when we did that when I first
accessed it it went across to the
subscriber ask the publisher give me
your articles and if you can highlight
Michael that the XML oh sorry I got
ahead of myself we're not logging at
this point what I'm going to do now is
we got with those three articles back I
asked for all the messages there are
currently three messages in the system
so what we're going to do now
is add a message to the system so I'm
going to go back to the I'm going to go
to the director web version of the
application this is the publisher now
and I'm just going to create a new
article so I'm going to create a new
article and say Larry Ellison Sue's San
Jose you want to land is jet at night
okay and the publication date for this
one we're going to make it May
what's today's date 18th May 18th
mm and this has to do with airports and
I'm the author here so I'll go ahead and
I'm going to save it it's going to go
into the database but no syndication
will happen immediately all right so
that article is saved now what I'm going
to do is I'm just going to click on a
link here any link will cause a request
to the application which is going to
cause like I said every request that
comes in to the news site will go off
from the subscriber to the publisher and
get the new articles and so what we'll
see is when I click on this will see the
to send the XML messages back and forth
and then it'll be added to this page
here so when I click on this it goes
across and when we see at the bottom we
see at the top is the actual XML that
got created again that yo generic record
and at the bottom you see that we
inserted one new article on the bottom
half and it's like if I click on this
you'll see that we constantly just get 0
new articles added to the system so
we've used the client capabilities in
our web obvious application HTTP
connection to send requests out get back
XML content and turn them into objects
that are rendered in our web pages and
on the publisher
I'd we've gone to the database taking
our AOS and encoded them in XML and sent
them out in this case based on a direct
action clearly there's lots of stuff
clearly there's lots of stuff you want
it you know you want a polling mechanism
you know to do it on a timer you want
security between all this but these were
we're really to highlight those api's
that you're going to want to be familiar
with to do these things so thank you
Michael and Michael view back for QA a
little bit what do I do now let's switch
back to the slides and that's using whoa
XML coder and will XML decoder to do
that XML but there is another way and it
may be so how many people have actually
used well XML coder or decoder out there
a couple alright so how many people have
done XML web objects basically using the
component model just building a
component in generating XML okay we have
a couple so a whole lot of hands went up
earlier that you're doing web objects
and XML stuff I'm missing something what
else have you used object space the XML
okay so you're just taking your objects
and created XML using other libraries
solution okay so using it using other
things to create your XML okay
well besides the will XML coder and
decoder you can use the component model
the component model basically says to
date we've been using components as part
of pages to generate a HTML document
these documents happen to be tagged so
why can't we just make these documents
of an XML type and the answer is you can
easily do that and we're going to have a
demo of an example of that coming up
real soon but web objects itself
actually uses this method my
understanding just to make sure I'm
right is the directive Java client
actually uses this to create those XML
part of the XML that goes out to the
Java client application there are
components that are generating this XML
so you can actually use what Bobby's
builder to put XML do the
petitions simply inserting the tags
using low generic container will generic
element or we can also use a new class a
new component that was added in four
five I believe called whoa
XML node it's in the well extensions
framework and it really is probably the
easiest way to learn just you want to
create an XML document you can create
and build or connect up some generic
elements generic containers bind them to
things and you should be able to create
XML very quickly without understanding
kind of the other api's and ins and outs
of encoding and decoding because you
really are just creating a character
string going out and but but you don't
want to forget to set the header to the
text XML content type so you want to set
your mind header the mime content-type
so what I do now is bring on Ron Luce
saying who you saw earlier today doing
smile it was really focusing there on
the client side putting using smiles
make quick time your browser or not use
your browser and you have QuickTime to
your UI we're going have what Ron's
gonna talk about now is how he did that
using generic element generic container
and will XML there okay so how many of
you were here for the session earlier
this morning whoa hello okay so you guys
did you guys like that okay all right
thank you so now you're probably
wondering though so exactly how did I
come how did I build the framework what
it what does it really look like okay
here's the source for the framework I've
got a whole bunch of web components and
they pretty much map directly to all the
elements that are in the smile the smile
spec each of the elements like the
document the media objects everything
has its own component and I even have a
sort of a inheritance model here that
all inherits from smile element simple
thing as Tim had said you want to
remember those
at the mimetype what I do in my
framework though is for the smile
document I actually set the mime type
for application smile since this is
smile specifically versus generic XML
each of these elements has a couple of
things in them the main the very
important one the one that you guys all
saw this morning there's the smile media
object I've got some logic in here for
actually generating the source and the
the href tag the href attribute that
goes along with the tag a whole lot of
other things that are in the smile spec
that aren't often used but for
completeness I added them here as well
so all of these though are generated
from little components and most of them
only contain Jews let's try this one
most of them only contain this this is
all that I did I've got component
content so that you can nest things and
then the actual media object that has
all of these bindings all these
beautiful bind goals so you can just
bind so by doing this you can lay them
out in Wahb the way you would layout
HTML elements that you normally would so
to see an example of that here's the
application that i showed earlier and
the video page this is actually what it
looks like beautiful smile elements
Billy that you can bind all of your
logic to you can set the mime type for
any of the media that you're sending out
four-foot smile media objects and
there's also so one thing with with all
these low components web objects really
wants to know what bindings are going to
be in there beforehand so that's why I
have all these very very long list of
variables and so
my objects now
what if you've got some things that you
don't really know ahead of time that in
my framework I don't really know ahead
of time what you people who would be it
who would build applications using this
would use and as attributes in your
smile tags so I left one thing in here a
other tag string so this lets me add
random things to to any of the tags that
get that get generated for my smile
documents so in my application here
there are some extensions to smile that
were added by quick time so quick time
player can understand a couple more tags
and one of them is the key T autoplay KP
time slider and the key T next values if
I can run this real quick so I can run
this on a different port real quick
and switch over to box box five
so it's got
so we can go and take a look at the
actual source here and let's choose
actually why don't we Sherlock for it
let's do that that's another thing so
Sherlock okay use of XML here still will
components on my app side I've described
what my application is going to send
back to Sherlock once it's found
something how it's going to be formatted
how all the results are formatted it's
formatted here as XML like so this truly
is an XML but the formatting is the same
what what's important though is I can
use the lo XML node to generate the the
tags the exactly these relevance and
result those items so I can just change
one thing we'll take a look at all the
songs in my app so let's try rock songs
well we'll try also
well well we'll skip the show-off part
okay
now I've got a direct action in my app
that will give me pop rock music we're
supposed to give me pop rock music okay
well I'll have to skip that can we go
back to number two please no wonder
seems to be something using my open base
connection alright well the main thing I
wanted to show you though is what it
looks like once we've actually generated
all the all of the once we've bound all
the tags to two values in our logic it
looks basically like this except all the
web objects tags really are changed
you've seen this before it looks just
like your HTML would and that's kind of
the beauty of it right you can still use
wob to generate something that isn't
really what you're used to generating
with web objects right you've got
instead of HTML you're generating xml
and you can use stuff beyond just
component content and web components you
can use the whoa xml node my framework
for generating sherlock information got
a sub project here called search me and
this is what the tags look like in here
you can see here I've got little XML
node and the cool thing so I've mixed
lots and I'll node with low repetitions
and low strings and low links little
hyperlink this is another advantage so
if instead of having to go through and
concentrate on using the map encoder and
generating all the extra stuff that you
would need to talk to Sherlock go to
QuickTime you just build it in what in
Wahb and it all kind of just works kind
of a nice thing about it so I suggest
that everyone try this out
it's beautiful thing yeah
thanks Ron so you have two opportunities
you can use will XML code or decoder if
you're going to be vending and consuming
and trying to go both directions perhaps
it's worth learning that if you just
want to be vending XML out to other
sites or other users or other
applications then the component model
makes a lot of sense because it's a
really natural fit for what you've
probably been doing with with generating
HTML applications so web object and XML
in summary it can be both a vendor and a
consumer of XML we've added facilities
to deal with the client side of an HTTP
connection as well as both consuming and
vending the XML itself easy encoding and
decoding encoding component model or
will XML coder decoding will XML decoder
as well as other api's mapping files if
you need DTD adherence both in and out
and it's actually used within the web
live application web objects product
itself in a number of number of
different ways direct Java client being
just just one of them
and it is used today by the number of
hands by a number of developers out
there already so I encourage you to go
and start to to create XML stuff and and
vend it to the world
so with that like to bring Michael and
run back up to answer any questions and
Ernie
any questions that you might have about
XML above it so it began for Tim in the
gang
you