Extensible Markup Language: in This Session, You Will Learn To
Extensible Markup Language: in This Session, You Will Learn To
Objectives
Ver. 1.0
Session 8
Slide 1 of 31
DOM defines the logical structure of documents. DOM provides an Application Programming Interface (API) for dynamically accessing and manipulating a document. The DOM objects have associated methods and properties to access and manipulate a document. A DOM-enabled parser is required to use the features provided by DOM. A DOM-enabled parser:
Parses an XML document to ascertain its validity. Creates an in-memory representation of the XML document as a tree structure.
Ver. 1.0
Session 8
Slide 2 of 31
MSXML parser:
Is the Microsoft implementation of DOM. Provides fundamental as well as added interfaces to access documents.
The following figure represents how a DOM tree is used by applications to access data.
MSXML Library XML Document Parser Parsed Document DOM Tree Root Child Text Child Text Application
Ver. 1.0
Session 8
Slide 3 of 31
Ver. 1.0
Session 8
Slide 4 of 31
Ver. 1.0
Session 8
Slide 5 of 31
Ver. 1.0
Session 8
Slide 6 of 31
It represents a single node in the XML document tree structure. It provides methods to work with child elements. Some of the methods of this object are appendChild(newChild), insertBefore(newNode,refNode), and removeChild(nodeName).
Ver. 1.0
Session 8
Slide 7 of 31
It provides a list of nodes present in an XML document for manipulation. This object enables you to iterate through a collection of nodes. Some of the method of this object are item() and nextNode().
Ver. 1.0
Session 8
Slide 8 of 31
It represents an attribute of the Element object. It is also a Node and inherits various attributes and methods of Node object. An attribute is not considered by the DOM to be a child node of an element, but rather a property.
Ver. 1.0
Session 8
Slide 9 of 31
It represents the text inside an XML element in the node tree. The splitText() method is associated with this object.
Ver. 1.0
Session 8
Slide 10 of 31
It represents the CDATA sections in the node tree. This node is used to escape the parts of text that normally would be recognized as markup.
Ver. 1.0
Session 8
Slide 11 of 31
It returns information about the most recent parse error. It provides properties to retrieve information, such as the error code, the error text, and the line that caused the error. Some of the properties of this object are errorCode, reason, line, and srcText.
Ver. 1.0
Session 8
Slide 12 of 31
The DOM objects can be used within scripting languages such as JavaScript and VBScript. Using DOM objects in scripts allow dynamically applying a style sheet to an XML document. The code for using DOM objects for accessing an XML document needs to be used as an HTML page.
Ver. 1.0
Session 8
Slide 13 of 31
DOM:
Used to validate XML documents against XML schemas. Ensures proper grammar and proper formation of the XML document.
DOMDocument object:
Main object in a DOM. Represents the top node in every document tree. Used to load an XML document, parse it, and validate it.
XMLSchemaCache object:
Loads the schema document associated with the XML document.
Ver. 1.0
Session 8
Slide 14 of 31
Used to hold a collection of schema documents that specifies the rules to which XML documents must conform. Following table describes some methods provided by the XMLSchemaCache object.
Method add(namespaceURI, variable) addCollection(XMLSch emaCollection object) get(namespaceURI) namespaceURI(index number) remove(namespaceURI) Description This method adds a new schema to the collection and also associates the specified namespaceURI with the schema. This method adds schemas from other schema collections. It also ensures that the namespaceURIs of the different schemas do not clash. This method returns a node that contains the <schema> element. This method returns the namespace that is associated with the schema at the specified index number. This method removes a schema from a collection.
Ver. 1.0
Session 8
Slide 15 of 31
To write a script that validates an XML document against the schema, you need to follow the given tasks:
1. Create the user interface to accept the name of the XML document and the XML schema. 2. Write the code to load the XML document. 3. Write the code to add the XML schema in the XMLSchemaCache object. 4. Write the code to validate the XML document against the schema.
Ver. 1.0
Session 8
Slide 16 of 31
Problem Statement:
The head office of CyberShoppe receives data in the form of XML documents from its branches. In order to ensure the consistency of data sent by the branches, the head office maintains the definitions for the structures of documents in schemas. After receiving data from the branches, the head office needs to verify that the data conforms to the schema of the respective document. For this, the head office needs to write a script that validates the data stored in an XML document against a schema. You will initially do the testing for the product.xml file.
Ver. 1.0
Session 8
Slide 17 of 31
Ver. 1.0
Session 8
Slide 18 of 31
Is a DOM object that is used to access an XSLT style sheet. Used to hold a cached style sheet that can then be dynamically associated with an XML document. Before a style sheet document can be applied to an XML document, it is converted into a tree structure by the parser. XSLT tree structure is loaded into the memory of the computer and used to process the XML document.
Ver. 1.0
Session 8
Slide 19 of 31
Used to apply a style sheet to an XML document and then process that document. Applies the given XSLT document to a specific XML document. Transforms an XML document by using the XSLT style sheet. JavaScript code to create an XSLProcessor object is as follows:
var xslprocobj= xsltobj.createProcessor();
Ver. 1.0
Session 8
Slide 20 of 31
Problem Statement:
CyberShoppe sells its products through an e-commerce Web site. Product details, such as product name, description, price, and quantity, need to be displayed. A customer can choose to view the product details either as a table or as a bulleted list.
Ver. 1.0
Session 8
Slide 21 of 31
Problem Statement:
The following XML document, customer.xml contains customer details:
<?xml version="1.0"?> <CUSTOMER> <CUSTOMERNAME>Harold Johnson</CUSTOMERNAME> <ADDRESS>56, Regent Road</ADDRESS> <CITY>London</CITY> <COUNTRY>UK</COUNTRY> <PHONE>444-425-2355</PHONE> </CUSTOMER>
Create a Web page that accepts the name of the XML file in a text box. When you click the Submit hyperlink, a JavaScript code should load the XML document and extract the CUSTOMERNAME element. The address and phone number should be changed to 94, McFarlane Avenue and 412-2332344, respectively, by using XML DOM objects.
Ver. 1.0
Session 8
Slide 22 of 31
Answer:
c. createNode()
Ver. 1.0
Session 8
Slide 23 of 31
You have written a script to load an XML document and validate it against a schema. Which of the following properties of the DOMDocument object will you use to ensure that the script waits till the XML document is loaded before executing the rest of the code?
a. b. c. d. async readyState documentElement xml
Answer:
a. async
Ver. 1.0
Session 8
Slide 24 of 31
Answer:
d. nodeValue
Ver. 1.0
Session 8
Slide 25 of 31
Which of the following properties of the IXMLDOMParseError object will you use to retrieve the string containing the line that caused an error while parsing an XML document?
a. b. c. d. srcText linePos reason line
Answer:
a. srcText
Ver. 1.0
Session 8
Slide 26 of 31
Which one of the following is an object that represents the complete XML document map?
a. b. c. d. IXMLDOMNode DOMDocument IXMLDOMNodeList IXMLDOMParseError
Answer:
b. DOMDocument
Ver. 1.0
Session 8
Slide 27 of 31
Session 8
Slide 28 of 31
Ver. 1.0
Session 8
Slide 29 of 31
Some DOM objects that are used to manipulate data in a document are:
Document Element Node NodeList Attr Text CDataSection ParseError
The Document object is the top-level object in the XML DOM. This object provides various properties and methods that help you to navigate, query, and modify the content and structure of XML documents. The Element object represents all the element nodes in a document.
Ver. 1.0
Session 8
Slide 30 of 31
Ver. 1.0
Session 8
Slide 31 of 31