Presentation is loading. Please wait.

Presentation is loading. Please wait.

DOM Programming The Document Object Model standardises  what an application can see of the XML data  how it can access it An XML structure is a tree.

Similar presentations


Presentation on theme: "DOM Programming The Document Object Model standardises  what an application can see of the XML data  how it can access it An XML structure is a tree."— Presentation transcript:

1 DOM Programming The Document Object Model standardises  what an application can see of the XML data  how it can access it An XML structure is a tree of Nodes  elements– text – entities  attributes– processing instructions

2 DOM Nodes and NodeLists All nodes have  type getNodeType()  name getNodeName()  value getNodeValue() Nodes are arranged in  NodeLists e.g. child elements of  NamedNodeMaps e.g. attributes of

3 DOM Node Traversal Methods Element nodes have  parent Node getParentNode()  children Node getFirstChild() Node getLastChild() NodeList getChildNodes()  siblings Node getNextSibling() Node getPreviousSibling()  attributes NamedNodeMap getAttributes()

4 DOM NodeLists NodeLists have  length int getLength()  individual items Node item(n) NamedNodeMaps have  length int getLength()  individual items Node item(n)  named items Node getNamedItem(str)

5 DOM Demonstration JavaScript binding allows Dynamic XML dom.html contains a demonstration of DOM access of an XML document. dom.html

6 Microsoft Extensions to DOM New functions combine DOM and XPath  NodeList selectNodes("XPath expression")  Node selectSingleNode("XPath expression") (see later lesson for XPath) DOM calls renamed as properties e.g. n.getNodeType() becomes n.nodeType and documentElement.getChildNodes() becomes documentElement.childNodes The property. text applied to an element represents the concatenation of its textual contents and those of all its subelements.

7 Link Checking: Sample DOM Use Often an application needs to  search through the entire document for a single piece of data every occurrence of some data Need functions to  traverse the complete document hierarchy checkAllNodes()  test each node checkThisNode()

8 Link Checking: Outline Framework function checkAllNodes(n){ checkThisNode(n); if(n.hasChildNodes){... } function checkThisNode(n){ if(n==null)return;... } Iterate around all children (see next page) Perform application- specific test (see sample file)

9 Link Checking: Code Details To iterate around all children var children=n.childNodes var i=0; for(i=0; i<children.length; i++) checkAllNodes(children.item(i)) Useful fragments for app-specific test  n.nodeName is element name / #PCDATA  n.getAttribute(name) returns value of the named attribute

10 Link Checking: Putting It Together To start the recursion off, call checkAllNodes( xmlstuff.XMLDocument.documentElement); See checkLinks.html checkLinks.html

11 DOM Pros and Cons Pros  very powerful and flexible  good for rich, complex data and documents Cons  Must write a complex program!  Highly tedious to specify correct DOM location

12 XPath: DOM Path Specification Standard for declarative expression of DOM traversal  XPath navigates around the elements in an XML document  like a URL navigates around documents in the Web Also used in conjunction with new standards for queries and linking.

13 XPath Expressions (1) /book/chapter/title  a title element inside a chapter element inside the top-level book element /book/*/title  a title element inside any element inside the top-level book element /book//title  a title element anywhere inside the top-level book element

14 XPath Expressions (2) para/quote  a quote element inside a paragraph element inside the current element./para/quote  same as above../para/quote  a quote element inside a paragraph element inside the parent of the current element

15 XPath Expressions (3) title|heading|label  either a title or a heading or a label element /book/chapter/@number  the number attribute of a chapter element inside a top-level book element

16 XPath Expressions (4) chapter[title]  a chapter element with a title element chapter[title="Gone with the Wind"]  a chapter element whose title element has the contents "Gone with the Wind" chapter[1]  the first chapter element para[@security='classified']  para elements with a security attribute set

17 XPath Pros and Cons XPath is like regular expressions for XML Pros  Simple, expressive  Good for both documents and data Cons  Can’t DO anything with it – must use in conjunction with DOM or XSLT processing


Download ppt "DOM Programming The Document Object Model standardises  what an application can see of the XML data  how it can access it An XML structure is a tree."

Similar presentations


Ads by Google