two types of xml parsers
TRANSCRIPT
8/19/2019 Two Types of XML Parsers
http://slidepdf.com/reader/full/two-types-of-xml-parsers 1/6
Two types of XML parsers:
Validating Parser
• It requires document type declaration
• It generates error if document does not
o Conform with DTD and
o Meet XML validity constraints Non-validating Parser
• It checs well!formedness for "ml document• It can ignore e"ternal DTD
What is XML Parser?
XML Parser provides way how to access or modify data present in an XML document# $ava
provides multiple options to parse XML document# %ollowing are various types of parsers
which are commonly used to parse XML documents#
Types of parsers:
• Dom Parser - Parses the document &y loading the complete contents of the document
and creating its complete hiearchical tree in memory#
• !X Parser - Parses the document on event &ased triggers# Does not load the complete
document into the memory#
• "D#M Parser - Parses the document in similar fashion to D'M parser &ut in more
easier way#
• t!X Parser - Parses the document in similar fashion to ()X parser &ut in more
efficient way#
• XPath Parser ! Parses the XML &ased on e"pression and is used e"tensively in
con*uction with X(LT#
• D#M$" Parser ! ) *ava li&rary to parse XML+ XPath and X(LT using $ava Collections
%ramewor + provides support for D'M+ ()X and $)XP#
D#M-Do%&ment #'(e%t ModelThe Document '&*ect Model protocol converts an XML document into a collection of o&*ects in
your program# XML documents have a hierarchy of informational units called nodes, thishierarchy allows a developer to navigate through the tree looing for specific information#
-ecause it is &ased on a hierarchy of information+ the D'M is said to &e tree &ased# D'M is a
way of descri&ing those nodes and the relationships &etween them#
8/19/2019 Two Types of XML Parsers
http://slidepdf.com/reader/full/two-types-of-xml-parsers 2/6
.ou can then manipulate the o&*ect model in any way that maes sense# This mechanism is also
nown as the /random access/ protocol+ &ecause you can visit any part of the data at any time#
.ou can then modify the data+ remove it+ or insert new data#
The XML D'M+ on the other hand+ also provides an )PI that allows a developer to add+ edit+
move+ or remove nodes in the tree at any point in order to create an application# ) D'M parser
creates a tree structure in memory from the input document and then waits for requests fromclient# ) D'M parser always serves the client application with the entire do%&ment no matter
how m&%h is a%t&ally needed &y the client# 0ith D'M parser+ method calls in client application
have to &e e"plicit and forms a ind of chained method calls#Document '&*ect Model is for defining the standard for accessing and manipulating XML
documents# XML D#M is used for
• Loading the "ml document
• )ccessing the "ml document
• Deleting the elements of "ml document
• Changing the elements of "ml document
)ccording to the D'M+ everything in an XML document is a node# It considers
• The entire document is a document node
• 1very XML element is an element node
• The te"t in the XML elements are te"t nodes
• 1very attri&ute is an attri&ute node
• Comments are comment nodes
D#M Levels
• Level 2 Core3 04C 5ecommendation+ 'cto&er 2667
It has feature for primitive navigation and manipulation of XML trees
other Level 2 features are3 )ll 8TML features• Level 9 Core3 04C 5ecommendation+ :ovem&er 9;;;
It adds :amespace support and minor new features
other Level 9 features are3 1vents+ <iews+ (tyle+ Traversal and 5ange
• Level 4 Core3 04C 0oring Draft+ )pril 9;;9
It supports3 (chemas+ XPath+ X(L+ X(LT
0e can access and parse the XML document in two ways3
Parsing&sing D#M )tree 'ased*
Parsing &sing !X )+vent 'ased*
Parsing the XML doc# using D'M methods and properties are called as tree 'ased approa%h
whereas using ()X =(imple )pi for Xml> methods and properties are called as event 'asedapproa%h,
8/19/2019 Two Types of XML Parsers
http://slidepdf.com/reader/full/two-types-of-xml-parsers 3/6
teps to sing D#M Parser
Let?s note down some &road steps involved in using a D'M parser for parsing any XML file in *ava#
DOM based XML Parsing:(tree based)
$)XP is a tool+ stands for $ava )pi for Xml Processing+ used for accessing and manipulating "ml
document in a tree &ased manner#
The following D'M *avaClasses are necessary to process the XML document3
• Document-uilder%actory class creates the instance of Document-uilder#
• Document-uilder produces a Document =a D'M> that conforms to the D'M specification#
The following methods and properties are necessary to process the XML document3
Property Meaning
node:ame %inding the name of the node
node<alue '&taining value of the node parent:ode To get parnet node
child:odes '&tain child nodes
attri&utes %or getting the attri&utes values
Method Meaning
get1lement-yTag:ame=name> To access the element &y specifying its name
appendChild=node> To insert a child node
removeChild=node> To remove e"isting child node
Loading an XML file:one,html
@htmlA@&odyA
@script typeBte"t*avascriptA
try
E
"mlDocumentBnew
)ctiveX'&*ect=FMicrosoft#XMLD'M>,
G
catch=e>E
try
E
"mlDocumentBdocument#implementation#cre
ateDocument=//+//+null>,
G
8/19/2019 Two Types of XML Parsers
http://slidepdf.com/reader/full/two-types-of-xml-parsers 4/6
catch=e>Ealert=e#message>G
G
try
E
"mlDocument#asyncBfalse,
"mlDocument#load=Ffaculty#"ml>,
document#write=FXML document student is
loaded>,
G
catch=e>Ealert=e#message>G
@scriptA
@&odyA
@htmlA
fa%<y,.ml:
@H"ml versionB2#;HA@ faculty A
@enoA4;@enoA
@personalinfA
@nameAJalpana@nameA
@addressAhyd@addressA
@phoneA6KN492;@phoneA
@personalinfA
@deptAcse@deptA
@colAmrcet@colA
@groupAmrgi@groupA@facultyA
!%tiveX#'(e%t: It creates empty "ml
document o&*ect#
se separate f&n%tion for Loading an
XML do%&ment: two,html
@htmlA
@headA
@script typeBte"t*avascriptA
%unction Myfunction=docfile>
E
try
E
"mlDocumentBnew
)ctiveX'&*ect=FMicrosoft#XMLD'M>,
G
catch=e>
E
try
E
"mlDocumentBdocument#implementation#cre
ateDocument=//+//+null>,
G
catch=e>Ealert=e#message>G
Gtry
E
"mlDocument#asyncBfalse,
"mlDocument#load=Ffaculty#"ml>,
return="mlDocument>,
G
catch=e>Ealert=e#message>G
return=null>,
G
@scriptA@headA
@&odyA
@script typeBte"t*avascriptA
"mlDocBMyfunction=Ffaculty#"ml>,
document#write=FXML document student is
loaded>,
@scriptA
@&odyA
@htmlA
Use of properties and methods
Three.html
@htmlA @headA
@script typeBte"t*avascript
srcBmyfunctionfile#*sA@scriptA
@headA @&odyA
@script typeBte"t*avascriptA
.mlDo%&ment/My0f&n%tion)1fa%<y,.ml2
*3
document#write=FXML document faculty is
loaded and content of this file is3>,
8/19/2019 Two Types of XML Parsers
http://slidepdf.com/reader/full/two-types-of-xml-parsers 5/6
document#write=F@&rA>,
document#write=F1:'3O
"mlDocument#get1lements-yTag:ame=Feno
>;Q#child:odes;Q#node<alue>,
document#write=F@&rA>,
document#write=F:ame3O
"mlDocument#get1lements-yTag:ame=Fna
me>;Q#child:odes;Q#node<alue>,
document#write=F@&rA>,
document#write=F)DD51((3O
"mlDocument#get1lements-yTag:ame=Fadd
ress>;Q#child:odes;Q#node<alue>,
document#write=F@&rA>,
document#write=F5:'3O
"mlDocument#get1lements-yTag:ame=Frno
>;Q#child:odes;Q#node<alue>,
! ! ! !! ! ! ! !! ! ! ! !! ! !
@scriptA
@&odyA
@htmlA
8/19/2019 Two Types of XML Parsers
http://slidepdf.com/reader/full/two-types-of-xml-parsers 6/6
We can access any XML element using the index value.
@htmlA @headA
@script typeBte"t*avascript srcBmyfunctionfile#*sA@scriptA
@headA @&odyA
@script typeBte"t*avascriptA
.mlDo%/My0f&n%tion)1fa%<y4,.ml2*3
valueB"mlDoc# get1lements-yTag:ame=Fname>,
document#write=Fvalue2Q#child:odes;Q#node<alue>,
@scriptA@&odyA@htmlA 5a%<y4,.ml:-----