SlideShare une entreprise Scribd logo
1  sur  19
DOM Parser
By
Sana Mateen
DOM
• The Document Object Model is an official recommendation of the
World Wide Web Consortium (W3C). It defines an interface that
enables programs to access and update the style, structure,and
contents of XML documents. XML parsers that support the DOM
implement that interface.
• When to use?
• You should use a DOM parser when:
• You need to know a lot about the structure of a document
• You need to move parts of the document around (you might want to
sort certain elements, for example)
• You need to use the information in the document more than once
• What you get?
• When you parse an XML document with a DOM parser, you get
back a tree structure that contains all of the elements of your
document. The DOM provides a variety of functions you can use to
examine the contents and structure of the document.
Common DOM methods
• When you are working with the DOM, there are several methods you'll use
often:
• Document.getDocumentElement() - Returns the root element of the
document.
• Node.getFirstChild() - Returns the first child of a given Node.
• Node.getLastChild() - Returns the last child of a given Node.
• Node.getNextSibling() - These methods return the next sibling of a given
Node.
• Node.getPreviousSibling() - These methods return the previous sibling of
a given Node.
• Node.getAttribute(attrName) - For a given Node, returns the attribute
with the requested name.
Steps to Using DOM
• Following are the steps used while parsing a document using DOM Parser.
• Import XML-related packages.
• Create a DocumentBuilder
• Create a Document from a file or stream
• Extract the root element
• Examine attributes
• Examine sub-elements
• Import XML-related packages
• import org.w3c.dom.*;
• import javax.xml.parsers.*;
• import java.io.*;
• Create a DocumentBuilder
• DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
• DocumentBuilder builder = factory.newDocumentBuilder();
• Create a Document from a file or stream
• StringBuilder xmlStringBuilder = new StringBuilder(); xmlStringBuilder.append("<?xml
version="1.0"?> <class> </class>"); ByteArrayInputStream input = new
ByteArrayInputStream( xmlStringBuilder.toString().getBytes("UTF-8"));
• Document doc = builder.parse(input);
• Extract the root element
• Element root = document.getDocumentElement();
• Examine attributes
• //returns specific attribute
• getAttribute("attributeName");
• //returns a (table) of names/values
• getAttributes();
• Examine sub-elements
• //returns a list of subelements of specified name
getElementsByTagName("subelementName"); /
• /returns a list of all child nodes
• getChildNodes();
SAX PARSER
•SAX (the Simple API for XML) is an event-based parser for xml
documents.
• Unlike a DOM parser, a SAX parser creates no parse tree. SAX is a
streaming interface for XML, which means that applications using SAX receive
event notifications about the XML document being processed an element, and
attribute, at a time in sequential order starting at the top of the document, and
ending with the closing of the ROOT element.
SAX
• Reads an XML document from top to bottom, recognizing the tokens that
make up a well-formed XML document
• Tokens are processed in the same order that they appear in the document
• Reports the application program the nature of tokens that the parser has
encountered as they occur
• The application program provides an "event" handler that must be
registered with the parser
• As the tokens are identified, callback methods in the handler are invoked
with the relevant information
When to use?
• You should use a SAX parser when:
• You can process the XML document in a linear fashion from the top down
• The document is not deeply nested
• You are processing a very large XML document whose DOM tree would
consume too much memory.Typical DOM implementations use ten bytes of
memory to represent one byte of XML
• The problem to be solved involves only part of the XML document
• Data is available as soon as it is seen by the parser, so SAX works well for
an XML document that arrives over a stream
• Disadvantages of SAX
• We have no random access to an XML document since it is processed in a
forward-only manner
• If you need to keep track of data the parser has seen or change the order of
items, you must write the code and store the data on your own
Dom parser
Dom parser
Dom parser
Dom parser
Dom parser

Contenu connexe

Tendances

Tendances (20)

DTD
DTDDTD
DTD
 
Character stream classes introd .51
Character stream classes introd  .51Character stream classes introd  .51
Character stream classes introd .51
 
Sax parser
Sax parserSax parser
Sax parser
 
Dom(document object model)
Dom(document object model)Dom(document object model)
Dom(document object model)
 
Database Programming
Database ProgrammingDatabase Programming
Database Programming
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Html frames
Html framesHtml frames
Html frames
 
Xml presentation
Xml presentationXml presentation
Xml presentation
 
Active browser web page
Active browser web pageActive browser web page
Active browser web page
 
Xml namespace
Xml namespaceXml namespace
Xml namespace
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
XML and DTD
XML and DTDXML and DTD
XML and DTD
 
Architectural structures and views
Architectural structures and viewsArchitectural structures and views
Architectural structures and views
 
Role of html in web development
Role of html in web developmentRole of html in web development
Role of html in web development
 
Flow oriented modeling
Flow oriented modelingFlow oriented modeling
Flow oriented modeling
 
Threads And Synchronization in C#
Threads And Synchronization in C#Threads And Synchronization in C#
Threads And Synchronization in C#
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
 
Software engineering : Layered Architecture
Software engineering : Layered ArchitectureSoftware engineering : Layered Architecture
Software engineering : Layered Architecture
 

En vedette

Using cookies and sessions
Using cookies and sessionsUsing cookies and sessions
Using cookies and sessionsNuha Noor
 
Understanding layout managers
Understanding layout managersUnderstanding layout managers
Understanding layout managersNuha Noor
 
Jdbc in servlets
Jdbc in servletsJdbc in servlets
Jdbc in servletsNuha Noor
 
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...Nuha Noor
 
Http request and http response
Http request and http responseHttp request and http response
Http request and http responseNuha Noor
 
Jsp elements
Jsp elementsJsp elements
Jsp elementsNuha Noor
 
Reading init param
Reading init paramReading init param
Reading init paramNuha Noor
 

En vedette (13)

Using cookies and sessions
Using cookies and sessionsUsing cookies and sessions
Using cookies and sessions
 
Understanding layout managers
Understanding layout managersUnderstanding layout managers
Understanding layout managers
 
Jdbc in servlets
Jdbc in servletsJdbc in servlets
Jdbc in servlets
 
Events1
Events1Events1
Events1
 
Intro xml
Intro xmlIntro xml
Intro xml
 
Xhtml
XhtmlXhtml
Xhtml
 
Xml dom
Xml domXml dom
Xml dom
 
Xml dtd
Xml dtdXml dtd
Xml dtd
 
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
 
Xml schema
Xml schemaXml schema
Xml schema
 
Http request and http response
Http request and http responseHttp request and http response
Http request and http response
 
Jsp elements
Jsp elementsJsp elements
Jsp elements
 
Reading init param
Reading init paramReading init param
Reading init param
 

Similaire à Dom parser (20)

Unit iv xml dom
Unit iv xml domUnit iv xml dom
Unit iv xml dom
 
Unit 2
Unit 2 Unit 2
Unit 2
 
Jdom how it works & how it opened the java process
Jdom how it works & how it opened the java processJdom how it works & how it opened the java process
Jdom how it works & how it opened the java process
 
Xml parsing
Xml parsingXml parsing
Xml parsing
 
DOM-XML
DOM-XMLDOM-XML
DOM-XML
 
XML
XMLXML
XML
 
25dom
25dom25dom
25dom
 
XMl
XMlXMl
XMl
 
Data interchange integration, HTML XML Biological XML DTD
Data interchange integration, HTML XML Biological XML DTDData interchange integration, HTML XML Biological XML DTD
Data interchange integration, HTML XML Biological XML DTD
 
Advanced guide to develop ajax applications using dojo
Advanced guide to develop ajax applications using dojoAdvanced guide to develop ajax applications using dojo
Advanced guide to develop ajax applications using dojo
 
XML
XMLXML
XML
 
WEB TECHNOLOGY Unit-4.pptx
WEB TECHNOLOGY Unit-4.pptxWEB TECHNOLOGY Unit-4.pptx
WEB TECHNOLOGY Unit-4.pptx
 
Solr Recipes Workshop
Solr Recipes WorkshopSolr Recipes Workshop
Solr Recipes Workshop
 
Xml
XmlXml
Xml
 
Extbase object to xml mapping
Extbase object to xml mappingExtbase object to xml mapping
Extbase object to xml mapping
 
XML
XMLXML
XML
 
DSpace 4.2 XMLUI Theming
DSpace 4.2 XMLUI ThemingDSpace 4.2 XMLUI Theming
DSpace 4.2 XMLUI Theming
 
Service Oriented Architecture -Unit II - Modeling databases in xml
Service Oriented Architecture -Unit II - Modeling databases in xml Service Oriented Architecture -Unit II - Modeling databases in xml
Service Oriented Architecture -Unit II - Modeling databases in xml
 
XML Document Object Model (DOM)
XML Document Object Model (DOM)XML Document Object Model (DOM)
XML Document Object Model (DOM)
 
Part 7
Part 7Part 7
Part 7
 

Plus de sana mateen

PHP Variables and scopes
PHP Variables and scopesPHP Variables and scopes
PHP Variables and scopessana mateen
 
Php and web forms
Php and web formsPhp and web forms
Php and web formssana mateen
 
Encryption in php
Encryption in phpEncryption in php
Encryption in phpsana mateen
 
Authentication methods
Authentication methodsAuthentication methods
Authentication methodssana mateen
 
Unit 1-subroutines in perl
Unit 1-subroutines in perlUnit 1-subroutines in perl
Unit 1-subroutines in perlsana mateen
 
Unit 1-uses for scripting languages,web scripting
Unit 1-uses for scripting languages,web scriptingUnit 1-uses for scripting languages,web scripting
Unit 1-uses for scripting languages,web scriptingsana mateen
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionssana mateen
 
Unit 1-scalar expressions and control structures
Unit 1-scalar expressions and control structuresUnit 1-scalar expressions and control structures
Unit 1-scalar expressions and control structuressana mateen
 
Unit 1-perl names values and variables
Unit 1-perl names values and variablesUnit 1-perl names values and variables
Unit 1-perl names values and variablessana mateen
 
Unit 1-introduction to scripts
Unit 1-introduction to scriptsUnit 1-introduction to scripts
Unit 1-introduction to scriptssana mateen
 
Unit 1-introduction to perl
Unit 1-introduction to perlUnit 1-introduction to perl
Unit 1-introduction to perlsana mateen
 
Unit 1-array,lists and hashes
Unit 1-array,lists and hashesUnit 1-array,lists and hashes
Unit 1-array,lists and hashessana mateen
 
Uses for scripting languages,web scripting in perl
Uses for scripting languages,web scripting in perlUses for scripting languages,web scripting in perl
Uses for scripting languages,web scripting in perlsana mateen
 
Scalar expressions and control structures in perl
Scalar expressions and control structures in perlScalar expressions and control structures in perl
Scalar expressions and control structures in perlsana mateen
 

Plus de sana mateen (20)

Files
FilesFiles
Files
 
PHP Variables and scopes
PHP Variables and scopesPHP Variables and scopes
PHP Variables and scopes
 
Php intro
Php introPhp intro
Php intro
 
Php and web forms
Php and web formsPhp and web forms
Php and web forms
 
Mail
MailMail
Mail
 
Files in php
Files in phpFiles in php
Files in php
 
File upload php
File upload phpFile upload php
File upload php
 
Regex posix
Regex posixRegex posix
Regex posix
 
Encryption in php
Encryption in phpEncryption in php
Encryption in php
 
Authentication methods
Authentication methodsAuthentication methods
Authentication methods
 
Unit 1-subroutines in perl
Unit 1-subroutines in perlUnit 1-subroutines in perl
Unit 1-subroutines in perl
 
Unit 1-uses for scripting languages,web scripting
Unit 1-uses for scripting languages,web scriptingUnit 1-uses for scripting languages,web scripting
Unit 1-uses for scripting languages,web scripting
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressions
 
Unit 1-scalar expressions and control structures
Unit 1-scalar expressions and control structuresUnit 1-scalar expressions and control structures
Unit 1-scalar expressions and control structures
 
Unit 1-perl names values and variables
Unit 1-perl names values and variablesUnit 1-perl names values and variables
Unit 1-perl names values and variables
 
Unit 1-introduction to scripts
Unit 1-introduction to scriptsUnit 1-introduction to scripts
Unit 1-introduction to scripts
 
Unit 1-introduction to perl
Unit 1-introduction to perlUnit 1-introduction to perl
Unit 1-introduction to perl
 
Unit 1-array,lists and hashes
Unit 1-array,lists and hashesUnit 1-array,lists and hashes
Unit 1-array,lists and hashes
 
Uses for scripting languages,web scripting in perl
Uses for scripting languages,web scripting in perlUses for scripting languages,web scripting in perl
Uses for scripting languages,web scripting in perl
 
Scalar expressions and control structures in perl
Scalar expressions and control structures in perlScalar expressions and control structures in perl
Scalar expressions and control structures in perl
 

Dernier

DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationBhangaleSonal
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf203318pmpc
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoordharasingh5698
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdfKamal Acharya
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...soginsider
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 

Dernier (20)

(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 

Dom parser

  • 2. DOM • The Document Object Model is an official recommendation of the World Wide Web Consortium (W3C). It defines an interface that enables programs to access and update the style, structure,and contents of XML documents. XML parsers that support the DOM implement that interface. • When to use? • You should use a DOM parser when: • You need to know a lot about the structure of a document • You need to move parts of the document around (you might want to sort certain elements, for example) • You need to use the information in the document more than once • What you get? • When you parse an XML document with a DOM parser, you get back a tree structure that contains all of the elements of your document. The DOM provides a variety of functions you can use to examine the contents and structure of the document.
  • 3.
  • 4. Common DOM methods • When you are working with the DOM, there are several methods you'll use often: • Document.getDocumentElement() - Returns the root element of the document. • Node.getFirstChild() - Returns the first child of a given Node. • Node.getLastChild() - Returns the last child of a given Node. • Node.getNextSibling() - These methods return the next sibling of a given Node. • Node.getPreviousSibling() - These methods return the previous sibling of a given Node. • Node.getAttribute(attrName) - For a given Node, returns the attribute with the requested name.
  • 5. Steps to Using DOM • Following are the steps used while parsing a document using DOM Parser. • Import XML-related packages. • Create a DocumentBuilder • Create a Document from a file or stream • Extract the root element • Examine attributes • Examine sub-elements • Import XML-related packages • import org.w3c.dom.*; • import javax.xml.parsers.*; • import java.io.*;
  • 6. • Create a DocumentBuilder • DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); • DocumentBuilder builder = factory.newDocumentBuilder(); • Create a Document from a file or stream • StringBuilder xmlStringBuilder = new StringBuilder(); xmlStringBuilder.append("<?xml version="1.0"?> <class> </class>"); ByteArrayInputStream input = new ByteArrayInputStream( xmlStringBuilder.toString().getBytes("UTF-8")); • Document doc = builder.parse(input); • Extract the root element • Element root = document.getDocumentElement(); • Examine attributes • //returns specific attribute • getAttribute("attributeName"); • //returns a (table) of names/values • getAttributes(); • Examine sub-elements • //returns a list of subelements of specified name getElementsByTagName("subelementName"); / • /returns a list of all child nodes • getChildNodes();
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12. SAX PARSER •SAX (the Simple API for XML) is an event-based parser for xml documents. • Unlike a DOM parser, a SAX parser creates no parse tree. SAX is a streaming interface for XML, which means that applications using SAX receive event notifications about the XML document being processed an element, and attribute, at a time in sequential order starting at the top of the document, and ending with the closing of the ROOT element.
  • 13. SAX • Reads an XML document from top to bottom, recognizing the tokens that make up a well-formed XML document • Tokens are processed in the same order that they appear in the document • Reports the application program the nature of tokens that the parser has encountered as they occur • The application program provides an "event" handler that must be registered with the parser • As the tokens are identified, callback methods in the handler are invoked with the relevant information
  • 14. When to use? • You should use a SAX parser when: • You can process the XML document in a linear fashion from the top down • The document is not deeply nested • You are processing a very large XML document whose DOM tree would consume too much memory.Typical DOM implementations use ten bytes of memory to represent one byte of XML • The problem to be solved involves only part of the XML document • Data is available as soon as it is seen by the parser, so SAX works well for an XML document that arrives over a stream • Disadvantages of SAX • We have no random access to an XML document since it is processed in a forward-only manner • If you need to keep track of data the parser has seen or change the order of items, you must write the code and store the data on your own