SlideShare ist ein Scribd-Unternehmen logo
1 von 44
Working with the XML Document Object Model


Objectives
In this lesson, you will learn to:
 Identify the need for XML Document Object Model
  (DOM)
 Use the following XML DOM objects to access
  different parts of an XML document:
     DOMDocument
     IXMLDOMNode
     IXMLDOMNodeList
     IXMLDOMParseError


©NIIT                    eXtensible Markup Language/Lesson 7/Slide 1 of 43
Working with the XML Document Object Model


The XML Document Object Model
 The Document Object Model (DOM) defines the
  logical structure of documents and the way a
  document is accessed and manipulated. It provides an
  Application Programming Interface (API) for XML and
  HTML documents.
 XML DOM views an XML document as being
  composed of objects. Each object has properties and
  behavior that can be manipulated using the methods
  provided by a DOM interface.




©NIIT                 eXtensible Markup Language/Lesson 7/Slide 2 of 43
Working with the XML Document Object Model


The XML Document Object Model (Contd.)
 Using XML DOM, programmers can create and build
  XML documents, navigate the structure of documents,
  and add, modify, or delete elements and their content.
 The objects and methods provided by XML DOM can
  be used with any programming language, such as
  Java, C++, and Visual Basic. These objects can also
  be used with scripting languages, such as VBScript
  and JavaScript.
 To be able to use the features of XML DOM, you need
  to use a DOM-enabled parser.



©NIIT                 eXtensible Markup Language/Lesson 7/Slide 3 of 43
Working with the XML Document Object Model


The XML Document Object Model (Contd.)
 A DOM-enabled parser reads an XML document and
  parses it to ensure that it is a valid document. Then, it
  creates an in-memory representation of the XML
  document by organizing its contents in a logical tree-
  structure. The tree-structure is made up of nodes.
 MSXML is an example of a DOM-enabled XML parser.




©NIIT                   eXtensible Markup Language/Lesson 7/Slide 4 of 43
Working with the XML Document Object Model


Implementation of DOM in the MSXML Parser
 When the MSXML parser loads an XML document, it
  reads the document and creates a tree structure that
  represents the various components of the XML
  document.




©NIIT                 eXtensible Markup Language/Lesson 7/Slide 5 of 43
Working with the XML Document Object Model


Implementation of DOM in the MSXML Parser
(Contd.)
 The following diagram depicts how DOM allows
  applications to access information stored in XML
  documents:
                      MSXML Library

  XML                                 DOM Tree
  Document   Parser                   Root                       Application
                        Parsed              Child
                        Document                    text
                                            Child
                                                    text




©NIIT                      eXtensible Markup Language/Lesson 7/Slide 6 of 43
Working with the XML Document Object Model


Tree structure of a Document
 The basic building block of the tree structure is the
  node.
 Nodes are generic containers that hold information
  about the elements, attributes, content, comments,
  and processing instructions that are stored in an XML
  document.
 The XML document can be viewed as a single node
  that contains all the other nodes.




©NIIT                   eXtensible Markup Language/Lesson 7/Slide 7 of 43
Working with the XML Document Object Model


Tree structure of a Document
 The root element of the XML document is represented
  as the root node of the corresponding DOM
  tree‑structure.
 Every component of the XML document is
  represented as a node in the DOM tree-structure.
 Each node has a name, type, and value associated
  with it. The name of the node is the name of the
  component with which the node is associated. The
  type of the node depends on the type of the
  component it represents.
 DOM treats each of these nodes as objects.
  Therefore, it is possible to create a script that loads an
  XML document, traverses through all nodes, and
  displays the required information to the user.
©NIIT                   eXtensible Markup Language/Lesson 7/Slide 8 of 43
Working with the XML Document Object Model


XML DOM Objects and Methods
 The main objects and methods provided by XML DOM
  that enable you to traverse, read, and manipulate the
  structure and content of an XML document are listed
  below:
     The DOMDocument object
     The IXMLDOMNode object
     The IXMLDOMNodeList object
     The IXMLDOMParseError object




©NIIT                 eXtensible Markup Language/Lesson 7/Slide 9 of 43
Working with the XML Document Object Model


The DOMDocument object
 The DOMDocument object is the top-level object in
  XML DOM. It implements all the basic DOM methods
  required to work with an XML document. It also has
  methods, which support XSLT.
 This object is associated with various methods that
  help you navigate, query, and modify the content and
  the structure of an XML document.




©NIIT                eXtensible Markup Language/Lesson 7/Slide 10 of 43
Working with the XML Document Object Model


The DOMDocument object (Contd.)
 Some of the most commonly used methods of the
  DOMDocument object are:
     createElement(elementname): Creates an element
      node.
     createAttribute(attributename): Creates an attribute
      node.
     CreateNode(type, name, namespace-URI): Creates
      a node of the specified type. Also supports
      namespace URI.
     getElementsByTagName(elementname): Returns a
      collection of element nodes that have the specified
      element name.
©NIIT                  eXtensible Markup Language/Lesson 7/Slide 11 of 43
Working with the XML Document Object Model


The DOMDocument object (Contd.)
     load(XMLdocument): Loads the specified XML
      document.
     appendChild(child node): Appends the specified
      child node to the document.
     save(destination): Saves the XML document
      represented by the DOMDocument object at the
      specified destination.
 Some of the commonly used properties of the
  DOMDocument object are:
     async: Allows you to specify whether the XML
      document can be loaded asynchronously.
     childNodes: Returns a list of child nodes.
     firstChild: Returns the first child in the document.
©NIIT                  eXtensible Markup Language/Lesson 7/Slide 12 of 43
Working with the XML Document Object Model


The DOMDocument object (Contd.)
    readyState: Returns the information about the
     state of the XML document (loading, loaded,
     interactive, complete).
    parseError: Returns an object of
     IXMLDOMParseError, which can be used to
     retrieve error information.
    xml: Returns the XML representation of a
     node.
    validateOnParse: Specifies whether the parser
     should check for validity of an XML document.

©NIIT               eXtensible Markup Language/Lesson 7/Slide 13 of 43
Working with the XML Document Object Model


Creating a DomDocument object
 The following is the code segment written using
  JavaScript to create an instance of the DOMDocument
  object:
  var myxmldoc = new
  ActiveXObject(Msxml2.DOMDocument.4.0);
 A reference to the newly created object is stored in
  the variable myxmldoc, which can be used to load
  and manipulate XML documents.




©NIIT                 eXtensible Markup Language/Lesson 7/Slide 14 of 43
Working with the XML Document Object Model


Loading an XML file
 To load an XML file by using a URL, you can write the
  following code in JavaScript :
   myxmldoc.load(http://www.sb.com/
  employee.xml);
 The load() method can also be used to load a file
  from the local host by providing the path, as shown in
  the following example:


  myxmldoc.load(c:examplesemployees.xml
  );


©NIIT                 eXtensible Markup Language/Lesson 7/Slide 15 of 43
Working with the XML Document Object Model


Loading an XML file (Contd.)
 To load an XML document synchronously, you can
  write the following code in JavaScript:
  var myxmldoc = new
  ActiveXObject(Msxml2.DOMDocument.4.0);
  myxmldoc.async=false;
  myxmldoc.load(employee.xml);




©NIIT               eXtensible Markup Language/Lesson 7/Slide 16 of 43
Working with the XML Document Object Model


Using the readyState Property
 To check if the document has been loaded
  completely, you can use the readyState property.
  The readyState property holds a numeric value,
  which represents one of the following states:
     LOADING (1): This state indicates that the loading
      process is in progress and data is not yet parsed.
     LOADED (2): This state indicates that data has
      been read and parsed but the object model is not
      yet ready.




©NIIT                 eXtensible Markup Language/Lesson 7/Slide 17 of 43
Working with the XML Document Object Model


Using the readyState Property (Contd.)
  INTERACTIVE (3): This state indicates that the object
   model is available with partially retrieved data set and
   is in the read-only mode.
  COMPLETED (4): This state indicates that the
   loading process is complete.




©NIIT                 eXtensible Markup Language/Lesson 7/Slide 18 of 43
Working with the XML Document Object Model


Creating a New Node
 You can create a new node by using the
  createNode() method.
 The following is the code segment to create a root
  element by using the createNode() method:
  var myxmldoc = new
  ActiveXObject(Msxml2.DOMDocument.4.0);
  if (myxmldoc.childnodes.length==0)
  {

  myroot=myxmldoc.createNode(1,ORDERDETAI
  LS, );
  myxmldoc.appendChild(myroot);
  myxmldoc.save(myxmldoc.XMLDocument);
  }
©NIIT            eXtensible Markup Language/Lesson 7/Slide 19 of 43
Working with the XML Document Object Model


Creating New Elements
 You can create new elements for the document by
  using the createElement() method of the
  myxmldoc DOMDocument object. The
  createElement() method takes one parameter, the
  name of the element that is to be created.
 The following example creates a new element node
  called “salary”:
  var xmlelement;
  xmlelement=myxmldoc.createElement
  (salary);


©NIIT               eXtensible Markup Language/Lesson 7/Slide 20 of 43
Working with the XML Document Object Model


Accessing Elements From an XML File
 To access the elements from an XML document, you
  can begin at the root element and navigate through the
  document tree. You can also query for a specific node
  or nodes.
 To reach a particular node or nodes within the
  document tree structure, you can use the
  getElementsByTagName() method.




©NIIT                eXtensible Markup Language/Lesson 7/Slide 21 of 43
Working with the XML Document Object Model


The IXMLDOMNode object
 The IXMLDOMNode object represents a node in the
  XML document tree structure.
 The node could be an element that contains child
  elements.
 The IXMLDOMNode object provides methods to work
  with child elements.
 Some commonly used methods of the IXMLDOMNode
  object are:
     appendChild(newchild): Appends a new child to
      the node.
     insertBefore(newNode, refNode): Inserts a new
      node before the specified node.
     removeChild(nodeName): Removes the specified
      node.
©NIIT                eXtensible Markup Language/Lesson 7/Slide 22 of 43
Working with the XML Document Object Model


The IXMLDOMNode object (Contd.)
     replaceChild(newNode, oldNode): Replaces the
      oldNode with newNode.
     hasChildNodes(): Specifies whether the node has
      child nodes.
     cloneNode(boolean): Creates a clone of the node
      represented by the IXMLDOMNode object.
Some of the commonly used properties of the
IXMLDOMNode object are:
     nodeName: Returns the name of the node.
     nodeType: Specifies the type of the node.
     nodeValue: Returns the text contained in the node.
     childNodes: Returns the child nodes of the node.
©NIIT                 eXtensible Markup Language/Lesson 7/Slide 23 of 43
Working with the XML Document Object Model


The IXMLDOMNode object (Contd.)
         firstChild: Returns the first child of the node.
         lastChild: Returns the last child of the node.
         text: Returns the text contained in the node.
         xml: Returns the XML code for the node.




©NIIT                    eXtensible Markup Language/Lesson 7/Slide 24 of 43
Working with the XML Document Object Model


Accessing Text Values of Elements
 The text enclosed within the tags in an XML file is
  used as a node value in DOM. A node value can be
  the value of an attribute or the text within an element.
 You can display the text within the elements or
  attributes by using the text property, as shown
  below:
   alert(myelement.text);
 You can also set the value of an element by using this
  property, as shown in the example given below:
        myelement.text=“Barbie Doll”;

©NIIT                  eXtensible Markup Language/Lesson 7/Slide 25 of 43
Working with the XML Document Object Model


Appending a New Child Node
 The following code segment creates a new element
  by using the createElement() method of the
  DOMDocument object. Then, it appends the newly
  created node to the last child of myelement by using
  the appendchild() method:
  var myelement=myxmldoc.documentElement;
  var newElement=myxmldoc.createElement
  (lastchild);
  myelement.appendChild(newElement);




©NIIT                eXtensible Markup Language/Lesson 7/Slide 26 of 43
Working with the XML Document Object Model


Inserting a Node Before Two Existing Nodes
 The following code segment creates a new element
  called department and replaces and existing node
  with the new element:
  var newElement= myxmldoc.createElement
  (department);
  var
  oldElement=myxmldoc.documentElement.chil
  dNodes.item(0).firstChild;


  myxmldoc.documentElement.childNodes.item
  (1).insertBefore(newElement,
  oldElement);
©NIIT               eXtensible Markup Language/Lesson 7/Slide 27 of 43
Working with the XML Document Object Model


Removing a Child Node
 The following code segment removes a child node by
  using the removeChild() method:
   var
   oldElement=myxmldoc.documentElement.chi
   ldNodes.item(0).firstChild;

   myxmldoc.documentElement.childNodes.ite
   m(0).RemoveChild(oldElement);




©NIIT               eXtensible Markup Language/Lesson 7/Slide 28 of 43
Working with the XML Document Object Model


Replacing a Node (Contd.)
 In the following example, the second element in the
  document is replaced with the new element named
  department:
Var newElement=
  myxmldoc.createElement(department);
Var
  oldElement=myxmldoc.documentElement.chil
  dNodes.item(0).firstChild;
myxmldoc.documentElement.childNodes.item(1
  ).replaceChild(newElement, oldElement);


©NIIT                eXtensible Markup Language/Lesson 7/Slide 29 of 43
Working with the XML Document Object Model


The IXMLDOMNodeList object
 The IXMLDOMNodeList object enables you to iterate
  through a collection of nodes.
 Some methods of the IXMLDOMNodeList object are:
    item(number):Returns the node at the position
     indicated by the specified number.
    nextNode(): Returns the next node in the list.
 To obtain the length of the node list, use the length
  property. The length property can also be used to
  traverse through the list of child nodes.



©NIIT                 eXtensible Markup Language/Lesson 7/Slide 30 of 43
Working with the XML Document Object Model


The IXMLDOMNodeList object (Contd.)
 The following code traverses through the child nodes
  of myelement:
  var
  myelement=myxmlDoc.getElementsByTagName(
  emp);
  for(i=0;i myelement.length –1;i++)
  alert(myelement.item(i).xml);




©NIIT                eXtensible Markup Language/Lesson 7/Slide 31 of 43
Working with the XML Document Object Model


The IXMLDOMParseError Object
 The IXMLDOMParseError object returns information
  about the most recent parse error.
 The IXMLDOMParseError object provides properties
  to retrieve information, such as the error code, the
  error text, and the line that caused the error.




©NIIT                eXtensible Markup Language/Lesson 7/Slide 32 of 43
Working with the XML Document Object Model


The IXMLDOMParseError Object (Contd.)
The properties of the IXMLDOMParseError
 object are:
    errorCode: Returns the error code.
    reason: Returns a string explaining the reason
     for the error.
    line: Returns the line number where the error
     occurred.
    linePos: Returns the position in the line where
     the error occurred.
    srcText: Returns a string containing the line
     that caused the error.
©NIIT                eXtensible Markup Language/Lesson 7/Slide 33 of 43
Working with the XML Document Object Model


Using the IXMLDOMParseError object
 You can use the IXMLDOMParseError object to
  display information about the errors that are
  encountered while parsing an XML document.
 Consider the following example:
  var prodxml = new
  ActiveXObject(Msxml2.DOMDocument.4.0);
  prodxml.async = false;
  prodxml.load(product.xml);
  if (prodxml.parseError.errorCode != 0)
  { alert(A parse error occurred.);}
  else
  {alert(prodxml.documentElement.xml);}
©NIIT                eXtensible Markup Language/Lesson 7/Slide 34 of 43
Working with the XML Document Object Model


Problem Statement 7.D.1
 The product details of CyberShoppe are stored in an
  XML document. The structure of the XML document is
  defined in a DTD. The data held in the XML document
  must be validated against the rules specified for the
  data store.




©NIIT                eXtensible Markup Language/Lesson 7/Slide 35 of 43
Working with the XML Document Object Model


Task List
 Identify a mechanism to access an XML document
  programmatically.
 Identify the objects required to access the XML
  document.
 Write the code to access the document.
 Execute the code.




©NIIT                 eXtensible Markup Language/Lesson 7/Slide 36 of 43
Working with the XML Document Object Model


Task 1: Identify a mechanism to access an XML
document programmatically.
Result:
 The contents of an XML document can be accessed
  and manipulated from any application by using XML
  DOM. Therefore, to access an XML document
  programmatically, the best solution is to use XML
  DOM. Applications developed using VBScript,
  JavaScript, C/C++, or Visual Basic can use XML DOM
  objects.




©NIIT               eXtensible Markup Language/Lesson 7/Slide 37 of 43
Working with the XML Document Object Model


Task 2: Identify the objects required to access
the XML document.
Result:
 You can validate the contents of an XML document
  against a DTD by using the following XML DOM
  objects:
     DOMDocument
     IXMLDOMParseError




©NIIT               eXtensible Markup Language/Lesson 7/Slide 38 of 43
Working with the XML Document Object Model


Task 3: Write the code to access the document
 To write a script that validates an XML document
  against the DTD, you need to follow the steps given
  below:
     Create the user interface for accepting the XML file
      name.
     Write the script to load the named XML document.
     Write the script to validate the XML document
      against the DTD.




©NIIT                  eXtensible Markup Language/Lesson 7/Slide 39 of 43
Working with the XML Document Object Model


Task 4: Execute the script




©NIIT              eXtensible Markup Language/Lesson 7/Slide 40 of 43
Working with the XML Document Object Model


Just a Minute…
 The details about products sold at CyberShoppe are
  stored in an XML document called product.xml. Write
  the code to display the price of all products by using
  DOM objects.




©NIIT                 eXtensible Markup Language/Lesson 7/Slide 41 of 43
Working with the XML Document Object Model


Summary
In this lesson, you learned that:
 DOM is an application programming interface that
  enables an application to access the contents of an
  XML document.
 DOM objects enable you to access and manipulate
  XML documents.
 When the MSXML parser loads an XML document, it
  creates a tree structure that represents the various
  components of the XML document.




©NIIT                  eXtensible Markup Language/Lesson 7/Slide 42 of 43
Working with the XML Document Object Model


Summary (Contd.)
 The basic building block of the tree structure is a
  node. A node is a container that holds information
  about the elements, attributes, and content stored in
  an XML document.
 Some XML DOM objects that are used to manipulate
  data in a document are:
     DOMDocument
     IXMLDOMNode
     IXMLDOMNodeList
     IXMLDOMParseError

©NIIT                 eXtensible Markup Language/Lesson 7/Slide 43 of 43
Working with the XML Document Object Model


Summary (Contd.)
 The DOMDocument object is the top-level object in
  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 IXMLDOMNode object represents a node in the
  XML document structure. This object provides
  methods to work with child elements.
 The IXMLDOMNodeList object enables you to iterate
  through a collection of nodes.
 The IXMLDOMParseError object returns information
  about the most recent error.

©NIIT               eXtensible Markup Language/Lesson 7/Slide 44 of 43

Weitere ähnliche Inhalte

Was ist angesagt?

Object oriented database model
Object oriented database modelObject oriented database model
Object oriented database modelPAQUIAAIZEL
 
Islamic University Previous Year Question Solution 2018 (ADBMS)
Islamic University Previous Year Question Solution 2018 (ADBMS)Islamic University Previous Year Question Solution 2018 (ADBMS)
Islamic University Previous Year Question Solution 2018 (ADBMS)Rakibul Hasan Pranto
 
9. Object Relational Databases in DBMS
9. Object Relational Databases in DBMS9. Object Relational Databases in DBMS
9. Object Relational Databases in DBMSkoolkampus
 
Oracle soa xml faq
Oracle soa xml faqOracle soa xml faq
Oracle soa xml faqxavier john
 
Xml viva questions
Xml viva questionsXml viva questions
Xml viva questionsVipul Naik
 
Building XML Based Applications
Building XML Based ApplicationsBuilding XML Based Applications
Building XML Based ApplicationsPrabu U
 
Overview of Object-Oriented Concepts Characteristics by vikas jagtap
Overview of Object-Oriented Concepts Characteristics by vikas jagtapOverview of Object-Oriented Concepts Characteristics by vikas jagtap
Overview of Object-Oriented Concepts Characteristics by vikas jagtapVikas Jagtap
 
08. Object Oriented Database in DBMS
08. Object Oriented Database in DBMS08. Object Oriented Database in DBMS
08. Object Oriented Database in DBMSkoolkampus
 
Object Oriented Dbms
Object Oriented DbmsObject Oriented Dbms
Object Oriented Dbmsmaryeem
 
Database Design and Normalization Techniques
Database Design and Normalization TechniquesDatabase Design and Normalization Techniques
Database Design and Normalization TechniquesNishant Munjal
 
Module 5 oodb systems semantic db systems
Module 5 oodb systems  semantic db systemsModule 5 oodb systems  semantic db systems
Module 5 oodb systems semantic db systemsTaher Barodawala
 
PBDJ 19-4(woolley rev)
PBDJ 19-4(woolley rev)PBDJ 19-4(woolley rev)
PBDJ 19-4(woolley rev)Buck Woolley
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structuresKrishna Nanda
 

Was ist angesagt? (20)

Chapt 1 odbms
Chapt 1 odbmsChapt 1 odbms
Chapt 1 odbms
 
Structures
StructuresStructures
Structures
 
03 x files
03 x files03 x files
03 x files
 
Object oriented data model
Object oriented data modelObject oriented data model
Object oriented data model
 
Object oriented database model
Object oriented database modelObject oriented database model
Object oriented database model
 
Islamic University Previous Year Question Solution 2018 (ADBMS)
Islamic University Previous Year Question Solution 2018 (ADBMS)Islamic University Previous Year Question Solution 2018 (ADBMS)
Islamic University Previous Year Question Solution 2018 (ADBMS)
 
9. Object Relational Databases in DBMS
9. Object Relational Databases in DBMS9. Object Relational Databases in DBMS
9. Object Relational Databases in DBMS
 
Oracle soa xml faq
Oracle soa xml faqOracle soa xml faq
Oracle soa xml faq
 
Xml viva questions
Xml viva questionsXml viva questions
Xml viva questions
 
Building XML Based Applications
Building XML Based ApplicationsBuilding XML Based Applications
Building XML Based Applications
 
12 SQL
12 SQL12 SQL
12 SQL
 
Overview of Object-Oriented Concepts Characteristics by vikas jagtap
Overview of Object-Oriented Concepts Characteristics by vikas jagtapOverview of Object-Oriented Concepts Characteristics by vikas jagtap
Overview of Object-Oriented Concepts Characteristics by vikas jagtap
 
08. Object Oriented Database in DBMS
08. Object Oriented Database in DBMS08. Object Oriented Database in DBMS
08. Object Oriented Database in DBMS
 
Object Oriented Dbms
Object Oriented DbmsObject Oriented Dbms
Object Oriented Dbms
 
Database Design and Normalization Techniques
Database Design and Normalization TechniquesDatabase Design and Normalization Techniques
Database Design and Normalization Techniques
 
Module 5 oodb systems semantic db systems
Module 5 oodb systems  semantic db systemsModule 5 oodb systems  semantic db systems
Module 5 oodb systems semantic db systems
 
PBDJ 19-4(woolley rev)
PBDJ 19-4(woolley rev)PBDJ 19-4(woolley rev)
PBDJ 19-4(woolley rev)
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structures
 
Unit 5 xml (1)
Unit 5   xml (1)Unit 5   xml (1)
Unit 5 xml (1)
 
02 xml schema
02 xml schema02 xml schema
02 xml schema
 

Ähnlich wie 04 sm3 xml_xp_07

04 sm3 xml_xp_08
04 sm3 xml_xp_0804 sm3 xml_xp_08
04 sm3 xml_xp_08Niit Care
 
Ado.net session13
Ado.net session13Ado.net session13
Ado.net session13Niit Care
 
Working with xml data
Working with xml dataWorking with xml data
Working with xml dataaspnet123
 
Understanding Dom
Understanding DomUnderstanding Dom
Understanding DomLiquidHub
 
java API for XML DOM
java API for XML DOMjava API for XML DOM
java API for XML DOMSurinder Kaur
 
Creating xml publisher documents with people code
Creating xml publisher documents with people codeCreating xml publisher documents with people code
Creating xml publisher documents with people codeRandall Groncki
 
buildingxmlbasedapplications-180322042009.pptx
buildingxmlbasedapplications-180322042009.pptxbuildingxmlbasedapplications-180322042009.pptx
buildingxmlbasedapplications-180322042009.pptxNKannanCSE
 
Ch23 xml processing_with_java
Ch23 xml processing_with_javaCh23 xml processing_with_java
Ch23 xml processing_with_javaardnetij
 
Document object model
Document object modelDocument object model
Document object modelAmit kumar
 
Working with XML and JSON Serializing
Working with XML and JSON SerializingWorking with XML and JSON Serializing
Working with XML and JSON Serializingssusere19c741
 
Xml and xml processor
Xml and xml processorXml and xml processor
Xml and xml processorHimanshu Soni
 
Xml and xml processor
Xml and xml processorXml and xml processor
Xml and xml processorHimanshu Soni
 
Applied xml programming for microsoft
Applied xml programming for microsoftApplied xml programming for microsoft
Applied xml programming for microsoftRaghu nath
 

Ähnlich wie 04 sm3 xml_xp_07 (20)

04 sm3 xml_xp_08
04 sm3 xml_xp_0804 sm3 xml_xp_08
04 sm3 xml_xp_08
 
The xml
The xmlThe xml
The xml
 
Ado.net session13
Ado.net session13Ado.net session13
Ado.net session13
 
Working with xml data
Working with xml dataWorking with xml data
Working with xml data
 
Understanding Dom
Understanding DomUnderstanding Dom
Understanding Dom
 
java API for XML DOM
java API for XML DOMjava API for XML DOM
java API for XML DOM
 
Dom
Dom Dom
Dom
 
Creating xml publisher documents with people code
Creating xml publisher documents with people codeCreating xml publisher documents with people code
Creating xml publisher documents with people code
 
Session 5
Session 5Session 5
Session 5
 
buildingxmlbasedapplications-180322042009.pptx
buildingxmlbasedapplications-180322042009.pptxbuildingxmlbasedapplications-180322042009.pptx
buildingxmlbasedapplications-180322042009.pptx
 
Ch23
Ch23Ch23
Ch23
 
Ch23 xml processing_with_java
Ch23 xml processing_with_javaCh23 xml processing_with_java
Ch23 xml processing_with_java
 
Document object model
Document object modelDocument object model
Document object model
 
Understanding XML DOM
Understanding XML DOMUnderstanding XML DOM
Understanding XML DOM
 
Working with XML and JSON Serializing
Working with XML and JSON SerializingWorking with XML and JSON Serializing
Working with XML and JSON Serializing
 
Xml and xml processor
Xml and xml processorXml and xml processor
Xml and xml processor
 
Xml and xml processor
Xml and xml processorXml and xml processor
Xml and xml processor
 
Xml writers
Xml writersXml writers
Xml writers
 
Full xml
Full xmlFull xml
Full xml
 
Applied xml programming for microsoft
Applied xml programming for microsoftApplied xml programming for microsoft
Applied xml programming for microsoft
 

Mehr von Niit Care (20)

Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 b
 
Ajs 4 b
Ajs 4 bAjs 4 b
Ajs 4 b
 
Ajs 4 a
Ajs 4 aAjs 4 a
Ajs 4 a
 
Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 c
 
Ajs 3 b
Ajs 3 bAjs 3 b
Ajs 3 b
 
Ajs 3 a
Ajs 3 aAjs 3 a
Ajs 3 a
 
Ajs 3 c
Ajs 3 cAjs 3 c
Ajs 3 c
 
Ajs 2 b
Ajs 2 bAjs 2 b
Ajs 2 b
 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 a
 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 c
 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 a
 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 c
 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-b
 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-c
 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-b
 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 

Kürzlich hochgeladen

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 

Kürzlich hochgeladen (20)

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 

04 sm3 xml_xp_07

  • 1. Working with the XML Document Object Model Objectives In this lesson, you will learn to: Identify the need for XML Document Object Model (DOM) Use the following XML DOM objects to access different parts of an XML document: DOMDocument IXMLDOMNode IXMLDOMNodeList IXMLDOMParseError ©NIIT eXtensible Markup Language/Lesson 7/Slide 1 of 43
  • 2. Working with the XML Document Object Model The XML Document Object Model The Document Object Model (DOM) defines the logical structure of documents and the way a document is accessed and manipulated. It provides an Application Programming Interface (API) for XML and HTML documents. XML DOM views an XML document as being composed of objects. Each object has properties and behavior that can be manipulated using the methods provided by a DOM interface. ©NIIT eXtensible Markup Language/Lesson 7/Slide 2 of 43
  • 3. Working with the XML Document Object Model The XML Document Object Model (Contd.) Using XML DOM, programmers can create and build XML documents, navigate the structure of documents, and add, modify, or delete elements and their content. The objects and methods provided by XML DOM can be used with any programming language, such as Java, C++, and Visual Basic. These objects can also be used with scripting languages, such as VBScript and JavaScript. To be able to use the features of XML DOM, you need to use a DOM-enabled parser. ©NIIT eXtensible Markup Language/Lesson 7/Slide 3 of 43
  • 4. Working with the XML Document Object Model The XML Document Object Model (Contd.) A DOM-enabled parser reads an XML document and parses it to ensure that it is a valid document. Then, it creates an in-memory representation of the XML document by organizing its contents in a logical tree- structure. The tree-structure is made up of nodes. MSXML is an example of a DOM-enabled XML parser. ©NIIT eXtensible Markup Language/Lesson 7/Slide 4 of 43
  • 5. Working with the XML Document Object Model Implementation of DOM in the MSXML Parser When the MSXML parser loads an XML document, it reads the document and creates a tree structure that represents the various components of the XML document. ©NIIT eXtensible Markup Language/Lesson 7/Slide 5 of 43
  • 6. Working with the XML Document Object Model Implementation of DOM in the MSXML Parser (Contd.) The following diagram depicts how DOM allows applications to access information stored in XML documents: MSXML Library XML DOM Tree Document Parser Root Application Parsed Child Document text Child text ©NIIT eXtensible Markup Language/Lesson 7/Slide 6 of 43
  • 7. Working with the XML Document Object Model Tree structure of a Document The basic building block of the tree structure is the node. Nodes are generic containers that hold information about the elements, attributes, content, comments, and processing instructions that are stored in an XML document. The XML document can be viewed as a single node that contains all the other nodes. ©NIIT eXtensible Markup Language/Lesson 7/Slide 7 of 43
  • 8. Working with the XML Document Object Model Tree structure of a Document The root element of the XML document is represented as the root node of the corresponding DOM tree‑structure. Every component of the XML document is represented as a node in the DOM tree-structure. Each node has a name, type, and value associated with it. The name of the node is the name of the component with which the node is associated. The type of the node depends on the type of the component it represents. DOM treats each of these nodes as objects. Therefore, it is possible to create a script that loads an XML document, traverses through all nodes, and displays the required information to the user. ©NIIT eXtensible Markup Language/Lesson 7/Slide 8 of 43
  • 9. Working with the XML Document Object Model XML DOM Objects and Methods The main objects and methods provided by XML DOM that enable you to traverse, read, and manipulate the structure and content of an XML document are listed below: The DOMDocument object The IXMLDOMNode object The IXMLDOMNodeList object The IXMLDOMParseError object ©NIIT eXtensible Markup Language/Lesson 7/Slide 9 of 43
  • 10. Working with the XML Document Object Model The DOMDocument object The DOMDocument object is the top-level object in XML DOM. It implements all the basic DOM methods required to work with an XML document. It also has methods, which support XSLT. This object is associated with various methods that help you navigate, query, and modify the content and the structure of an XML document. ©NIIT eXtensible Markup Language/Lesson 7/Slide 10 of 43
  • 11. Working with the XML Document Object Model The DOMDocument object (Contd.) Some of the most commonly used methods of the DOMDocument object are: createElement(elementname): Creates an element node. createAttribute(attributename): Creates an attribute node. CreateNode(type, name, namespace-URI): Creates a node of the specified type. Also supports namespace URI. getElementsByTagName(elementname): Returns a collection of element nodes that have the specified element name. ©NIIT eXtensible Markup Language/Lesson 7/Slide 11 of 43
  • 12. Working with the XML Document Object Model The DOMDocument object (Contd.) load(XMLdocument): Loads the specified XML document. appendChild(child node): Appends the specified child node to the document. save(destination): Saves the XML document represented by the DOMDocument object at the specified destination. Some of the commonly used properties of the DOMDocument object are: async: Allows you to specify whether the XML document can be loaded asynchronously. childNodes: Returns a list of child nodes. firstChild: Returns the first child in the document. ©NIIT eXtensible Markup Language/Lesson 7/Slide 12 of 43
  • 13. Working with the XML Document Object Model The DOMDocument object (Contd.) readyState: Returns the information about the state of the XML document (loading, loaded, interactive, complete). parseError: Returns an object of IXMLDOMParseError, which can be used to retrieve error information. xml: Returns the XML representation of a node. validateOnParse: Specifies whether the parser should check for validity of an XML document. ©NIIT eXtensible Markup Language/Lesson 7/Slide 13 of 43
  • 14. Working with the XML Document Object Model Creating a DomDocument object The following is the code segment written using JavaScript to create an instance of the DOMDocument object: var myxmldoc = new ActiveXObject(Msxml2.DOMDocument.4.0); A reference to the newly created object is stored in the variable myxmldoc, which can be used to load and manipulate XML documents. ©NIIT eXtensible Markup Language/Lesson 7/Slide 14 of 43
  • 15. Working with the XML Document Object Model Loading an XML file To load an XML file by using a URL, you can write the following code in JavaScript : myxmldoc.load(http://www.sb.com/ employee.xml); The load() method can also be used to load a file from the local host by providing the path, as shown in the following example: myxmldoc.load(c:examplesemployees.xml ); ©NIIT eXtensible Markup Language/Lesson 7/Slide 15 of 43
  • 16. Working with the XML Document Object Model Loading an XML file (Contd.) To load an XML document synchronously, you can write the following code in JavaScript: var myxmldoc = new ActiveXObject(Msxml2.DOMDocument.4.0); myxmldoc.async=false; myxmldoc.load(employee.xml); ©NIIT eXtensible Markup Language/Lesson 7/Slide 16 of 43
  • 17. Working with the XML Document Object Model Using the readyState Property To check if the document has been loaded completely, you can use the readyState property. The readyState property holds a numeric value, which represents one of the following states: LOADING (1): This state indicates that the loading process is in progress and data is not yet parsed. LOADED (2): This state indicates that data has been read and parsed but the object model is not yet ready. ©NIIT eXtensible Markup Language/Lesson 7/Slide 17 of 43
  • 18. Working with the XML Document Object Model Using the readyState Property (Contd.) INTERACTIVE (3): This state indicates that the object model is available with partially retrieved data set and is in the read-only mode. COMPLETED (4): This state indicates that the loading process is complete. ©NIIT eXtensible Markup Language/Lesson 7/Slide 18 of 43
  • 19. Working with the XML Document Object Model Creating a New Node You can create a new node by using the createNode() method. The following is the code segment to create a root element by using the createNode() method: var myxmldoc = new ActiveXObject(Msxml2.DOMDocument.4.0); if (myxmldoc.childnodes.length==0) { myroot=myxmldoc.createNode(1,ORDERDETAI LS, ); myxmldoc.appendChild(myroot); myxmldoc.save(myxmldoc.XMLDocument); } ©NIIT eXtensible Markup Language/Lesson 7/Slide 19 of 43
  • 20. Working with the XML Document Object Model Creating New Elements You can create new elements for the document by using the createElement() method of the myxmldoc DOMDocument object. The createElement() method takes one parameter, the name of the element that is to be created. The following example creates a new element node called “salary”: var xmlelement; xmlelement=myxmldoc.createElement (salary); ©NIIT eXtensible Markup Language/Lesson 7/Slide 20 of 43
  • 21. Working with the XML Document Object Model Accessing Elements From an XML File To access the elements from an XML document, you can begin at the root element and navigate through the document tree. You can also query for a specific node or nodes. To reach a particular node or nodes within the document tree structure, you can use the getElementsByTagName() method. ©NIIT eXtensible Markup Language/Lesson 7/Slide 21 of 43
  • 22. Working with the XML Document Object Model The IXMLDOMNode object The IXMLDOMNode object represents a node in the XML document tree structure. The node could be an element that contains child elements. The IXMLDOMNode object provides methods to work with child elements. Some commonly used methods of the IXMLDOMNode object are: appendChild(newchild): Appends a new child to the node. insertBefore(newNode, refNode): Inserts a new node before the specified node. removeChild(nodeName): Removes the specified node. ©NIIT eXtensible Markup Language/Lesson 7/Slide 22 of 43
  • 23. Working with the XML Document Object Model The IXMLDOMNode object (Contd.) replaceChild(newNode, oldNode): Replaces the oldNode with newNode. hasChildNodes(): Specifies whether the node has child nodes. cloneNode(boolean): Creates a clone of the node represented by the IXMLDOMNode object. Some of the commonly used properties of the IXMLDOMNode object are: nodeName: Returns the name of the node. nodeType: Specifies the type of the node. nodeValue: Returns the text contained in the node. childNodes: Returns the child nodes of the node. ©NIIT eXtensible Markup Language/Lesson 7/Slide 23 of 43
  • 24. Working with the XML Document Object Model The IXMLDOMNode object (Contd.) firstChild: Returns the first child of the node. lastChild: Returns the last child of the node. text: Returns the text contained in the node. xml: Returns the XML code for the node. ©NIIT eXtensible Markup Language/Lesson 7/Slide 24 of 43
  • 25. Working with the XML Document Object Model Accessing Text Values of Elements The text enclosed within the tags in an XML file is used as a node value in DOM. A node value can be the value of an attribute or the text within an element. You can display the text within the elements or attributes by using the text property, as shown below: alert(myelement.text); You can also set the value of an element by using this property, as shown in the example given below: myelement.text=“Barbie Doll”; ©NIIT eXtensible Markup Language/Lesson 7/Slide 25 of 43
  • 26. Working with the XML Document Object Model Appending a New Child Node The following code segment creates a new element by using the createElement() method of the DOMDocument object. Then, it appends the newly created node to the last child of myelement by using the appendchild() method: var myelement=myxmldoc.documentElement; var newElement=myxmldoc.createElement (lastchild); myelement.appendChild(newElement); ©NIIT eXtensible Markup Language/Lesson 7/Slide 26 of 43
  • 27. Working with the XML Document Object Model Inserting a Node Before Two Existing Nodes The following code segment creates a new element called department and replaces and existing node with the new element: var newElement= myxmldoc.createElement (department); var oldElement=myxmldoc.documentElement.chil dNodes.item(0).firstChild; myxmldoc.documentElement.childNodes.item (1).insertBefore(newElement, oldElement); ©NIIT eXtensible Markup Language/Lesson 7/Slide 27 of 43
  • 28. Working with the XML Document Object Model Removing a Child Node The following code segment removes a child node by using the removeChild() method: var oldElement=myxmldoc.documentElement.chi ldNodes.item(0).firstChild; myxmldoc.documentElement.childNodes.ite m(0).RemoveChild(oldElement); ©NIIT eXtensible Markup Language/Lesson 7/Slide 28 of 43
  • 29. Working with the XML Document Object Model Replacing a Node (Contd.) In the following example, the second element in the document is replaced with the new element named department: Var newElement= myxmldoc.createElement(department); Var oldElement=myxmldoc.documentElement.chil dNodes.item(0).firstChild; myxmldoc.documentElement.childNodes.item(1 ).replaceChild(newElement, oldElement); ©NIIT eXtensible Markup Language/Lesson 7/Slide 29 of 43
  • 30. Working with the XML Document Object Model The IXMLDOMNodeList object The IXMLDOMNodeList object enables you to iterate through a collection of nodes. Some methods of the IXMLDOMNodeList object are: item(number):Returns the node at the position indicated by the specified number. nextNode(): Returns the next node in the list. To obtain the length of the node list, use the length property. The length property can also be used to traverse through the list of child nodes. ©NIIT eXtensible Markup Language/Lesson 7/Slide 30 of 43
  • 31. Working with the XML Document Object Model The IXMLDOMNodeList object (Contd.) The following code traverses through the child nodes of myelement: var myelement=myxmlDoc.getElementsByTagName( emp); for(i=0;i myelement.length –1;i++) alert(myelement.item(i).xml); ©NIIT eXtensible Markup Language/Lesson 7/Slide 31 of 43
  • 32. Working with the XML Document Object Model The IXMLDOMParseError Object The IXMLDOMParseError object returns information about the most recent parse error. The IXMLDOMParseError object provides properties to retrieve information, such as the error code, the error text, and the line that caused the error. ©NIIT eXtensible Markup Language/Lesson 7/Slide 32 of 43
  • 33. Working with the XML Document Object Model The IXMLDOMParseError Object (Contd.) The properties of the IXMLDOMParseError object are: errorCode: Returns the error code. reason: Returns a string explaining the reason for the error. line: Returns the line number where the error occurred. linePos: Returns the position in the line where the error occurred. srcText: Returns a string containing the line that caused the error. ©NIIT eXtensible Markup Language/Lesson 7/Slide 33 of 43
  • 34. Working with the XML Document Object Model Using the IXMLDOMParseError object You can use the IXMLDOMParseError object to display information about the errors that are encountered while parsing an XML document. Consider the following example: var prodxml = new ActiveXObject(Msxml2.DOMDocument.4.0); prodxml.async = false; prodxml.load(product.xml); if (prodxml.parseError.errorCode != 0) { alert(A parse error occurred.);} else {alert(prodxml.documentElement.xml);} ©NIIT eXtensible Markup Language/Lesson 7/Slide 34 of 43
  • 35. Working with the XML Document Object Model Problem Statement 7.D.1 The product details of CyberShoppe are stored in an XML document. The structure of the XML document is defined in a DTD. The data held in the XML document must be validated against the rules specified for the data store. ©NIIT eXtensible Markup Language/Lesson 7/Slide 35 of 43
  • 36. Working with the XML Document Object Model Task List Identify a mechanism to access an XML document programmatically. Identify the objects required to access the XML document. Write the code to access the document. Execute the code. ©NIIT eXtensible Markup Language/Lesson 7/Slide 36 of 43
  • 37. Working with the XML Document Object Model Task 1: Identify a mechanism to access an XML document programmatically. Result: The contents of an XML document can be accessed and manipulated from any application by using XML DOM. Therefore, to access an XML document programmatically, the best solution is to use XML DOM. Applications developed using VBScript, JavaScript, C/C++, or Visual Basic can use XML DOM objects. ©NIIT eXtensible Markup Language/Lesson 7/Slide 37 of 43
  • 38. Working with the XML Document Object Model Task 2: Identify the objects required to access the XML document. Result: You can validate the contents of an XML document against a DTD by using the following XML DOM objects: DOMDocument IXMLDOMParseError ©NIIT eXtensible Markup Language/Lesson 7/Slide 38 of 43
  • 39. Working with the XML Document Object Model Task 3: Write the code to access the document To write a script that validates an XML document against the DTD, you need to follow the steps given below: Create the user interface for accepting the XML file name. Write the script to load the named XML document. Write the script to validate the XML document against the DTD. ©NIIT eXtensible Markup Language/Lesson 7/Slide 39 of 43
  • 40. Working with the XML Document Object Model Task 4: Execute the script ©NIIT eXtensible Markup Language/Lesson 7/Slide 40 of 43
  • 41. Working with the XML Document Object Model Just a Minute… The details about products sold at CyberShoppe are stored in an XML document called product.xml. Write the code to display the price of all products by using DOM objects. ©NIIT eXtensible Markup Language/Lesson 7/Slide 41 of 43
  • 42. Working with the XML Document Object Model Summary In this lesson, you learned that: DOM is an application programming interface that enables an application to access the contents of an XML document. DOM objects enable you to access and manipulate XML documents. When the MSXML parser loads an XML document, it creates a tree structure that represents the various components of the XML document. ©NIIT eXtensible Markup Language/Lesson 7/Slide 42 of 43
  • 43. Working with the XML Document Object Model Summary (Contd.) The basic building block of the tree structure is a node. A node is a container that holds information about the elements, attributes, and content stored in an XML document. Some XML DOM objects that are used to manipulate data in a document are: DOMDocument IXMLDOMNode IXMLDOMNodeList IXMLDOMParseError ©NIIT eXtensible Markup Language/Lesson 7/Slide 43 of 43
  • 44. Working with the XML Document Object Model Summary (Contd.) The DOMDocument object is the top-level object in 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 IXMLDOMNode object represents a node in the XML document structure. This object provides methods to work with child elements. The IXMLDOMNodeList object enables you to iterate through a collection of nodes. The IXMLDOMParseError object returns information about the most recent error. ©NIIT eXtensible Markup Language/Lesson 7/Slide 44 of 43