XML Data Binding - PowerPoint PPT Presentation

1 / 60
About This Presentation
Title:

XML Data Binding

Description:

This session looks at how IE4 or IE5 can be used with ... MARQUEE. . . . innerText and innerHTML. LABEL. . . . value. INPUT TYPE=TEXT. . . . checked ... – PowerPoint PPT presentation

Number of Views:184
Avg rating:3.0/5.0
Slides: 61
Provided by: alexh73
Category:
Tags: xml | binding | data | marquee

less

Transcript and Presenter's Notes

Title: XML Data Binding


1
XML Data Binding
  • Alex Homer

Write these URLs down now youll need them
http//webdev.wrox.co.uk/books/2882/xmlcase.zip
http//webdev.wrox.co.uk/books/1576/ http//www.m
icrosoft.com/java/
2
2 main topics
XML Data Binding
This session looks at how IE4 or IE5 can be used
with XML to create dynamic and network-friendly
data-handling applications using client-side data
binding and server-side XML document handling.
the two main topic areas are
  • What is XML Data Binding?
  • XML Documents and ASP

3
5 XML topics
What is XML Data Binding?
  • Never mind XML, what is Data-Binding?
  • Data-Management Techniques on the Web
  • Data-Binding in Internet Explorer 4 and 5
  • Displaying Data using Data-Binding
  • Accessing XML Data in a DSO with Script Code

4
Access design view
What is Data Binding?
5
Access runtime view
What is Data Binding?
6
Web data updates
Data-Management on the Web
7
Issues for Web data access
Data-Management on the Web
  • How do we get the data from the server to the
    client?
  • How do we store it client-side, and how (i.e.
    with what) do we bind it to controls in a Web
    page?
  • How do we get updates back to the server over
    HTTP?
  • How do we use this information on the server to
    update the original data source?

8
RDS Schematic
Data-Binding in IE4 and IE5
Microsoft Remote Data Services
9
RDS DSO
Data-Binding in IE4 and IE5
ltOBJECT CLASSID"clsidBD96C556-65A3-11D0-983A-00C
04FC29E33" ID"dsoBookList" HEIGHT0
WIDTH0gt ltPARAM NAME"Server"
VALUE"http//www.yourserver.com"gt ltPARAM
NAME"Connect" VALUE"DSNbooksUIDanonPWD"gt
ltPARAM NAME"SQL" VALUE"SELECT FROM
BookList"gt lt/OBJECTgt
  • DATASRC - the DSO that the element is bound to.
  • DATAFLD - the column or field that the element
    is bound to.
  • DATAFORMATAS - whether to parse the data (treat
    it as HTML) or just display it 'as is' (i.e. as
    text).

ltSPAN DATASRC"dsoBookList" DATAFLD"tTitle"
DATAFORMATAS"HTML"gtlt/SPANgt
10
Binding Attributes
2 main topics
11
XML Data file
Data-Binding in IE4 and IE5
OK, so RDS is great - but my data looks like this

lt?xml version"1.0"?gt ltBOOKLISTgt ltITEMgt
ltCODEgt16-041lt/CODEgt ltCATEGORYgtHTMLlt/CATEGORYgt
ltRELEASE_DATEgt1998-03-07lt/RELEASE_DATEgt
ltTITLEgtInstant HTMLlt/TITLEgt ltSALESgt127853lt/SALES
gt lt/ITEMgt ltITEMgt ltCODEgt16-048lt/CODEgt
ltCATEGORYgtScriptinglt/CATEGORYgt
ltRELEASE_DATEgt1998-04-21lt/RELEASE_DATEgt
ltTITLEgtInstant JavaScriptlt/TITLEgt
ltSALESgt375298lt/SALESgt lt/ITEMgt ... etc
... lt/BOOKLISTgt
12
The Java DSO
Data-Binding in IE4 and IE5
The Microsoft Java XML Data Source Object
ltAPPLET CODE"com.ms.xml.dso.XMLDSO.class"
ID"dsoBookList" WIDTH0 HEIGHT0
MAYSCRIPTtruegt ltPARAM NAME"URL"
VALUE"booklist.xml"gt lt/APPLETgt
Get it from http//www.microsoft.com/java/
13
The XML DSO
Data-Binding in IE4 and IE5
The IE5 ltXMLgt Data Source Object Element
ltXML ID"dsoBookList"gt lt?xml version"1.0"?gt
ltBOOKLISTgt ltITEMgt ltCODEgt16-041lt/CODEgt
ltCATEGORYgtHTMLlt/CATEGORYgt
ltRELEASE_DATEgt1998-03-07lt/RELEASE_DATEgt
ltTITLEgtInstant HTMLlt/TITLEgt
ltSALESgt127853lt/SALESgt lt/ITEMgt ltITEMgt
... lt/ITEMgt ... etc ...
lt/BOOKLISTgt lt/XMLgt
14
XML Data Gotchas
Data-Binding in IE4 and IE5
But remember that we're aiming to bind to an XML
file so the data in our source XML file must
be valid and well-formed
  • Things like ltHRgt dont conform to XML syntax
    requirements
  • Use ltHR/gt, ltBR/gt, ltINPUT TYPE"TEXT"/gt, etc.
  • HTML elements may void the schema or data type
    definition (DTD)
  • Enclose it all in a CDATA section within the XML
    document
  • lttTitlegtlt!CDATAltBgtltIgtInstant HTMLlt/Igtlt/Bgtgtlt/tT
    itlegt
  • or escape the angled brackets individually (i.e.
    'HTML encode' it)
  • lttTitlegtltBgtltIgtInstant
    HTMLlt/Igtlt/Bgtlt/tTitlegt

15
2 types of data binding
Displaying Data with Data-Binding
Two Types of Data Binding
  • Tabular data binding
  • sometimes referred to as table repetition
    agent binding
  • Single record data binding
  • often called current record data binding

16
Tabular Data Binding
2 main topics
Tabular Data Binding Example
XML Data file
... ltXML ID"dsoBookList" SRC"booklist.xml"gtlt/XML
gt ... ltTABLE DATASRC"dsoBookList"gt ltTHEADgt
ltTRgt ltTHgtCodelt/THgt ltTHgtCategorylt/THgt
ltTHgtRelease Datelt/THgt ltTHgtTitlelt/THgt
ltTHgtSaleslt/THgt lt/TRgt lt/THEADgt ltTBODYgt ltTRgt
ltTDgtltSPAN DATAFLD"CODE"gtlt/SPANgtlt/TDgt
ltTDgtltSPAN DATAFLD"CATEGORY"gtlt/SPANgtlt/TDgt
ltTDgtltSPAN DATAFLD"RELEASE_DATE"gtlt/SPANgtlt/TDgt
ltTDgtltBgtltSPAN DATAFLD"TITLE"gtlt/Bgtlt/SPANgtlt/TDgt
ltTDgtltSPAN DATAFLD"SALES"gtlt/SPANgtlt/TDgt lt/TRgt
lt/TBODYgt lt/TABLEgt ...
The use of a ltTBODYgt element is not mandatory
when you want all the contents of the table to be
repeated for each record, and in fact it is often
omitted. IE will automatically repeat all bound
elements, once for each record, even if there is
no ltTBODYgt element.
17
Table Paging
Displaying Data with Data-Binding
Table Paging with Tabular Data Binding
The properties of an ADO recordset include the
dataPageSize property. This is mapped in HTML to
the DATAPAGESIZE attribute of the table 
DATAPAGESIZE - maximum number of records to
display
Set this attribute in the opening HTML ltTABLEgt
tag to create a table that displays only a
specified number of records  ltTABLE
DATASRC"dsoBookList" DATAPAGESIZE10gt
Move through the recordset using the nextPage and
previousPage methods. In IE5 we can also use the
firstPage and lastPage.
The bound table also exposes the recordNumber
property of the underlying data set for each
element within the table.
18
Multiple Recordsets
Displaying Data with Data-Binding
Hierarchical (Master/Detail) Recordsets
To display this kind of data we have to use a
series of nested tables that mirror the hierarchy
of the source data ltTABLE DATASRC"dsoBooks"gt
ltTRgtltTDgt ltSPAN DATAFLD"BOOK_CODE"gt
ltSPAN DATAFLD"BOOK_TITLE"gt lt/TDgtlt/TRgt
ltTABLE DATASRC"dsoBooks"
DATAFLD"AUTHOR"gt ltTRgtltTDgt ltSPAN
DATAFLD"FIRST_NAME"gt ltSPAN
DATAFLD"LAST_NAME"gt lt/TDgtlt/TRgt
lt/TABLEgt lt/TDgtlt/TRgt lt/TABLEgt
Not all of our data will always be in 'flat'
tables 
19
Hierarchical XML Data
2 main topics
XML Data file
Some Hierarchical XML Data
lt?xml version"1.0" ?gt ltBOOKLISTgt ltITEMgt
ltCODEgt16-048lt/CODEgt ltTITLEgtInstant
JavaScriptlt/TITLEgt ltSALESgt375298lt/SALESgt
ltAUTHORgt ltFIRST_NAMEgtMartinlt/FIRST_NAMEgt
ltLAST_NAMEgtWilliamslt/LAST_NAMEgt lt/AUTHORgt
ltAUTHORgt ltFIRST_NAMEgtCheryllt/FIRST_NAMEgt
ltLAST_NAMEgtCaprialdilt/LAST_NAMEgt lt/AUTHORgt
lt/ITEMgt ltITEMgt ltCODEgt23-177lt/CODEgt
ltTITLEgtJavaScript Todaylt/TITLEgt
ltSALESgt118524lt/SALESgt ltAUTHORgt
ltFIRST_NAMEgtAngelalt/FIRST_NAMEgt
ltLAST_NAMEgtMillanialt/LAST_NAMEgt lt/AUTHORgt
lt/ITEMgt ... lt/BOOKLISTgt 
20
Nested Table Binding
2 main topics
XML Data file
Nested Binding to Hierarchical XML Data
ltTABLE DATASRC"dsoBookList" WIDTH100gt
ltTHEADgt ltTRgt ltTH ALIGN"LEFT"gtCodelt/THgt ltTH
ALIGN"LEFT"gtTitlelt/THgt ltTH ALIGN"LEFT"gtSaleslt/
THgt ltTH ALIGN"LEFT"gtAuthor(s)lt/THgt lt/TRgt
lt/THEADgt ltTBODYgt ltTR VALIGN"TOP"gt
ltTDgtltSPAN DATAFLD"CODE"gtlt/SPANgtlt/TDgt ltTDgtltSPAN
DATAFLD"TITLE"gtlt/SPANgtlt/TDgt ltTDgtltSPAN
DATAFLD"SALES"gtlt/SPANgtlt/TDgt ltTDgt lt!-- inner
table bound to detail recordset via DATAFLD
attribute--gt ltTABLE DATASRC"dsoBookList"
DATAFLD"AUTHOR"gt ltTRgt ltTDgt ltSPAN
DATAFLD"FIRST_NAME"gtlt/SPANgtltSPAN
DATAFLD"LAST_NAME"gtlt/SPANgt lt/TDgt lt/TRgt
lt/TABLEgt lt/TDgt lt/TRgt lt/TBODYgt lt/TABLEgt
21
Fixed Layout Tables
Displaying Data with Data-Binding
New 'Go-Faster Stripes' for Tables
Don't forget that IE5 adds fixed layout tables to
speed up the rendering of tables - no need to
wait while the whole table loads  ltTABLE
style"table-layoutfixed" width600gt ltTRgt
ltTD width25gtcell content herelt/TDgt ltTD
width40gtcell content herelt/TDgt ltTD
width35gtcell content herelt/TDgt lt/TRgt lt/TABLEgt
22
Current Record Binding 1
2 main topics
Current Record Data Binding Example
XML Data file
... ltXML ID"dsoBookList" SRC"booklist.xml"gtlt/XML
gt ... Code ltINPUT ID"Code" TYPE"TEXT"
DATASRC"dsoBookList"
DATAFLD"CODE" SIZE5gtltPgt Title ltINPUT
ID"Title" TYPE"TEXT" DATASRC"dsoBookList"
DATAFLD"TITLE" SIZE30gtltPgt Category
ltSELECT ID"Category" DATASRC"dsoBookList"
DATAFLD"CATEGORY" SIZE1gt
ltOPTION VALUE"HTML"gtHTML ltOPTION
VALUE"Scripting"gtScripting ltOPTION
VALUE"ASP"gtASP lt/SELECTgtltPgt Release
date ltSPAN ID"Release" DATASRC"dsoBookList"
DATAFLD"RELEASE_DATE"gtlt/SPANgtlt
Pgt Sales to date ltSPAN ID"Sales"
DATASRC"dsoBookList"
DATAFLD"SALES"gtlt/SPANgtltPgt ...
This automatically displays the contents of the
first record. But how do our users get to
another record?
23
Current Record Binding 2
2 main topics
Current Record Data Binding Example
XML Data file
Easy - use the move, moveFirst, moveLast,
moveNext and movePrevious methods of the DSO and
the underlying recordset
... ltTABLEgt ltTRgtltTDgt ltBUTTON ONCLICK"dsoBookList.
recordset.MoveFirst()"gt nbspltnbsp lt/BUTT
ON gt ltBUTTON onclick"if (! dsoBookList.recordset.
BOF) dsoBookList.recordset.Move
Previous()"gt nbspltnbsp lt/BUTTONgt ltBUTTON
onclick"if (! dsoBookList.recordset.EOF)
dsoBookList.recordset.MoveNext()"gt
nbspgtnbsp lt/BUTTON gt ltBUTTON
onclick"dsoBookList.recordset.MoveLast()"gt
nbspgtnbsp lt/BUTTON gt lt/TDgtlt/TRgt lt/TABLEgt .
..
24
Dynamic Binding
Displaying Data with Data-Binding
Dynamic Data Binding
The DATASRC, DATAFLD and DATAFORMATAS attributes
of an element are exposed through the dataSrc,
dataFld and dataFormatAs properties. To remove
the bindings, just set the properties to an empty
string. To change the binding of elements within
a bound table, remove the binding of the table
first (in the ltTABLEgt tag), change the bindings
of the elements in the table, and then reset the
binding of the table.
25
Browser Events
Displaying Data with Data-Binding
Browser or Control Data Binding Events
ltINPUT ID"txtTitle" DATASRC"dsoBookList"
DATAFLD"tTitle"gt ... ltSCRIPT LANGUAGE"JScript"gt
function txtTitle.onbeforeupdate() return
confirm("Are you sure you want to change this
value ?") lt/SCRIPTgt
26
DSO Events
Displaying Data with Data-Binding
DSO Data Binding Events
ltINPUT ID"txtStatus" VALUE"Initializing, please
wait ..."gt ... ltSCRIPT LANGUAGE"JavaScript"gt func
tion dsoBookList.ondatasetcomplete()
txtStatus.value "Data arrived OK" lt/SCRIPTgt
27
Access with Script
Accessing XML Data with Script
The DSO's Recordset Property
DSOs expose the underlying data through the
Recordset property  myRecordset
dsoMyDSO.recordset // JavaScript or
JScript Set myRecordset dsoMyDSO.recordset '
same but in VBScript
Once we have a reference to the recordset we can
access the fields to get their values theValue
myRecordset('field_name') // JavaScript or
JScript theValue myRecordset("field_name") '
same but in VBScript
After data loading has completed the data in our
source recordset or XML document is exposed as a
standard ADO client-side (or disconnected)
recordset - irrespective of the kind of DSO and
data file we use.
28
Elements with Attributes
Accessing XML Data with Script
XML Data Binding in IE5 with Attributes
The IE5 data-binding agent treats an attribute in
an element as a separate element, so ltmyElem
myAttr"attr_value"gtelem_valuelt/myElemgt   is
treated as being ltmyElemgtltmyAttrgtattr_valuelt/myAt
trgtelem_valuelt/myElemgt 
ltTABLE DATASRC"dsoData" DATAFLD"myElement"gt
ltTRgt ltTDgt ltINPUT TYPE"TEXT"
DATASRC"dsoData" DATAFLD"TEXT"gt lt/TDgt
lt/TRgtlt/TABLEgt
var objDSO document.all('dsoData')strValue
objDSO.recordset.fields('field_name').value.fields
('TEXT')
29
Second main topic
XML Data Binding
  • What is XML Data Binding?
  • XML Documents and ASP
  • Creating XML documents from a data store
  • Updating source data from an XML document

30
The XML source file type
Creating XML From a Data Store
In the Java DSO the source file is specified in
the URL parameter, set with a ltPARAMgt
element ltPARAM NAME"URL" VALUE"booklist.xml"gt o
r as the URL property at run-time.
In an IE5 data island, the XML source is
specified by a SRC attribute ltXML
ID"dsoBookList" SRC"booklist.xml"gtlt/XMLgt
These assume that the source is an XML-format
text file named booklist.xml. But, it could just
as easily be booklist.asp, as long as the
MIME-type of the file is "text/xml".
31
Dynamic XML from ASP
Creating XML From a Data Store
Creating an XML document dynamically is easy.
Three possibilities that we'll look at next are
  • Directly with ASP Script Code
  • With a Custom Component
  • With ADO Persistence Methods

32
ASP code for XML
2 main topics
lt Response.Buffer False 'stream XML
to the client as it's created Response.ContentType
"text/xml" 'tell browser that it's XML we're
sending gt lt?xml version"1.0"?gt ltAUTHORLISTgt lt
'-- select all the author details -- Set oConn
Server.CreateObject("ADODB.Connection") oConn.Open
"connection_string" strSQL "SELECT FROM
Authors ORDER BY tLinkName" Set oRs
oConn.Execute(strSQL) QUOT Chr(34) Do While Not
oRs.EOF strLinkName oRs("tLinkName") If
oRS.Fields("bPhoto") ltgt 0 Then strPhotoURL
"http//webdev.wrox.co.uk/authors/" strLinkName
".gif" End If If oRS.Fields("bInterview")
ltgt 0 Then strIview "http//webdev.wrox.co.u
k/authors/" strLinkName ".htm" End If
Response.Write " ltAUTHORINFOgt" vbCrlf
Response.Write " ltAUTHOR_NAMEgt" oRs("tName")
"lt/AUTHOR_NAMEgt" vbCrlf Response.Write "
ltSHORT_BIOgt" Server.HTMLEncode(oRs("tBio")) _
"lt/SHORT_BIOgt" vbCrlf
Response.Write " ltPHOTO_URLgt" strPhotoURL
"lt/PHOTO_URLgt" vbCrlf Response.Write "
ltINTERVIEW_URLgt" strIview "lt/INTERVIEW_URLgt"
vbCrlf Response.Write " lt/AUTHORINFOgt"
vbCrlf oRs.MoveNext Loop oRs.Close gt lt/AUTHORLI
STgt
33
ASP code result
2 main topics
lt?xml version"1.0" ?gt ltAUTHORLISTgt
ltAUTHORINFOgt ltAUTHOR_NAMEgtDave
Sussmanlt/AUTHOR_NAMEgt ltSHORT_BIOgtDavid
Sussman is a developer, trainer and author who
has worked with Wrox on a variety of
projects. He is also a Microsoft Certified
Professional and a Certified Solution Developer.
lt/SHORT_BIOgt ltPHOTO_URLgthttp//webdev.wrox
.co.uk/authors/sussmand.giflt/PHOTO_URLgt
ltINTERVIEW_URLgthttp//webdev.wrox.co.uk/authors/su
ssmand.htmlt/INTERVIEW_URLgt lt/AUTHORINFOgt
... lt/AUTHORLISTgt
ltXML ID"dsoBookList" SRC"authorlist.asp"gtlt/XMLgt
... ltTABLE DATASRC"dsoBookList"
CELLSPACING"10"gt ltTRgt ltTD ROWSPAN"2"gtltIMG
HSPACE"15" DATAFLD"PHOTO_URL"gtlt/TDgt ltTDgtltSPAN
CLASS"auth_name" DATAFLD"AUTHOR_NAME"gtlt/SPANgtlt/T
Dgt lt/TRgt ltTRgt ltTD VALIGN"TOP"gt ltSPAN
DATAFLD"SHORT_BIO"gtlt/SPANgtltPgt Read an ltBgtltA
DATAFLD"INTERVIEW_URL"gtinterviewlt/Agtlt/Bgt with
ltSPAN DATAFLD"AUTHOR_NAME"gtlt/SPANgt lt/TDgt
lt/TRgt lt/TABLEgt
34
With a component
Creating XML From a Data Store
Using a Custom or Commercial Component
Many suppliers provide components that can do the
same job as ASP when creating XML documents, for
example DB2XML - http//www.informatik.fh-wiesba
den.de/turau/DB2XML/index.html   XML-DBMS -
http//www.informatik.tu-darmstadt.de/DVS1/staff/b
ourret/xmldbms/readme.html   XMLServlet -
http//www.beyond.com/PKSN102998/prod.htm   There'
s a useful list at http//www.xmlsoftware.com/
Or build your own - convert ASP to Visual Basic
and wrap it up in an ActiveX DLL. Return the XML
document as a string, or stream it direct to the
browser using Response.Write. Above all, you can
create the XML with whatever structure you like. 
35
With a ASP2XML
Creating XML From a Data Store
lt 'create the component instance Set objA2X
Server.CreateObject("Stonebroom.ASP2XML")
  'set the values required to get an XML
document strConnect "connection_string" strKeyFi
eld "kBookCode" strQueryString "SELECT FROM
booklist WHERE tTitle LIKE 'Script'" blnOmitData
Types True 'makes binding easier by omitting
attributes   'execute the getXML method strResult
objA2X.getXML(strConnect, strKeyField,
strQueryString, _ , , ,
, , blnOmitDataTypes) Set objA2X Nothing
'finished with the component   'create the XML
data island Response.Write "ltXML ID" Chr(34)
"dsoBookList" Chr(34) _ "gt"
vbCrlf Response.Write strResult
vbCrlf Response.Write "lt/XMLgt" vbCrlf gt ...
36
ASP2XML Result
Creating XML From a Data Store
... ltXML ID"dsoBookList"gt lt?xml
version"1.0"?gt ltBookListgt ltA2X_Recordgt
ltA2X_RecordNumbergt00001lt/A2X_RecordNumbergt
ltA2X_UpdateActiongtNONElt/A2X_UpdateActiongt
ltA2X_RecordMatchgtkBookCode'1274'lt/A2X_RecordMatch
gt ltkBookCodegt1274lt/kBookCodegt
ltdReleaseDategt1998-01-12 000000lt/dReleaseDategt
lttTitlegtInstant JavaScriptlt/tTitlegt
lttDescriptiongtLearn JavaScript with this
booklt/tDescriptiongt lt/A2X_Recordgt ltA2X_Recordgt
...etc... lt/A2X_Recordgt lt/BookListgt lt/XMLgt ...
Download the VB source for a sample component
like this from http//webdev.wrox.co.uk/books/288
2/xmlcase.zip
37
With ADO Persistence
Creating XML From a Data Store
Using ADO Persistence Methods
Version 2.5 of ADO fully implements the data
persistence feature first introduced in ADO 2.1.
We can persist data from a recordset into XML
using the Save method of the Recordset object
with the integral ADO constant value adPersistXML
as the 'format' parameter   objRS.Save
"path_and_filename", adPersistXML
ADO 2.5 also introduces a stream object. Data can
be passed directly from one application to
another without writing it to disk. We can
stream XML created from the recordset contents
direct to the browser by simply replacing the
path and filename with the ASP Response
object   objRS.Save Response, adPersistXML
38
ADO XML Format
2 main topics
ADO Persisted XML Data Example
ltxml xmlnss'uuidBDC6E3F0-6DA3-11d1-A2A3-00AA00C
14882' xmlnsdt'uuidC2F41010-65B3-11d1-A29F-00
AA00C14882' xmlnsrs'urnschemas-microsoft-com
rowset' xmlnsz'RowsetSchema'gt ltsSchema
id'RowsetSchema'gt ltsElementType name'row'
content'eltOnly' rsupdatable'true'gt
ltsAttributeType name'kBookCode' rsnumber'1'
rswriteunknown'true'
rsbasecatalog'globalexample' rsbasetable'BookL
ist' rsbasecolumn'kBookCode
' rskeycolumn'true'gt ltsdatatype
dttype'string' dtmaxLength'50'
rsprecision'0'
rsmaybenull'false'/gt lt/sAttributeTypegt
... ... ltsextends type'rsrowbase'/gt
lt/sElementTypegt lt/sSchemagt ltrsdatagt ltzrow
kBookCode'1274' dReleaseDate'1998-01-12T000000
' tTitle'Instant JavaScript'
tDescription'Learn JavaScript now!'/gt ltzrow
kBookCode'138x' dReleaseDate'1998-03-13T000000
' tTitle'Instant Scriptlets'
tDescription'Makes Scriptlets Fun!'/gt lt/rsdatagt
lt/xmlgt
39
adopersist 1
2 main topics
Using ADO Persisted XML Data
lt 'create an XML data island from an ADO
recordset strConnect "connection_string" strSQL
"SELECT FROM BookList WHERE tTitle LIKE
'Script'" Set objRS Server.CreateObject("ADODB
.Recordset") objRS.Open strSQL, strConnect,
adOpenDynamic, adLockOptimistic,
adCmdText objRS.Save Response, adPersistXML
'save it to the Response objRS.Close gt
lt!-- table to bind DATASRC"dsoData" --gt ltTABLE
ID"tblMain"gt ltTRgtltTDgt lt!-- table to bind
DATASRC"dsoData" DATAFLD"rsdata"--gt
ltTABLE ID"tblSub"gt ltTRgtltTDgt lt!--
table to bind DATASRC"dsoData"
DATAFLD"zrow"--gt ltTABLE ID"tblData"
CELLPADDING"10"gt ltTHEADgtltTRgtlt/TRgtlt/THE
ADgt ltTBODYgtltTRgtlt/TRgtlt/TBODYgt
lt/TABLEgt lt/TDgtlt/TRgt lt/TABLEgt
lt/TDgtlt/TRgt lt/TABLEgt ...
40
adopersist 2
2 main topics
Using ADO Persisted XML Data
ltSCRIPT LANGUAGE"VBScript"gt Sub window_onload()
QUOT Chr(34) 'double-quote character 'get a
reference to the ltXMLgt data island element Set
colXML document.all.tags("XML") Set objXML
colXML(0) 'the first and only XML element
objXML.id "dsoData" 'set the ID property
'get a reference to the recordset in the data
island Set objRecs objXML.Recordset Set
objRecs objRecs("rsdata").value Set objRecs
objRecs("zrow").value 'get a reference to
the TR rows of the innermost TABLE element Set
objTableHead document.all("tblData").rows(0)
Set objTableRow document.all("tblData").rows(1)
...
41
adopersist 3
2 main topics
... 'loop through the fields in the
recordset For Each objField in objRecs.Fields
strFieldName objField.Name If
strFieldName ltgt "Text" Then 'this is a field
containing data 'create a new cell in the
heading row of the table objTableHead.insert
Cell 'get a reference to it, i.e. the last
one in the cells collection Set
objTableCell objTableHead.cells(objTableHead.cel
ls.length - 1) objTableCell.innerHTML
"ltBgt" strFieldName "lt/Bgt" 'create a
new cell in the body row of the table
objTableRow.insertCell 'get a reference to
it, i.e. the last one in the cells collection
Set objTableCell objTableRow.cells(objTableRow
.cells.length - 1) 'create a SPAN element
with appropriate DATAFLD attribute
strNewSpanElem "ltSPAN DATAFLD" QUOT
strFieldName _ QUOT
"gtlt/SPANgt" 'insert the SPAN element into
the cell objTableCell.innerHTML
strNewSpanElem End If Next ...
42
adopersist 4
2 main topics
XML Data file
Using ADO Persisted XML Data
... 'add the bindings to the table elements
document.all("tblData").dataFld"zrow"
document.all("tblData").dataSrc"dsoData"
document.all("tblSub").dataFld"rsdata"
document.all("tblSub").dataSrc"dsoData"
document.all("tblMain").dataSrc"dsoData End
Sub lt/SCRIPTgt
43
Batch Updating XML
Creating XML From a Data Store
Batch Updating XML Document Files
ADO can write (persist) the XML to disk as a
file. The same can be done in ASP (using the
FileSystemObject) or in your custom components
(using whatever native file-handling code they
implement).   Create the XML file weekly, daily
or on the hour depending on how often it changes,
and the importance of having the most up-to-date
content.
... 'create new file, overwriting any existing
one Set objFSO CreateObject("Scripting.FileSyste
mObject") Set objFile objFSO.CreateTextFile("pat
h_and_filename", True) QUOT Chr(34) 'double
quote character objFile.WriteLine "lt?xml
version" QUOT "1.0" QUOT
"?gt" objFile.WriteLine "ltBOOKLISTgt" ... ...
iterate through records writing the data as XML
to the file ... objFile.WriteLine
"lt/BOOKLISTgt" objFile.Close ...
44
Updating the Source
Updating the Source Data
OK, so we can create an XML file dynamically. But
what about when we want to edit the data - and
update the original source?
  • With ASP Script and MSXML
  • With a Custom Component
  • With ADO Persistence Methods

45
Getting data to the server
Updating the Source Data
Getting an XML Document back to the Server
But first, we have to get the XML document
containing the update information from the client
back to the server.
The easiest way is to post it back from a ltFORMgt
on the page, and then access it in our ASP page
from the Request.Form collection.
... ltFORM NAME"frmUpdate" ACTION"update_source.a
sp" METHOD"POST"gt ltINPUT TYPE"HIDDEN"
NAME"hidXML"gt lt/FORMgt ... ... ltSCRIPT
LANGUAGE"JScript"gt ... document.all("hidXML").val
ue strUpdates 'string holding the
XML document.all("frmUpdate").submit
'submit form to server ... lt/SCRIPTgt
46
Loading into MSXML
Updating the Source Data
Using the MSXML Parser in ASP on the Server
We can write the XML string sent from the browser
to the server's disk using the FileSystemObject
in ASP then open it into the component with the
load method   'create an instance of the MS XML
Parser and load an XML file Set objXML
Server.CreateObject("microsoft.XMLDOM") objXML.loa
d "path_and_filename" ...
Alternatively we can feed the XML string direct
to the component with the loadXML
method  'create an instance of the MS XML
Parser and load an XML string Set objXML
Server.CreateObject("microsoft.XMLDOM") objXML.loa
dXML Request.Form("txtXML") ...
47
Displaying load errors
Updating the Source Data
Using the MSXML Parser in ASP on the Server
... 'see if it loaded OK, i.e. is a well-formed
XML file If objXML.parseError.errorCode ltgt 0
Then 'error loading document so display error
details strError "ltBgtInvalid XML file
!lt/BgtltBRgt" _ "File URL "
objXML.parseError.url "ltBRgt" _
"Line No. " objXML.parseError.line "ltBRgt" _
"Character " objXML.parseError.line
pos "ltBRgt" _ "File Position "
objXML.parseError.filepos "ltBRgt" _
"Source Text " objXML.parseError.srcText
"ltBRgt" _ "Error Code "
objXML.parseError.errorCode "ltBRgt" _
"Description " objXML.parseError.reason
Response.Write strError "lt/BODYgtlt/HTMLgt"
Response.End End If ...
48
Simple update document
Updating the Source Data
Using the MSXML Parser in ASP on the Server
lt?xml version"1.0"?gt ltBOOKLIST_UPDATEgt ltITEMgt
lt!-- original book code number used to locate
correct record --gt ltORIGINAL_CODEgt16-024lt/ORIG
INAL_CODEgt lt!-- new values for this record
--gt ltCODEgt16-041lt/CODEgt
ltCATEGORYgtHTMLlt/CATEGORYgt ltRELEASE_DATEgt1998-0
3-07lt/RELEASE_DATEgt ltTITLEgtInstant HTML
lt/TITLEgt lt/ITEMgt ltITEMgt ... etc ...
lt/ITEMgt lt/BOOKLIST_UPDATE gt 
49
MSXML updating data
2 main topics
XML Data file
... Set objCon Server.CreateObject("ADODB.Connec
tion") objCon.Open "connection_string" 'open
data source ready to update it   'get reference
to BOOKLIST_UPDATE root element of XML
document Set objRoot objXML.documentElement   'g
et a reference to the collection of ITEM child
nodes Set colItems objRoot.childNodes   'loop
through each ITEM node collecting child node
values For Each objItem in colItems Set
objNode objItem.selectSingleNode("ORIGINAL_CODE"
) strOriginalCode objNode.text Set
objNode objItem.selectSingleNode("CODE")
strNewBookCode objNode.text Set objNode
objItem.selectSingleNode("CATEGORY")
strNewCategory objNode.text Set objNode
objItem.selectSingleNode("RELEASE_DATE")
strNewRelDate objNode.text Set objNode
objItem.selectSingleNode("TITLE") strNewTitle
objNode.text   'build a SQL string to update
the database strSQL "UPDATE booklist SET
kBookCode'" strNewBookCode "', " _
"tCategory'" strNewCategory "', " _
"tRelDate'" strNewRelDate "', tTitle'"
strNewTitle "' " _ "WHERE
kBookCode'" strOriginalCode "'"  
objCon.Execute strSQL 'execute SQL to update the
data source Next ...
50
ASP2XML data file
Creating XML From a Data Store
Storing Update Information in the XML Document
... ltXML ID"dsoBookList"gt lt?xml
version"1.0"?gt ltBookListgt ltA2X_Recordgt
ltA2X_RecordNumbergt00001lt/A2X_RecordNumbergt
ltA2X_UpdateActiongtNONElt/A2X_UpdateActiongt
ltA2X_RecordMatchgtkBookCode'1274'lt/A2X_RecordMatch
gt ltkBookCodegt1274lt/kBookCodegt
ltdReleaseDategt1998-01-12 000000lt/dReleaseDategt
lttTitlegtInstant JavaScriptlt/tTitlegt
lttDescriptiongtLearn JavaScript with this
booklt/tDescriptiongt lt/A2X_Recordgt ltA2X_Recordgt
...etc... lt/A2X_Recordgt lt/BookListgt lt/XMLgt ...
51
With a custom component
2 main topics
ltSCRIPT LANGUAGE"JScript"gt function
fnSaveChanges()   // get a reference to the
recordset in the data island var objRS
dsoBookList.recordset var strUpdates
'ltBookListgt\n' // string to hold updates
XML   // loop through records collecting any
with UpdateAction // not set to NONE, and add
to the string in strUpdates while (! objRS.EOF)
if (objRS('A2X_UpdateAction') ! 'NONE')
strUpdates ' ltA2X_Recordgt\n' '
ltA2X_RecordNumbergt' objRS('A2X_RecordNumber')
'lt/A2X_RecordNumbergt\n' '
ltA2X_UpdateActiongt' objRS('A2X_UpdateAction')
'lt/A2X_UpdateActiongt\n' '
ltA2X_RecordMatchgt' objRS('A2X_RecordMatch')
'lt/A2X_RecordMatchgt\n' ' ltkBookCode
A2X_DataType"TEXT"gt' objRS('kBookCode')
'lt/kBookCodegt\n' ' ltdReleaseDate
A2X_DataType"DATE"gt' objRS('dReleaseDate')
'lt/dReleaseDategt\n' ' lttTitle
A2X_DataType"TEXT"gt' objRS('tTitle')
'lt/tTitlegt\n' ' lttDescription
A2X_DataType"TEXT"gt' objRS('tDescription')
'lt/tDescriptiongt\n' '
lt/A2X_Recordgt\n' objRS.MoveNext()
strUpdates 'lt/BookListgt\n'   //put
updates string into hidden control on form
document.all('hidXML').value strUpdates
alert('The XML updates document that will be sent
to server is\n\n' strUpdates)   // submit
form to the server where another ASP page can
update data source document.all('frmUpdate').sub
mit() lt/SCRIPTgt
XML Data file
52
ASP2XML updating
Updating the Source Data
Updating the Data Source from the XML Document
'create component instance and set connection
string Set objA2X Server.CreateObject("Stonebroom
.ASP2XML") strConnect
"connection_string" strXML Request.Form("txtXML"
) 'XML containing updates 'execute the putXML
method lngNumUpdates objA2X.putXML(strConnect,
strXML) Set objA2X Nothing 'finished with
the component   'return value is number of
updated records 'or -1 if an error occurred If
lngNumUpdates -1 Then Response.Write
"Error, failed to update records." Else
Response.Write "Updated " lngNumUpdates "
records." End If
53
Concurrent updates
Updating the Source Data
Managing Concurrent Updates
or - what happens when someone else is editing
the same data?
lt?xml version"1.0"?gt ltbooklistgt ltA2X_Recordgt
ltA2X_RecordNumbergt00001lt/A2X_RecordNumbergt
ltA2X_UpdateActiongtNONElt/A2X_UpdateActiongt
ltA2X_RecordMatchgtkBookCode'1274'lt/A2X_RecordMatch
gt ltkBookCodegt1274lt/kBookCodegt
ltdReleaseDategt1998-01-12 000000lt/dReleaseDategt
ltA2X_Original_dReleaseDategt1998-01-12
000000lt/A2X_Original_dReleaseDategt
lttTitlegtInstant JavaScriptlt/tTitlegt
ltA2X_Original_tTitlegtInstant JavaScriptlt/A2X_Origi
nal_tTitlegt lttDescriptiongtLearn JavaScript next
week instead.lt/tDescriptiongt ltA2X_Original_tDesc
riptiongtLearn JavaScript now!lt/A2X_Original_tDescr
iptiongt lt/A2X_Recordgt ... etc ... lt/booklistgt
54
With ADO Persistence
Updating the Source Data
Updating with ADO Persisted XML Data
ADO 2.5 provides an automated way to tackle
source data updates. It works much like Remote
Data Services. We get the contents of an XML
document into a normal ADO recordset with the
Open method of the Recordset object. We specify
the special 'persistence' OLE-DB provider and the
'format' constant adCmdFile   objRS.Open
"path_and_filename", "ProviderMSPersist", , ,
adCmdFile
So an XML document can be loaded into a
disconnected ADO recordset. We can then update
the original data source just like with RDS. We
call the UpdateBatch method   objRS.UpdateBatch  
For this to work we have first open a connection
to the data source and assign this to the
recordset's ActiveConnection property
55
ADO Persistence code
Updating the Source Data
Updating with ADO Persisted XML Data
Set objCon Server.CreateObject("ADODB.Connection
") Set objRS Server.CreateObject("ADODB.Recordse
t") objCon.Open "connection_string" 'open
connection to data source objRS.Open
"path_and_filename" 'load XML data into
recordset objRS.ActiveConnection objCon 'link
recordset to data source objRS.UpdateBatch
'and update source records objRS.Close obj
Con.Close
This code loads an XML document file stored on
the server's disk into the recordset. We have to
have already sent the XML document back from the
client to the server, and stored it on disk as a
file. We could POST it from a form on the
client, collect it from the ASP Request object as
a string, and use the FileSystemObject to write
it to disk as a file.
OR What about a file-upload component, or even
DAV ?
56
HTTP uploads
Updating the Source Data
It's New and it's Exciting - HTTP Posting in
Script
A new XMLHTTP component is installed with
IE5 ltSCRIPT LANGUAGE"JScript"gt function
postXML() // create an instance of the
XMLHTTP component var xmlhttp new
ActiveXObject("Microsoft.XMLHTTP") // open a
connection to the server ready to POST the data
xmlhttp.open("POST", "http//yourserver.com/put_xm
l.asp", false) // send the complete XML
document by referencing the root element
xmlhttp.send(dsoBookList.XMLDocument) //
display any response from the server in the page
document.write(xmlhttp.ResponseText) lt/SCRIPTgt
57
ADO open stream
Updating the Source Data
Streaming Data into an ADO Recordset
The Open method of an ADO recordset can read
data direct from a stream object that represents
a recordset.
We can read data directly from the browser into a
recordset object on the server ready to use the
UpdateBatch method. lt If Request.ServerVariables
("REQUEST_METHOD") "POST" Then Set objCon
Server.CreateObject("ADODB.Connection") Set
objRS Server.CreateObject("ADODB.Recordset")
objCon.Open "connection_string" objRS.Open
Request, , , , adCmdFile 'read direct from
Request objRS.ActiveConnection objCon
objRS.UpdateBatch objRS.Close
objCon.Close End If gt 
58
2 main topics
Summary
  • What is XML Data Binding?
  • Never mind XML, what is Data-Binding?
  • Data-Management Techniques on the Web
  • Data-Binding in Internet Explorer 4 and 5
  • Displaying Data using Data-Binding
  • Accessing XML Data in a DSO with Script Code
  • XML Documents and ASP
  • Creating XML documents from a data store
  • Directly with ASP Script Code
  • With a Custom Component
  • With ADO Persistence Methods
  • Updating source data from an XML document
  • With ASP Script and MSXML
  • With a Custom Component
  • With ADO Persistence Methods

59
IE5 XML Samples
2 main topics
XML Data file
http//webdev.wrox.co.uk/books/1576/
60
Thank you
2 main topics
XML Data file
2 main toics
Thank You For Listening
Resources
The Wrox Web-Developer site http//webdev.wrox.co.
uk/ The Sample XML Database Access
Component http//webdev.wrox.co.uk/books/2882/xmlc
ase.zip IE5 XML Data Binding Samples http//webde
v.wrox.co.uk/books/1576/ Microsoft Java DSO Home
Page http//www.microsoft.com/java/ Wrox Press
Home Page http//www.microsoft.com/java/
Write a Comment
User Comments (0)
About PowerShow.com