1 / 24

The Document Object Model

The Document Object Model. Sample Chapter from XML Programming Using the Microsoft XML Parser. Introduction to The Document Object Model. The DOM provide an application programming interface (API). It is platform and language neutral.

hollis
Télécharger la présentation

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. The Document Object Model Sample Chapter from XML Programming Using the Microsoft XML Parser

  2. Introduction to The Document Object Model • The DOM provide an application programming interface (API). • It is platform and language neutral. • Many scripting and programming languages can be used to navigate the DOM • However, the platform and language is dependent on the parser used to expose the DOM. • The DOM is a tree based API that requires the entire document be loaded into memory while processing occurs.

  3. World Wide Web Consortium (W3C) Dom Specification Levels • Level 0 – Not a W3C specification. Contains functionalities equivalent to those exposed in Netscape 3 and IE 3. • Level 1 – Released in 1998. Provides support for XML 1.0 and HTML 4 • Level 2 – Released in 2000. Extends level one by supporting namespaces and CSS among others. • Level 3 – Released in 2004. Finishes namespace support. Also adds support for validation, loading and saving a document, among others. Information gathered at www.w3.org/DOM/Activity

  4. XML DOM Parsers • Software that can interpret an XML document as a DOM instance. • Typically represented as tree nodes located in RAM. (The Document Tree) • A node is a general term for an element, attribute, comment, etc. • Tree traversal is generally topdown leftright • Nodes can be accessed sequentially.

  5. MSXML • The sample chapter uses MSXML3. MSXML3 implements DOM Level 1. The latest version from Microsoft is MSXML5.You can use the following objects with MSXML3: • DocumentFragment – A part of an XML document • Document – An entire XML document • Node – Represents a single node in the document tree • NodeList – An ordered collection of nodes. Can be addressed by index (0 based) • NamedNodeMap – A collection of nodes that can be addressed by index as well as name. Typically used with attributes. • CharacterData – Handles character data • Attr – Attribute • Element – Element • Text – Text (Element/Attribute content) • Comment – comment • DOMException – Used to raise exception when something goes wrong. An example is when a non-existent node is accessed.

  6. MSXML3 Extended Interfaces • Allow you to access additional objects • CDATASection – Extends text interface so CDATA sections can be handled • DocumentType – For handling <!DOCTYPE> • Entity – For representing an entity • EntityReference – XML entities such as &quot; • Notation – Used with notations • ProcessingInstruction – Used with processing instructions • Properties, methods, and constants are used to interact with the objects in this slide as well as the previous one.

  7. An XML Document as a Tree • Each of the components in an XML document can be mapped onto a node. • Different types of nodes include element nodes, attribute nodes, comment nodes, etc. • The document node refers to the entire document

  8. Creating a DOM Tree of an XML Document in Memory Using Client Side VBScript • Step 1 – Create an instance of the Document object • Step 2 – Specify synchronization requirement • Step 3 – Load content into the DOM instance object • <Script Language="VbScript"> • <!-- • Dim docObj • Set docObj = CreateObject ("MSXML2.DOMDocument")QUOTES MISSING ON ONLINE CHAPTERMSXML2 WORKS WITH DOWNLOADING LATEST VERSION docObj.async=false • docObj.load("books.xml") • msgbox docObj.xml • --> • </Script>

  9. Creating an Instance of the DOM • DOM Level 1 doesn’t provide a way to create a document instance. This wasn’t resolved until 2004 with DOM level 3. • Microsoft specific methods will be used since the book is a little older. • Set docObj = CreateObject ("MSXML2.DOMDocument") • The above code creates a Document object named docObj. • CreateObject is used to create an instance of the object.

  10. Setting Asynchronization Flag • Setting it to true means that other processing can occur while the document is loaded. • You generally want to set it to false so that the entire document is loaded in memory before any more processing occurs.

  11. ReadyState Property • You can use the document object’s readystate property to see where loading stands. • 1 – Loading in progress2 – Data loaded; reading and processing in progress3 – Some data read and parsed. Available as read only.4 – Loading complete. However, a failure may have occurred. • If docObj.readystate=4 Then msgbox docObj.xmlEnd If

  12. Checking for Loading Failure • If docObj.readystate=4 Then Dim loadingerr Set loadingerr = docObj.parseError If loadingerr.errorcode = 0 Then msgbox docObj.xml Else msgbox loadingerr.errorcode End If End If

  13. Reference to the DOM tree • The load method loads the XML file into memory. Below we used a relative URL as both the XML file and the .htm file exist in the same directory. • docObj.load("books.xml")

  14. Reference to the Root Object • You can return a reference to the root node in the XML document, the Document element. • Dim booksnodeSet booksnode = docobj.DocumentElement • Booksnode now contains a pointer to the document root, the <books> element.

  15. Fundamentals of a Node • Every node has properties and methods • Since nodes are specific types, they may have additional properties and methods related to that type. For example there are properties and element node might expose that an attribute node would not. • However all nodes have nodeName, nodeType and nodeValue properties. • document.write "The Node name is " & booksnode.nodeName & "<BR>“document.write "The Node type is " & booksnode.nodeType & "<BR>“document.write "The Node value is " & booksnode.nodeValue & "<BR>“ • The Node name is booksThe Node type is 1The Node value is • The root element has no value since it’s just a container. It has no text.

  16. Node Names, Types, and Values

  17. Other Node Object Properties • ParentNode • childNodes • firstChild • lastChild • previousSibling • nextSibling • Attributes • ownerDocument MSXML Extensions • nodeTypeString – Refers to node type using a string rather than a numeric value • Xml (We used this to display the entire file in a message box)

  18. What Can We Do with A Node? • You can’t assume a node has children • Use the hasChildNodes() method to find out. • document.write "The Node has children: " & theNode.hasChildNodes() & "<BR>"

  19. MSXML Node Interface Extensions • selectNodes(Criteriastring) – returns descendant nodes • selectSingleNodes(Criteriastring) – returns first matching node

  20. Fundamentals of a NodeList • Represents a set of nodes • W3C recognizes the length property and the item method • MsgBox "Number of <BOOK> objects under <books>" is & booksnode.selectNodes("book").length

  21. Getting to the Individuals of a NodeList • The item() method takes a numeric parameter and returns a reference to the associated node. The collection is zero based. Dim BookNodes Set BookNodes = Booksnode.SelectNodes("book") Dim oneNode Set oneNode = BookNodes.item(1) msgBox oneNode.xml

  22. Fundamentals of an Element Node • If you are using namespaces, the basename property returns the name of the element without the prefix. • The prefix property returns the prefix. • The namespaceURI property returns the namespace URI

  23. Relating to Other Element Nodes • firstbook.parentnode.nodeName returns “BOOKS” • firstBook.nextSibling.xml returns the second book’s XML • firstBook.lastChild.xml returns the price element as well as its content. • firstbook.ownerDocument.nodeName returns “#document”

  24. Learning About Attributes of an Element Node • firstbook.attributes.length returns 3 • firstbook.attributes(0).nodevalue returns the isbn of the first book • firstbook.attributes returns a NamedNodeMap object, an unordered list of nodes whose individual nodes may be retrieved by name using the getAttribute method. • firstbook.getAttribute(“ISBN”) returns the isbn of the first book.

More Related