1 / 48

WORKING WITH THE DOCUMENT OBJECT MODEL

TUTORIAL 10. WORKING WITH THE DOCUMENT OBJECT MODEL. THE W3C DOM. To modify an XML or XSLT document, you need to be able to access the document and its contents.

karena
Download Presentation

WORKING WITH THE DOCUMENT OBJECT MODEL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TUTORIAL 10 WORKING WITH THE DOCUMENT OBJECT MODEL New Perspectives on XML, 2nd Edition Tutorial 10

  2. THE W3C DOM • To modify an XML or XSLT document, you need to be able to access the document and its contents. • This is done through a Document Object Model or DOM, which is a systematic framework for working with the content and structure of a document. New Perspectives on XML, 2nd Edition Tutorial 10

  3. DOM LEVELS New Perspectives on XML, 2nd Edition Tutorial 10

  4. 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 var IE = window.ActiveXObject ? true:false; var MOZ = document.implementation.createDocument ? true:false; New Perspectives on XML, 2nd Edition Tutorial 10

  5. CREATING A CROSS-BROWSER SOLUTION if (IE) { Internet Explorer code } else if (MOZ) { Mozilla code } New Perspectives on XML, 2nd Edition Tutorial 10

  6. 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); Where docObj is the variable name of the document object and PID is the program ID that indicates the type of document object to be created. New Perspectives on XML, 2nd Edition Tutorial 10

  7. CREATING A DOCUMENT OBJECT IN MOZILLA docObj = document.implementation.createDocument(uri,root,doctype); Where uri is the URI of the document’s namespace, root is the qualified name of the document’s root element, and doctype is the type of document to create New Perspectives on XML, 2nd Edition Tutorial 10

  8. LOADING A FILE INTO A DOCUMENT OBJECT • 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. docObj.async=false; Where docObj is the document object into which you want to load the file New Perspectives on XML, 2nd Edition Tutorial 10

  9. LOADING A STYLE SHEET OBJECT • Because XSLT style sheets are also XML documents, you need to create a document object using ActiveX • Rental-threaded model • Free-threaded model var FreeThreadPID = ["Msxml2.FreeThreadedDOMDocument.5.0", "Msxml2.FreeThreadedDOMDocument.4.0", "Msxml2.FreeThreadedDOMDocument.3.0"]; New Perspectives on XML, 2nd Edition Tutorial 10

  10. TRANSFORMING A DOCUMENT • The transformNode() method creates a text string containing the code of the result document and has the following syntax: docObj.transformNode(styleObj) resultStr = XMLdoc.transformNode(XSLTdoc); docObj.transformNodeToObject(styleObj, resultObj) New Perspectives on XML, 2nd Edition Tutorial 10

  11. CREATING A TEMPLATE OBJECT • For a large and complicated style sheet or for programs that need to run several transformations, Microsoft suggests storing the compiled style sheet in a template object, which increases the efficiency of the program because the cached style sheet can be accessed repeatedly without being recompiled New Perspectives on XML, 2nd Edition Tutorial 10

  12. CREATING A TEMPLATE OBJECT New Perspectives on XML, 2nd Edition Tutorial 10

  13. CREATING A PROCESSOR OBJECT • To create and use a processor object, you follow four steps: • Insert a free-threaded style sheet into the template object. • Create an XLST processor based on the template. • Specify an input source document for the processor. • Transform the source document based on the style sheet. New Perspectives on XML, 2nd Edition Tutorial 10

  14. CREATING A PROCESSOR OBJECT New Perspectives on XML, 2nd Edition Tutorial 10

  15. RESULTS OF THE TRANSFORMATION New Perspectives on XML, 2nd Edition Tutorial 10

  16. TRANSFORMING A DOCUMENT WITH MOZILLA New Perspectives on XML, 2nd Edition Tutorial 10

  17. TRANSFORMING A DOCUMENT WITH MOZILLA New Perspectives on XML, 2nd Edition Tutorial 10

  18. CONVERTING A DOCUMENT OBJECT OR FRAGMENT TO A TEXT STRING • 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

  19. CONVERTING A DOCUMENT OBJECT OR FRAGMENT TO A TEXT STRING New Perspectives on XML, 2nd Edition Tutorial 10

  20. WORKING WITH THE DOCUMENT OBJECT New Perspectives on XML, 2nd Edition Tutorial 10

  21. WORKING WITH THE DOCUMENT OBJECT New Perspectives on XML, 2nd Edition Tutorial 10

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

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

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

  25. USING FAMILIAL RELATIONS New Perspectives on XML, 2nd Edition Tutorial 10

  26. NODE TYPES, NAMES, AND VALUES • nodeObj.nodeType • nodeObj.nodeName • nodeObj.nodeValue New Perspectives on XML, 2nd Edition Tutorial 10

  27. ADDING AND REMOVING NODES nodeObj = docObj.createElement(tag); Where nodeObj is the new element node, docObj is the document object containing the new node, and tag is the tag name associated with the element New Perspectives on XML, 2nd Edition Tutorial 10

  28. CREATING NODE OBJECTS New Perspectives on XML, 2nd Edition Tutorial 10

  29. INSERTING AND REMOVING NODES New Perspectives on XML, 2nd Edition Tutorial 10

  30. CREATING A DOCUMENT FRAGMENT New Perspectives on XML, 2nd Edition Tutorial 10

  31. CLONING A DOCUMENT FRAGMENT New Perspectives on XML, 2nd Edition Tutorial 10

  32. WORKING WITH ATTRIBUTE NODES • sortList() function function sortList() { if (IE) { XSLTdoc=new ActiveXObject(getPID(FreeThreadPID)); loadDoc(XSLTdoc, "clist.xsl"); } } New Perspectives on XML, 2nd Edition Tutorial 10

  33. ATTRIBUTE METHODS New Perspectives on XML, 2nd Edition Tutorial 10

  34. FILTERING THE SOURCE DOCUMENT <xsl:param name="group" select="//person" /> <xsl:param name="group" select="//person[amount > 100]" /> New Perspectives on XML, 2nd Edition Tutorial 10

  35. SETTING A PARAMETER VALUE processorObj.addParameter(parameter, value, uri) Where processorObj is a processor object, parameter is the name of the style sheet parameter, value is the value passed to the parameter, and uri is an optional value that specifies the namespace URI for the parameter New Perspectives on XML, 2nd Edition Tutorial 10

  36. SELECTING A NODE SET 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

  37. DEBUGGING TECHNIQUES • When you write scripts that load and modify XML documents, you occasionally run into documents that fail to load. • You can also retrieve error information from the XML processor. • MSXML places information about loading errors into a parseError object. • You can extract information from this object to learn the reasons why MSXML failed to load a document. New Perspectives on XML, 2nd Edition Tutorial 10

  38. MSXML PARSEERROR OBJECT New Perspectives on XML, 2nd Edition Tutorial 10

  39. MSXML PARSEERROR OBJECT New Perspectives on XML, 2nd Edition Tutorial 10

  40. ERROR HANDLING WITH MOZILLA • If a Mozilla-based browser fails to load a document, the browser loads a different XML document containing information about the error. New Perspectives on XML, 2nd Edition Tutorial 10

  41. 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 New Perspectives on XML, 2nd Edition Tutorial 10

  42. 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 as discussed by Garrett, a user interacts with a Web server through a Web page running on their browser. New Perspectives on XML, 2nd Edition Tutorial 10

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

  44. WORKING WITH AJAX New Perspectives on XML, 2nd Edition Tutorial 10

  45. WORKING WITH AJAX New Perspectives on XML, 2nd Edition Tutorial 10

  46. THE XMLHttpRequest OBJECT • 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(); New Perspectives on XML, 2nd Edition Tutorial 10

  47. METHODS OF THE XMLHttpRequest OBJECT New Perspectives on XML, 2nd Edition Tutorial 10

  48. PROPERTIES OF THE XMLHttpRequest OBJECT New Perspectives on XML, 2nd Edition Tutorial 10

More Related