1 / 36

WORKING WITH THE DOCUMENT OBJECT MODEL

TUTORIAL 10A. 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.

skah
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 10A WORKING WITH THE DOCUMENT OBJECT MODEL New Perspectives on XML, 2nd Edition Tutorial 10A

  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 10A

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

  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 10A

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

  6. Practice • P. 569 • P. 570-571 • P. 574-575 New Perspectives on XML, 2nd Edition Tutorial 10A

  7. 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 10A

  8. 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 10A

  9. Practice • P. 576-577 • P. 578-579 New Perspectives on XML, 2nd Edition Tutorial 10A

  10. 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 10A

  11. 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 10A

  12. 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 10A

  13. 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 10A

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

  15. 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 10A

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

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

  18. Practice • P. 580 • P. 581-582 • P. 583-584 • P. 585 • P. 586-587 New Perspectives on XML, 2nd Edition Tutorial 10A

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

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

  21. 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 10A

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

  23. Practice • P. 588 • P. 589 • P. 591 New Perspectives on XML, 2nd Edition Tutorial 10A

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

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

  26. 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 10A

  27. 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 10A

  28. 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 10A

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

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

  31. 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 10A

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

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

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

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

  36. Practice • P. 603 • P. 604-605 • P. 605-606 New Perspectives on XML, 2nd Edition Tutorial 10A

More Related