Using XML in ASP Applications - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Using XML in ASP Applications

Description:

Tree view of an XML document. Methods for the ... Manipulate through DHTML methods. var root = Query.documentElement; ... Can also do on client via DHTML ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 34
Provided by: stephe226
Category:
Tags: asp | xml | applications | dhtml | using

less

Transcript and Presenter's Notes

Title: Using XML in ASP Applications


1
Using XML in ASP Applications
  • smohr_at_answerthink.com

2
How is XML Used in ASP?
  • Divorce Data from Presentation
  • Platform Neutral Data
  • Enable Automated Data Consumers

3
Browser Compatibility
  • IE5 Default Style Sheet
  • Ships with MSXML
  • Mozilla is coming (NGLayout engine)

4
Configuring IIS for XML
  • Set the MIME type .xml -gt text/xml
  • Use Response.ContentTypetext/xml

5
Parsers
  • Dont reinvent the wheel
  • Do use a standard model
  • W3C Document Object Model
  • Simple API for XML
  • Component Software

6
W3C DOM
  • Tree view of an XML document
  • Methods for the creation and modification of XML
    documents
  • Document, Node, Element, Collections
  • Two Levels
  • Level I is a Recommendation
  • Level II is a draft containing extensions

7
SAX
  • Ad hoc standard
  • Event based parser
  • Application must respond to events -- no tree
  • Useful for very large documents or when
    interested only in a small subset of a document

8
MSXML
  • COM component that ships with IE 5
  • Implements W3C DOM, preliminary XSL, some
    Microsoft extensions
  • Also available in redistributable version
    (requires some DLLs from IE4 or later)

9
Other Parsers
  • IBM XML4J (Java) and XML4C (C)
  • W3C DOM
  • SAX
  • DataChannel XJParser
  • Evolved from Microsoft partnership
  • W3C DOM and SAX

10
The Agenda Application
11
Document Building Strings
  • For small amounts of simple XML
  • sXML ltAGENDA_QUERYgtltVENUEgt
    Venue.children(Venue.selectedIndex).text
  • sXML lt/VENUEgtltTRACKgt Track.children(Track.s
    electedIndex).text
  • sXML lt/TRACKgtltDAYgt Day.children(Day.selecte
    dIndex).text
  • sXML lt/DAYgtlt/AGENDA_QUERYgt

12
Document Building Data Islands
  • For repeated tasks from a standard template
  • Also used for data binding
  • Add the template to the body
  • ltXML id"Query"gt
  • ltAGENDA_QUERYgt
  • ltVENUE/gt
  • ltTRACK/gt
  • ltDAY/gt
  • lt/AGENDA_QUERYgt
  • lt/XMLgt

13
Data Islands (continued)
  • Manipulate through DHTML methods
  • var root Query.documentElement
  • root.childNodes(0).text Venue.children(Venue.sel
    ectedIndex).text
  • root.childNodes(1).text Track.children(Track.sel
    ectedIndex).text
  • root.childNodes(2).text Day.children(Day.selecte
    dIndex).text

14
The childNodes Collection
  • Property of Node
  • NodeList object
  • Collection of node objects (except attributes)
  • Immediate children of the parent
  • 0 to length indices

15
Sending the Document to the Server
  • GET is simple
  • Built into MSXML
  • Length restrictions
  • POST
  • Needs extra support from IXMLHTTP
  • No length restrictions

16
GET MSXML
  • Go to a new page
  • window.navigate("http//.../AgendaGet.asp?QueryDo
    c" Query.XMLDocument.xml)
  • Load a new document into the parser
  • parser.load(http//.../AgendaGet.asp?QueryDoc
    Query.XMLDocument.xml)

17
POST IXMLHTTP
  • var PostObject new ActiveXObject("Microsoft.XMLH
    TTP")
  • PostObject.Open("POST", "http//localhost/XMLASP/A
    gendaPost.asp", false)
  • PostObject.send(Query.XMLDocument)

18
Getting the XML String
  • Use the QueryString collection
  • sReqString Request.QueryString("QueryDoc")
  • parser.loadXML(sReqString)
  • Best for cases where you might not always get XML

19
Getting the XML DOM
  • Request is an XML document
  • Uses IStream support in MSXML
  • parser.load(Request)

20
Walking the Tree
  • Standard tree walking
  • Depth first (recursion) or breadth first
  • function RecurseTree(node)
  • for (var i 0 i lt node.childNodes.length
    i)
  • RecurseTree(node.childNodes(i))

21
Node Features
  • Properties
  • nodeName, nodeType
  • nodeValue, text, xml
  • Collections
  • childNodes, attributes

22
Node Features (continued)
  • Methods
  • appendChild, insertBefore, removeChild,
    replaceChild
  • cloneNode

23
Document Building Objects
  • XMLDOMDocument
  • createElement, createAttribute
  • documentElement
  • XMLDOMNode
  • text
  • appendChild, setAttribute

24
Returning the Results
  • Set the content type
  • Use text manipulation or XML DOM to build a reply
  • Response.Write(some_xml)

25
Building a Reply using the DOM
  • cloneNode to quickly generate similar records
  • Build a subtree, set the PCDATA, clone it
  • Append the results to the parent

26
Building a Reply (continued)
  • replyDoc.documentElement replyDoc.createElement(
    "AGENDA")
  • sessionSubTree replyDoc.createElement("SESSION")
  • sessionSubTree.appendChild(replyDoc.createElement(
    "TOPIC"))
  • . . .
  • sessionSubTree.childNodes(0).text "What Forest?
    I Only See (DOM) Trees."
  • . . .
  • replyDoc.documentElement.appendChild(sessionSubTre
    e.cloneNode(true))

27
Visual Presentation
  • Decide when it is going to be viewed instead of
    consumed
  • Close to client vs. browser neutrality
  • HTML -- flexible, but limits utility and reuse of
    the ASP
  • CSS -- easy, but browser issues
  • XSL -- powerful! Not standard

28
Server-generated HTML
  • Walk the DOM tree, translate into appropriate
    (D)HTML
  • Can also do on client via DHTML
  • Limits you to human clients -- separation of data
    and presentation is lost

29
CSS
  • In IE5, assign an CSS to the document
  • lt?xml version"1.0"?gt
  • lt?xml-stylesheet type"text/css"
    href"mstalk1.css" ?gt
  • ltAGENDAgtltTOPICgt. . .
  • CSS
  • TOPIC
  • COLOR black
  • display block
  • FONT-FAMILY Verdana
  • . . .

30
XSL
  • W3C Draft for a presentation language
  • Transformation
  • Reorganize content
  • Pattern-matching and query language
  • Can specify context and conditions
  • Styling
  • Like CSS, but expressed in XML

31
XSL Example
  • lt?xml version"1.0" ?gt
  • ltHTML xmlnsxsl"http//www.w3.org/TR/WD-xsl"gt
  • ltBODYgt
  • ltxslfor-each selectSESSION"gt
  • ltDIVgt
  • ltDIV STYLE"font-familyVerdana
  • font-size14pt alignleft"gt
  • ltxslvalue-of selectTOPIC" /gt
  • lt/DIVgt
  • . . .

32
Summary
  • ASP and XML are a powerful match
  • MSXML meets common ASP requirements
  • XML makes a platform neutral interface
  • Explicitly separate data from presentation
  • Remember automated consumers!

33
Further Resources
  • XML Applications (Wrox)
  • XML Design and Implementation (Wrox)
  • MSXML IXMLHTTP reference -- http//msdn.microsof
    t.com/xml/reference/scriptref/XMLDOM_Objects.asp
  • ASP XML Tutorials -- http//msdn.microsoft.com/x
    ml/tutorial/default.asp
Write a Comment
User Comments (0)
About PowerShow.com