SlideShare a Scribd company logo
2
Most read
3
Most read
12
Most read
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.
Dom parser
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();
Dom parser
Dom parser
Dom parser
Dom parser
Dom parser
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

More Related Content

PPTX
Java Server Pages(jsp)
PPTX
Servlets
PPT
C# basics
PPTX
Need of object oriented programming
PPT
Introduction to XML
PPTX
Laravel ppt
Java Server Pages(jsp)
Servlets
C# basics
Need of object oriented programming
Introduction to XML
Laravel ppt

What's hot (20)

PPTX
Object Oriented Testing
PPT
Introduction To Dotnet
PPT
Bottom - Up Parsing
PPT
Introduction to ADO.NET
PDF
Collections in Java Notes
PPTX
Java program structure
PPTX
Le langage html
PPTX
C# Delegates
PPTX
Web services SOAP
PPTX
04. xss and encoding
PPTX
Semantics analysis
DOCX
Mongoose getting started-Mongo Db with Node js
PPTX
Odbms concepts
PPTX
Adbms 16 object definition language
PDF
Xml schema
PPTX
Introduction to Spring Framework
PPTX
Java features
PDF
Java I/o streams
PDF
Learn C# Programming - Data Types & Type Conversion
Object Oriented Testing
Introduction To Dotnet
Bottom - Up Parsing
Introduction to ADO.NET
Collections in Java Notes
Java program structure
Le langage html
C# Delegates
Web services SOAP
04. xss and encoding
Semantics analysis
Mongoose getting started-Mongo Db with Node js
Odbms concepts
Adbms 16 object definition language
Xml schema
Introduction to Spring Framework
Java features
Java I/o streams
Learn C# Programming - Data Types & Type Conversion
Ad

Viewers also liked (13)

PPTX
Using cookies and sessions
PPT
Understanding layout managers
PPTX
Jdbc in servlets
PPT
Events1
PPTX
Intro xml
PPTX
PPTX
Xml dom
PPTX
Xml dtd
PPT
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
PPTX
Xml schema
PPTX
Http request and http response
PPTX
Jsp elements
PPTX
Reading init param
Using cookies and sessions
Understanding layout managers
Jdbc in servlets
Events1
Intro xml
Xml dom
Xml dtd
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Xml schema
Http request and http response
Jsp elements
Reading init param
Ad

Similar to Dom parser (20)

PDF
Xml parsing
PPT
PPT
PPTX
Unit iv xml dom
PPTX
Unit 2
PDF
Service Oriented Architecture - Unit II - Sax
DOCX
PPT
uptu web technology unit 2 Xml2
PPT
Xml parsers
PPT
Processing XML with Java
PDF
PPTX
WEB PRORAMMING NOTES WITH EXAMPLE PROGRAMS
PPTX
Sax parser
PPT
Understanding XML DOM
PDF
Building XML Based Applications
PDF
Processing XML
PPT
XML SAX PARSING
PPTX
Xml and xml processor
Xml parsing
Unit iv xml dom
Unit 2
Service Oriented Architecture - Unit II - Sax
uptu web technology unit 2 Xml2
Xml parsers
Processing XML with Java
WEB PRORAMMING NOTES WITH EXAMPLE PROGRAMS
Sax parser
Understanding XML DOM
Building XML Based Applications
Processing XML
XML SAX PARSING
Xml and xml processor

More from sana mateen (20)

PPTX
PPTX
PHP Variables and scopes
PPTX
Php intro
PPTX
Php and web forms
PPTX
PPTX
Files in php
PPTX
File upload php
PPTX
Regex posix
PPTX
Encryption in php
PPTX
Authentication methods
PPTX
Unit 1-subroutines in perl
PPTX
Unit 1-uses for scripting languages,web scripting
PPTX
Unit 1-strings,patterns and regular expressions
PPTX
Unit 1-scalar expressions and control structures
PPTX
Unit 1-perl names values and variables
PPTX
Unit 1-introduction to scripts
PPTX
Unit 1-introduction to perl
PPTX
Unit 1-array,lists and hashes
PPTX
Uses for scripting languages,web scripting in perl
PPTX
Scalar expressions and control structures in perl
PHP Variables and scopes
Php intro
Php and web forms
Files in php
File upload php
Regex posix
Encryption in php
Authentication methods
Unit 1-subroutines in perl
Unit 1-uses for scripting languages,web scripting
Unit 1-strings,patterns and regular expressions
Unit 1-scalar expressions and control structures
Unit 1-perl names values and variables
Unit 1-introduction to scripts
Unit 1-introduction to perl
Unit 1-array,lists and hashes
Uses for scripting languages,web scripting in perl
Scalar expressions and control structures in perl

Recently uploaded (20)

PPTX
Safety Seminar civil to be ensured for safe working.
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Construction Project Organization Group 2.pptx
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PDF
PPT on Performance Review to get promotions
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPT
Project quality management in manufacturing
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
PPTX
Current and future trends in Computer Vision.pptx
PPTX
Fundamentals of Mechanical Engineering.pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPT
introduction to datamining and warehousing
PPT
Mechanical Engineering MATERIALS Selection
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Safety Seminar civil to be ensured for safe working.
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
Foundation to blockchain - A guide to Blockchain Tech
Construction Project Organization Group 2.pptx
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PPT on Performance Review to get promotions
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Project quality management in manufacturing
Embodied AI: Ushering in the Next Era of Intelligent Systems
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
Current and future trends in Computer Vision.pptx
Fundamentals of Mechanical Engineering.pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
introduction to datamining and warehousing
Mechanical Engineering MATERIALS Selection
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf

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.
  • 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();
  • 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