240 likes | 261 Views
Learn about the purpose, development, and usage of XML Document Object Model (DOM) with essential interfaces and methods. Explore techniques to add, delete elements, attributes, and manipulate DOM trees effectively for enhanced data handling.
E N D
Beginning XML 4th Edition
Chapter 11 Objectives • What is the purpose of the XML Document Object Model? • How the DOM specification was developed at W3C • About important XML DOM interfaces and objects, such as the Node and Document interfaces • How to add and delete elements and attributes from a DOM and manipulate a DOM tree in other ways • How the XML DOM is used "under the covers" in Microsoft InfoPath 2003
What Is an XML Document Object Model For? • One of the possibilities is to read a file from disk. This is a common technique now when dealing with configuration files; user options held in an XML format are easier to deal with than the traditional techniques such as INI files or Registry settings. • A second option is to convert a string representation embedded in the source code. This is popular for examples but not often used in real-life scenarios because of the difficulty in changing the XML when needed. • A third method is to accept a stream piped from another source such as a response from a web service or a database stored procedure.
Interfaces and Objects • Abstract concept • Properties defined are all available • Methods specified are all available
The Document Object Model at the W3C DOM Level 1 www.w3.org/DOM/ DOM Level 2 www.w3.org/TR/DOM-Level-2-Core/ DOM Level 3 www.w3.org/TR/DOM-Level-3-Core/
Two Ways to View DOM Nodes • A hierarchy of node objects • Easy to identify common properties and methods • Document node as root • Descendant nodes are specialized object types • Child node • DocumentType • ProcessingInstructions • Comments We like the second one
Overview of the XML DOM Nodes • Root of the DOM hierarchy is the Document node • Child nodes are: • DocumentType nodes • Element nodes • ProcessingInstruction nodes • Comment nodes Only the element node can have child nodes.
Tools to Run the Examples • The Differences Between Browsers • Microsoft • COM class • Active X Component • JavaScript (Jscript) • Mozilla aka Firefox • alert( )
Try It Out Navigating to the Document Element
The Node Object attributes childNodes firstChild lastChild localName namespaceURI nextSibling nodeName nodeType nodeValue ownerDocument parentNode prefix previousSibling
Try It Out Exploring Child Nodes
Methods of the Node Object appendChild(newChild) cloneNode(deep) hasAttributes() hasChildNodes() insertBefore(newChild, refChild) isSupported(feature, version) normalize() removeChild(oldChild) replaceChild(newChild, oldChild)
Try It Out Deleting a NodeAdding New Nodes
The Effect of Text Nodes <Book xml:space="preserve"> <Chapter>This is Chapter 1.</Chapter> <Chapter>This is Chapter 2.</Chapter> <Chapter>This is Chapter 3.</Chapter> </Book> See the IE Result Click Here See the Firefox Result Click Here
The NamedNodeMap Object • getNamedItem(name) • getNamedItemNS(namespaceURI, localName) • item(index) • removeNamedItem(name) • removeNamedItemNS(namespaceURI, localName) • setNamedItem(node) • setNamedItemNS(node) • and • NodeList Object • DOMException Object
Try It Out Adding and Removing AttributesCreating a DOMException
The Document Interface • documentElement • doctype • implementation 3 Interfaces and 14 methods
How the XML DOM Is Used in InfoPath 2007 • XML-based form tool • Using XML DOM in MSXML 6 • Free trial download http://office.microsoft.com/en-us/infopath/default.aspx