DOM Programming n The Document Object Model standardises

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

DOM Nodes and Node. Lists n n All nodes have ¨ type get. Node. Type() ¨ name get. Node. Name() ¨ value get. Node. Value() Nodes are arranged in ¨ Node. Lists e. g. child elements of <ol> ¨ Named. Node. Maps e. g. attributes of <img>

DOM Node Traversal Methods n Element nodes have ¨ parent Node get. Parent. Node() ¨ children Node get. First. Child() Node get. Last. Child() Node. List get. Child. Nodes() ¨ siblings Node get. Next. Sibling() Node get. Previous. Sibling() ¨ attributes Named. Node. Map get. Attributes()

DOM Node. Lists n Node. Lists have ¨ length ¨ individual n int get. Length() items Node item(n) Named. Node. Maps have ¨ length int get. Length() ¨ individual Node item(n) items ¨ named items Node get. Named. Item(str)

DOM Demonstration n Java. Script binding allows Dynamic XML n dom. html contains a demonstration of DOM access of an XML document.

Microsoft Extensions to DOM n New functions combine DOM and XPath ¨ Node. List select. Nodes("XPath expression") ¨ Node select. Single. Node("XPath expression") (see later lesson for XPath) n DOM calls renamed as properties e. g. n. get. Node. Type() becomes n. node. Type and document. Element. get. Child. Nodes() becomes document. Element. child. Nodes n The property. text applied to an element represents the concatenation of its textual contents and those of all its subelements.

Link Checking: Sample DOM Use n Often an application needs to ¨ search through the entire document for a single piece of data n every occurrence of some data n n Need functions to ¨ traverse the complete check. All. Nodes() ¨ test document hierarchy each node check. This. Node()

Link Checking: Outline Framework function check. All. Nodes(n){ check. This. Node(n); if(n. has. Child. Nodes){. . . Iterate around all children } (see next page) } function check. This. Node(n){ if(n==null)return; Perform application. . . specific test (see sample file) }

Link Checking: Code Details n To iterate around all children var children=n. child. Nodes var i=0; for(i=0; i<children. length; i++) check. All. Nodes(children. item(i)) n Useful fragments for app-specific test is element name / #PCDATA ¨ n. get. Attribute(name) returns value of the named attribute ¨ n. node. Name

Link Checking: Putting It Together n To start the recursion off, call check. All. Nodes( xmlstuff. XMLDocument. document. Element); n See check. Links. html

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

XPath: DOM Path Specification n 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 n Also used in conjunction with new standards for queries and linking.

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

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

XPath Expressions (3) n title|heading|label ¨ either a title or a heading n /book/chapter/@number ¨ the or a label element number attribute of a chapter element inside a top-level book element
![XPath Expressions (4) n chapter[title] ¨ a chapter element with a title element n XPath Expressions (4) n chapter[title] ¨ a chapter element with a title element n](http://slidetodoc.com/presentation_image_h2/855f29e34ff680bc94133fe91627443e/image-16.jpg)
XPath Expressions (4) n chapter[title] ¨ a chapter element with a title element n chapter[title="Gone with the Wind"] ¨a chapter element whose title element has the contents "Gone with the Wind" n chapter[1] ¨ the first chapter element n para[@security='classified'] ¨ para elements with a security attribute set

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