100 likes | 193 Views
CIS 375—Web App Dev II. DOM. Introduction to DOM. The XML Document ________ Model (DOM) is a programming interface for XML documents. It defines the way an XML document can be accessed and _____________.
E N D
Introduction to DOM • The XML Document ________ Model (DOM) is a programming interface for XML documents. • It defines the way an XML document can be accessed and _____________. • The XML DOM is designed to be used with any programming language and any __________ system. • The DOM represents a ______ view of the XML document. • The documentElement is the top-level of the tree. • This element has one or many ___________ that represent the branches of the tree.
Parsing the DOM • The Microsoft XML _______ is a COM component that comes with Microsoft Internet Explorer 5.0. • The Microsoft XMLDOM parser: • Supports JavaScript, VBScript, Perl, VB, Java, C++, … • Supports W3C XML 1.0 and XML DOM • Supports DTD and validation • Creating an XML document object using JavaScript, _________, and VBScript in ASP: var xmlDoc = new ActiveXObject("Microsoft.XMLDOM") set xmlDoc = CreateObject("Microsoft.XMLDOM") set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
Loading XML Files and Text • Loading a file <script type="text/javascript"> var xmlDoc = new ActiveXObject("Microsoft.XMLDOM") xmlDoc.async="false" xmlDoc.load("note.xml") // ....... processing the document goes here </script> • Loading text (see example) <script type="text/javascript"> var text="<note>“ text=text+"<to>Tove</to><from>Jani</from>“ text=text+"<heading>Reminder</heading>“ text=text+"<body>Don't forget me this weekend!</body>“ text=text+"</note>" var xmlDoc = new ActiveXObject("Microsoft.XMLDOM") xmlDoc.async="false" xmlDoc.loadXML(text) // ....... processing the document goes here </script>
Parser Errors • The parseError _______ can be used to extract error information from the Microsoft XML parser, but is not a part of the W3C DOM standard. • The following ___________ code accesses the parseError object: var xmlDoc = new ActiveXObject("Microsoft.XMLDOM") xmlDoc.async="false" xmlDoc.load("note_error.xml") document.write("<br>Error Code: ") document.write(xmlDoc.parseError.errorCode) document.write("<br>Error Reason: ") document.write(xmlDoc.parseError.reason) document.write("<br>Error Line: ") document.write(xmlDoc.parseError.line) • Try it Yourself or just look at the XML file
DOM Validator • To help you validate your xml, w3schools has used Microsoft's XML parser to create an xml validator. • Paste your XML in the text area, and validate it by pressing the validate button. • You can also validate your XML files simply by typing the URL of your XML file and pressing the submit button. • NOTE: If you get an error when accessing this file, it is because your IE security settings do not allow access across domains. To correct, select • Tools, Internet Options, Security, Custom Level…, Miscellaneous, Access data sources across domains
Accessing the DOM • The following VBScript code examples traverse an XML node _____, and display the XML elements in the browser: • JUST TRY IT and also try the CD catalog example • The following JavaScript reads XML data from an XML document and writes the XML data into (waiting) ______ elements. • JUST TRY IT • Addressing elements by number is not the preferred way to extract XML elements. Using ______ is better. • JUST TRY IT
The HttpRequest Object • The httpRequest object is not a part of the W3C DOM __________. • How to get an XML file from the server using the httpRequest object (works only in IE): • Try it Yourself or Try this example • How to get an XML file from the server using the httpRequest object (works only in IE): • Try it Yourself • You can also send an XML document to an ASP page on the server, analyze the request, and send back the result. • Try it Yourself (see the w3Schools page for ASP code)
DOM NodeTypes • Nodes are separated into different types. • The XML file used in the examples: note_special.xml • We traverse the file to get the nodeType of the nodes: NodeType • We traverse the file to get the nodeName of the same nodes: NodeName • We traverse the file to get the nodeValue of the same nodes: NodeValue • In IE5, you can also get the nodeType as a string, with the .nodeTypeString property: NodeTypeString