510 likes | 614 Views
TUTORIAL 10. WORKING WITH THE DOCUMENT OBJECT MODEL. Updating XML documents. XML API. XML and XSLT are a markup languages Describe how things are arranged and how things look API Collection of prewritten procedures Enables processing of XML Two APIs for use with XML
E N D
TUTORIAL 10 WORKING WITH THE DOCUMENT OBJECT MODEL New Perspectives on XML, 2nd Edition Tutorial 10
Updating XML documents New Perspectives on XML, 2nd Edition Tutorial 10
XML API • XML and XSLT are a markup languages • Describe how things are arranged and how things look • API • Collection of prewritten procedures • Enables processing of XML • Two APIs for use with XML • W3C DOM (Document Object Model) • Requires entire XML document be held in memory; memory intensive • SAX (Simple API for XML) • Can be used on a stream of XML which need not be held entirely in memory • Developed by XML-DEV mailing list New Perspectives on XML, 2nd Edition Tutorial 10
DOM LEVELS Page 572 New Perspectives on XML, 2nd Edition Tutorial 10
General approach • Determine which browser is being used • Create document object • Load a file into the document object • Transform the document object using XSLT • Display the transformed document New Perspectives on XML, 2nd Edition Tutorial 10
CREATING A CROSS-BROWSER SOLUTION • Because there are some fundamental differences between Internet Explorer and the Mozilla-based browsers in implementing the Document Object Model, any program code that you write has to first determine which browser is in use. • Object-detection <script type=“text/javascript”> var IE = window.ActiveXObject ? true:false; var MOZ = document.implementation.createDocument ? true:false; </script> Java Script is case sensitive New Perspectives on XML, 2nd Edition Tutorial 10
CREATING A CROSS-BROWSER SOLUTION if (IE) { Internet Explorer code } else if (MOZ) { Mozilla code } New Perspectives on XML, 2nd Edition Tutorial 10
General approach • Determine which browser is being used • Create document object • Load a file into the document object • Transform the document object using XSLT • Display the transformed document New Perspectives on XML, 2nd Edition Tutorial 10
CREATING A DOCUMENT OBJECT IN INTERNET EXPLORER • A document object is an object that can store the contents and structure of a document. docObj = new ActiveXObject(PID); • docObj is the variable name of the document object • PID is the program ID that indicates the type of document object to be created. Each version of MSXML supports a different program ID New Perspectives on XML, 2nd Edition Tutorial 10
CREATING A DOCUMENT OBJECT IN INTERNET EXPLORER • IE creates document objects using ActiveX • Microsoft technology used to create interactive content for the web • Program IDs supported by different versions of MSXMLMsxml2.DOMDocument.5.0 Msxml2.DOMDocument.4.0 Msxml2.DOMDocument.3.0 MSXML2.DOMDocument Microsoft.XMLDOM • Example: XMLdoc = new ActiveXobject (“Msxml2.DOMDocument.3.0”); To determine the most recent version, query the browser (page 576) New Perspectives on XML, 2nd Edition Tutorial 10
CREATING A DOCUMENT OBJECT IN MOZILLA • Syntax docObj = document.implementation.createDocument (uri,root,doctype); uri is the URI of the document’s namespace root is the qualified name of the document’s root element doctype is the type of document to create (always enter a value of null) • Example XMLdoc = document.implementaton.createDocument (“http://lhouse.org”, Persons, null); New Perspectives on XML, 2nd Edition Tutorial 10
General approach • Determine which browser is being used • Create document object • Load a file into the document object • Transform the document object using XSLT • Display the transformed document New Perspectives on XML, 2nd Edition Tutorial 10
LOADING A FILE INTO A DOCUMENT OBJECT Default • First choose load strategy: • An asynchronous load does not require the application loading the file to wait for it to finish loading before proceeding through the lines in the program code • A synchronous load causes the application to stop until the file is completely loaded. • Example: choose synchronous load docObj.async=false; • Example load: docObj.load(url) Use for large files or retrieving files through a slow internet connection New Perspectives on XML, 2nd Edition Tutorial 10
General approach • Determine which browser is being used • Create document object • Load a file into the document object • Transform the document object using XSLT • Display the transformed document New Perspectives on XML, 2nd Edition Tutorial 10
Transforming a Document Using XSLT(Internet Explorer) • Because XSLT style sheets are also XML documents, you need to create a document object using ActiveX for the style sheet • Rental-threaded model • Content is accessed by the XML processor using a single sequence of instructions • Free-threaded model • Content is access by the processor through multiple input threads Preferable for efficiency reasons New Perspectives on XML, 2nd Edition Tutorial 10
Transforming a Document Using XSLT(Internet Explorer) • Once a style sheet is loaded, IE applies the style sheet to the source document using either of two methods: • transformNode() • Creates a text string containing the code for the result document • transformNotetoObject() • Stores the result lin another document object • Either method can be drag on resources • Create a template Object and Processor Object… Simple Can write code to further manipulate the contents New Perspectives on XML, 2nd Edition Tutorial 10
Template and Processor Objects (Internet Explorer) • Template object • Uses a large and complicated style sheet or for programs that need to run several transformations, • Increases the efficiency of the program because the cached style sheet can be accessed repeatedly without being recompiled • Processor object • Processes the template object to produce the result object • Display the result object New Perspectives on XML, 2nd Edition Tutorial 10
Creating a Template Object (Internet Explorer) • Processor must compile the style sheet object each time the transformation is run. • For large and complicated style sheets or programs that need to run several transformations, store the style sheet in a template object and use the free-threaded model • Store the program IDs in an array: Var FreeThreadPID = [“Msxm1.FreeThreadedDocument.5.0”, “Msxm1.FreeThreadedDocument.4.0”, “Msxm1.FreeThreadedDocument.3.0”]; • Create document object for the style sheet XSLTdoc=New Active Xobject (getPID (FreeThreadPID) ); New Perspectives on XML, 2nd Edition Tutorial 10
Using a processor object (Internet Explorer) • Steps for creating and using a processor object • Insert a free-threaded style sheet into the template object • Create an XSLT processor based on the template • Specify an input source document for the processor • Transform the source document based on the style sheet Example function doTransform() { varcontTable = document.getElementById(“ctabale”)” If (IE) { XSLTemp.stylesheet=XSLTdoc: XSLTProc=XSLTemp.createprocessor(); XSLTProc.input=XMLdoc; XSLTLProc.transform()XSLTproc.out; } } See pages 584-587 for details New Perspectives on XML, 2nd Edition Tutorial 10
TRANSFORMING A DOCUMENT WITH MOZILLA New Perspectives on XML, 2nd Edition Tutorial 10
TRANSFORMING A DOCUMENT WITH MOZILLA Figure 10-17 page 589 New Perspectives on XML, 2nd Edition Tutorial 10
CONVERTING A DOCUMENT OBJECT OR FRAGMENT TO A TEXT STRING WITH MOZILLA • To convert a document object or a fragment to a text string in Mozilla, you create a serializer object, which contains a textual representation of the contents of a document object or fragment serialObj = new XMLSerializer(); Where serialObj is the serializer object that will contain the text of the document object or fragment. New Perspectives on XML, 2nd Edition Tutorial 10
CONVERTING A DOCUMENT OBJECT OR FRAGMENT TO A TEXT STRING WITH MOZILLA Figure 10-18 Page 591 New Perspectives on XML, 2nd Edition Tutorial 10
General approach • Determine which browser is being used • Create document object • Load a file into the document object • Transform the document object using XSLT • Display the transformed document New Perspectives on XML, 2nd Edition Tutorial 10
Display the document: New Perspectives on XML, 2nd Edition Tutorial 10
WORKING WITH THE DOCUMENT OBJECT Page 592 New Perspectives on XML, 2nd Edition Tutorial 10
WORKING WITH THE DOCUMENT OBJECT Page 593 New Perspectives on XML, 2nd Edition Tutorial 10
VIEWING THE NODE TREE • A node that contains other nodes is a parent node, and the • nodes it contains are child nodes. • Nodes that share the same parent are sibling nodes. • Nodes can contain different types of content. For example, element nodes refer to elements from the Document Object Model, and text nodes refer to the actual text content of element nodes. • Attribute nodes refer to the attributes contained within elements or processing instructions. New Perspectives on XML, 2nd Edition Tutorial 10
ACCESSING ELEMENTS BY TAG NAME docObj.getElementsByTagName(tag) Where docObj is the document object and tag is the element’s tag name. Example:XMLdoc.getElementsByTagName("person") New Perspectives on XML, 2nd Edition Tutorial 10
USING FAMILIAL RELATIONS • Each node in a node tree can also be treated as a node object with its own collection of properties and methods nodeObj.firstChild Where nodeObj is a node from the document’s node tree. New Perspectives on XML, 2nd Edition Tutorial 10
USING FAMILIAL RELATIONS Page 595 New Perspectives on XML, 2nd Edition Tutorial 10
NODE TYPES, NAMES, AND VALUES • nodeObj.nodeType • nodeObj.nodeName • nodeObj.nodeValue Page 596 New Perspectives on XML, 2nd Edition Tutorial 10
ADDING AND REMOVING NODES nodeObj = docObj.createElement(tag); Where • nodeObj is the new element node • docObj is the document object containing the new node • tag is the tag name associated with the element New Perspectives on XML, 2nd Edition Tutorial 10
CREATING NODE OBJECTS Page 599 New Perspectives on XML, 2nd Edition Tutorial 10
INSERTING AND REMOVING NODES Page 600 New Perspectives on XML, 2nd Edition Tutorial 10
CREATING A DOCUMENT FRAGMENT Page 601 New Perspectives on XML, 2nd Edition Tutorial 10
CLONING A DOCUMENT FRAGMENT Page 602 New Perspectives on XML, 2nd Edition Tutorial 10
Add new Records to the Contribution List New Perspectives on XML, 2nd Edition Tutorial 10
ATTRIBUTE METHODS Page 609 New Perspectives on XML, 2nd Edition Tutorial 10
FILTERING THE SOURCE DOCUMENT <xsl:param name="group" select="//person" /> <xsl:param name="group" select="//person[amount > 100]" /> Page 614 New Perspectives on XML, 2nd Edition Tutorial 10
FILTERING THE SOURCE DOCUMENT function filterList() { var filterStr = document.webform.filter.value; if (filterStr==“”) filter=“//person” else filter = “//person[“+filterStr+”]”; } Extract filter expression from form Insert filter expression into parameter New Perspectives on XML, 2nd Edition Tutorial 10
SETTING A PARAMETER VALUEin Internet Explorer processorObj.addParameter(parameter, value, uri) • processorObj is a processor object • parameter is the name of the style sheet parameter • value is the value passed to the parameter • uri is an optional value that specifies the namespace URI for the parameter New Perspectives on XML, 2nd Edition Tutorial 10
SELECTING A NODE SETin Internet Explorer object.selectSingleNode(xpath) Where object is a document or node object and xpath is an XPath expression. object.selectNodes(xpath) New Perspectives on XML, 2nd Edition Tutorial 10
SAVING AN XML DOCUMENT docObj.save(location) Where location is one of the following: • A filename • The name of another document object • An ASP (Active Server Pages) response object • A custom COM object that supports persistence • Supported on IE on a Web server. New Perspectives on XML, 2nd Edition Tutorial 10
WORKING WITH AJAX • AJAX, or Asynchronous JavaScript and XML, refers to the use of HTML, XML, XSLT, and JavaScript to enable fast, efficient communication between applications running on a user’s browser and data stored and updated on a secure Web server. • In the classic Web application model, a user interacts with a Web server through a Web page running on their browser. New Perspectives on XML, 2nd Edition Tutorial 10
WORKING WITH AJAX • The AJAX Web application model adds an intermediary between the user and the server-side system, which is called an AJAX engine. • The AJAX engine is responsible for communicating with the server and for relaying any information from the server to the user interface. New Perspectives on XML, 2nd Edition Tutorial 10
WORKING WITH AJAX • Ajax engine: • Written in JavaScript • Ajax uses HTTP to communicate with Web server • Client uses JavaScript call functions to communicate with Ajax engine • Ajax engine placed within Web page • Enables asynchronous rather than synchronous communication New Perspectives on XML, 2nd Edition Tutorial 10
WORKING WITH AJAX Page 628 New Perspectives on XML, 2nd Edition Tutorial 10
THE XMLHttpRequest OBJECT Ajax engine • Internet Explorer • To create an XMLHttpRequest object, usereqObj = new ActiveXObject(progID); Where reqObj is the XMLHttpRequest object and progID is an ActiveX program ID for XMLHttpRequest objects. • Mozilla and Safari • To create an XMLHttpRequest object, usereqObj = new XMLHttpRequest(); Ajax engine New Perspectives on XML, 2nd Edition Tutorial 10
METHODS OF THE XMLHttpRequest OBJECT Page 630 New Perspectives on XML, 2nd Edition Tutorial 10