SlideShare una empresa de Scribd logo
1 de 37
XML Parsers
XML Parsers
 What is a XML parser?
 DOM and SAX parser API
 Xerces-J parsers overview
 Work with XML parsers (example)
What is a XML Parser?
It is a software library (or a package) that
provides methods (or interfaces) for client
applications to work with XML documents
It checks the well-formattedness
It may validate the documents
It does a lot of other detailed things so that
a client is shielded from that complexities
What is a XML Parser?
(continued)
DOM and SAX Parsers
in general
DOM: Document Object Model
SAX: Simple API for XML
A DOM parser implements DOM API
A SAX parser implement SAX API
Most major parsers implement both
DOM and SAX API’s
DOM and SAX Parsers
DOM parsers
• DOM Document object
• Main features of DOM parsers
DOM and SAX Parsers
DOM Document Object
A DOM document is an object containing
all the information of an XML document
It is composed of a tree (DOM tree) of
nodes , and various nodes that are somehow
associated with other nodes in the tree but
are not themselves part of the DOM tree
DOM and SAX Parsers
DOM Document Object
There are 12 types of nodes in a DOM
Document object
Document node
Element node
Text node
Attribute node
Processing instruction node
…….
DOM and SAX Parsers
DOM parsers – continued (Appendix)
Sample XML document
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href=“test.css"?>
<!-- It's an xml-stylesheet processing instruction. -->
<!DOCTYPE shapes SYSTEM “shapes.dtd">
<shapes>
……
<squre color=“BLUE”>
<length> 20 </length>
</squre>
……
</shapes>
DOM and SAX Parsers
DOM parsers – continued (Appendix)
DOM and SAX Parsers
main features of DOM parsers
A DOM parser creates an internal structure in
memory which is a DOM document object
Client applications get the information of the
original XML document by invoking methods
on this Document object or on other objects it
contains
DOM parser is tree-based (or DOM obj-based)
Client application seems to be pulling the data
actively, from the data flow point of view
DOM and SAX Parsers
main features of DOM parsers (cont.)
Advantage:
(1) It is good when random access to widely
separated parts of a document is required
(2) It supports both read and write operations
Disadvantage:
(1) It is memory inefficient
(2) It seems complicated, although not really
DOM and SAX Parsers
SAX parsers
It does not first create any internal structure
Client does not specify what methods to call
Client just overrides the methods of the API
and place his own code inside there
When the parser encounters start-tag, end-
tag,etc., it thinks of them as events
DOM and SAX Parsers
SAX parsers (cont.)
When such an event occurs, the handler
automatically calls back to a particular method
overridden by the client, and feeds as arguments
the method what it sees
SAX parser is event-based,it works like an event
handler in Java (e.g. MouseAdapter)
Client application seems to be just receiving the
data inactively, from the data flow point of view
DOM and SAX Parsers
SAX parsers (cont.)
Advantage:
(1) It is simple
(2) It is memory efficient
(3) It works well in stream application
Disadvantage:
The data is broken into pieces and clients
never have all the information as a whole
unless they create their own data structure
Appendix: Call back in Java
class MyMouseListener extends java.awt.event.MouseAdapter {
/** Overriding the method mousePressed(). */
public void mousePressed(java.awt.event.MouseEvent event) {
..…do something here after the mouse is pressed …….
}
/** Overriding the method mousePressed(). */
public void mouseReleased(java.awt.event.MouseEvent event) {
..…do something here after the mouse is released …….
}
}
MyMouseListener Listener = new MyMouseListener();
java.awt.Button MyButton=new java.awt.Button("ok");
MyButton.addMouseListener(Listener);
DOM and SAX Parsers
Xerces-J Parser Overview
It is a Java package
Provides two parsers, one is a DOM parser
and another is a SAX parser
It is a validating parser
It fully supports DOM2 and SAX2, and
partially supports DOM3 (W3C XML
Schema)
It is very popular
Xerces-J Parser Overview
package structure
java.lang.Object
| +--org.apache.xerces.framework.XMLParser
| +-- org.apache.xerces.parsers.DOMParser
+-- org.apache.xerces.parsers.SAXParser
Xerces-J Parser Overview
DOMParser methods
 Void parse (java.lang.String systemId)
Parses the input source specified by
the
given system identifier.
Document getDocument()
Returns the document
……
Xerces-J DOMParser
DOM interfaces
 Document
 Element
 Attr
 NodeList
 ProcessingInstruction
 NamedNodeMap
 . . . . .
Xerces-J Parser Overview
SAXParser methods
 Void parse (java.lang.String systemId)
Parses the input source specified by the
given system identifier.
 Void setContentHandler(Contenthandler handler)
Allow an application to register a content event handler.
 Void setErrorHandler(Errorhandler handler)
Set error handler.
 ……
Xerces-J Parser Overview
SAXParser interfaces
 ContentHandler
 DTDHandler
 EntityResolver
 ErrorHandler
Work with XML Parsers
Example
Task: Extract all information about
circles
<?xml version="1.0"?>
<!DOCTYPE shapes SYSTEM “shapes.dtd">
<shapes>
<circle color=“BLUE”>
<x> 20 </x>
<y> 20 </y>
<radius> 20 </radius>
</circle>
</shapes>
Example
DOMParser:create client class
public class shapes_DOM {
static int numberOfCircles = 0;
static int x[] = new int[1000];
static int y[] = new int[1000];
static int r[] = new int[1000];
static String color[] = new String[1000];
public static void main(String[] args) {
……
}
}
Example
(DOMParser: create a DOMParser)
import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;
public class shapes_DOM {
……
public static void main(String [ ] args ) {
try{
DOMParser parser=new DOMParser();
parser.parse(args[0]);
Document doc=parser.getDocument();
……
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
Example
(DOMParser: get all the circle nodes)
NodeList nodelist =
doc.getElementsByTagName("circle");
numberOfCircles = nodelist.getLength();
Example
(DOMParser: iterate over circle nodes)
for(int i=0; i<nodelist.getLength(); i++) {
Node node = nodelist.item(i);
.
.
.
}
Example
(DOMParser: get color attribute)
25 NamedNodeMap attrs = node.getAttributes();
26 if(attrs.getLength()!=0)
26’ color[i]=
(String)attrs.getNamedItem("color").getNodeValue();
Example
(DOMParser: get child nodes)
27 // get the child nodes of a circle
28 NodeList childnodelist = node.getChildNodes();
29 // get the x and y
30 for(int j=0; j<childnodelist.getLength(); j++) {
31 Node childnode = childnodelist.item(j);
32 Node textnode = childnode.getFirstChild();
33 String childnodename = childnode.getNodeName();
34 if(childnodename.equals("x"))
35 x[i]=Integer.parseInt(textnode.getNodeValue().trim());
36 else if(childnodename.equals("y"))
37 y[i]=Integer.parseInt(textnode.getNodeValue().trim());
38 else if(childnodename.equals("radius"))
39 r[i]=Integer.parseInt(texxtnode.getNodeValue().trim())
40 }
Example
(SAXarser: create client class)
public class shapes_SAX extends DefaultHandler {
static int numberOfCircles = 0;
static int x[] = new int[1000];
static int y[] = new int[1000];
static int r[] = new int[1000];
static String color[] = new String[1000];
public static void main(String[] args) {
……
}
}
Example
(SAXParser: create a SAXParser)
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import org.apache.xerces.parsers.SAXParser;
public class shapes_SAX extends DefaultHandler {
public static void main(String [ ] args ) {
try{
shapes_SAX SAXHandler = new shapes_SAX();
SAXParser parser = new SAXParser();
parser.setContentHandler(SAXHandler);
parser.parse(args[0]);
} catch (Exception e) { … … }
}
}
Example
(SAXParser: override methods of interest)
startDocument() endDocument()
startElement() endElement()
startCDATA() endCDATA()
startDTD() endDTD()
characters()
 … …
Example
(SAXParser: override startElement() )
21 public void startElement(String uri, String localName,
String rawName, Attributes attributes) {
22 if(rawName.equals("circle" )
23 color[numberOfCircles]=attributes.getValue("color");
26 else if(rawName.equals("x"))
27 flagX = 1;
28 else if(rawName.equals("y"))
29 flagY = 1;
30 else if(rawName.equals("radius"))
31 flagR = 1;
32 }
Example
(SAXParser: override endElement() )
33 public void endElement(String uri, String
localName, String rawName) {
34 numberOfCircles += 1;
35 }
Example
(SAXParser: override characters() )
36 public void characters(char characters[], int start,
int length) {
38 String characterData =
39 (new String(characters,start,length)).trim();
42 if(flagX==1) {
43 x[numberOfCircles] = Integer.parseInt(characterData);
flagX=0;
}
44 if(flagY==1) {
45 y[numberOfCircles] = Integer.parseInt(characterData);
flagY=0;
}
46 if(flagR==1) {
47 r[numberOfCircles] = Integer.parseInt(characterData);
flagR=0;
}
49 }
Example
(SAXParser: override endDocument() )
50 public void endDocument() {
51 // print the result
52 System.out.println("circles="+numberOfCircles);
53 for(int i=0;i<numberOfCircles;i++) {
54 String line="";
55 line=line+"(x="+x[i]+",y="+y[i]+",r="+r[i]
+",color="+color[i]+")";
56 System.out.println(line);
57 }
58 }

Más contenido relacionado

La actualidad más candente

Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScriptAndres Baravalle
 
XML Document Object Model (DOM)
XML Document Object Model (DOM)XML Document Object Model (DOM)
XML Document Object Model (DOM)BOSS Webtech
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptWalid Ashraf
 
screen output and keyboard input in js
screen output and keyboard input in jsscreen output and keyboard input in js
screen output and keyboard input in jschauhankapil
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in phpCPD INDIA
 
PHP Cookies and Sessions
PHP Cookies and SessionsPHP Cookies and Sessions
PHP Cookies and SessionsNisa Soomro
 
Introduction of Html/css/js
Introduction of Html/css/jsIntroduction of Html/css/js
Introduction of Html/css/jsKnoldus Inc.
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object ModelWebStackAcademy
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in javaGoogle
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessionsSukrit Gupta
 

La actualidad más candente (20)

Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
XML Document Object Model (DOM)
XML Document Object Model (DOM)XML Document Object Model (DOM)
XML Document Object Model (DOM)
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
 
Web services
Web servicesWeb services
Web services
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to Javascript
 
screen output and keyboard input in js
screen output and keyboard input in jsscreen output and keyboard input in js
screen output and keyboard input in js
 
Sessions and cookies
Sessions and cookiesSessions and cookies
Sessions and cookies
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
 
PHP Cookies and Sessions
PHP Cookies and SessionsPHP Cookies and Sessions
PHP Cookies and Sessions
 
Introduction of Html/css/js
Introduction of Html/css/jsIntroduction of Html/css/js
Introduction of Html/css/js
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
 
XSLT
XSLTXSLT
XSLT
 
Sessions in php
Sessions in php Sessions in php
Sessions in php
 
HTML Forms
HTML FormsHTML Forms
HTML Forms
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessions
 
PHP - Introduction to File Handling with PHP
PHP -  Introduction to  File Handling with PHPPHP -  Introduction to  File Handling with PHP
PHP - Introduction to File Handling with PHP
 
Java Script ppt
Java Script pptJava Script ppt
Java Script ppt
 

Destacado (19)

Xml processors
Xml processorsXml processors
Xml processors
 
XML DOM
XML DOMXML DOM
XML DOM
 
XML SAX PARSING
XML SAX PARSING XML SAX PARSING
XML SAX PARSING
 
6 xml parsing
6   xml parsing6   xml parsing
6 xml parsing
 
Java XML Parsing
Java XML ParsingJava XML Parsing
Java XML Parsing
 
Dom parser
Dom parserDom parser
Dom parser
 
Session 5
Session 5Session 5
Session 5
 
Xml3
Xml3Xml3
Xml3
 
Semantic web xml-rdf-dom parser
Semantic web xml-rdf-dom parserSemantic web xml-rdf-dom parser
Semantic web xml-rdf-dom parser
 
Xml parsing
Xml parsingXml parsing
Xml parsing
 
Introduce to XML
Introduce to XMLIntroduce to XML
Introduce to XML
 
Dino's DEV Project
Dino's DEV ProjectDino's DEV Project
Dino's DEV Project
 
Simple API for XML
Simple API for XMLSimple API for XML
Simple API for XML
 
Parsing XML Data
Parsing XML DataParsing XML Data
Parsing XML Data
 
5 xml parsing
5   xml parsing5   xml parsing
5 xml parsing
 
java API for XML DOM
java API for XML DOMjava API for XML DOM
java API for XML DOM
 
J2ee architecture
J2ee architectureJ2ee architecture
J2ee architecture
 
Lexical Analyzers and Parsers
Lexical Analyzers and ParsersLexical Analyzers and Parsers
Lexical Analyzers and Parsers
 
Difference Between DOM and SAX parser in java with examples
Difference Between DOM and SAX parser in java with examplesDifference Between DOM and SAX parser in java with examples
Difference Between DOM and SAX parser in java with examples
 

Similar a Xml parsers

SAX, DOM & JDOM parsers for beginners
SAX, DOM & JDOM parsers for beginnersSAX, DOM & JDOM parsers for beginners
SAX, DOM & JDOM parsers for beginnersHicham QAISSI
 
Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...
Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...
Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...GeeksLab Odessa
 
DevNation'15 - Using Lambda Expressions to Query a Datastore
DevNation'15 - Using Lambda Expressions to Query a DatastoreDevNation'15 - Using Lambda Expressions to Query a Datastore
DevNation'15 - Using Lambda Expressions to Query a DatastoreXavier Coulon
 
Building node.js applications with Database Jones
Building node.js applications with Database JonesBuilding node.js applications with Database Jones
Building node.js applications with Database JonesJohn David Duncan
 
Lucene Introduction
Lucene IntroductionLucene Introduction
Lucene Introductionotisg
 
Jquery dojo slides
Jquery dojo slidesJquery dojo slides
Jquery dojo slideshelenmga
 
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 processHicham QAISSI
 
Apache Flink: API, runtime, and project roadmap
Apache Flink: API, runtime, and project roadmapApache Flink: API, runtime, and project roadmap
Apache Flink: API, runtime, and project roadmapKostas Tzoumas
 
Ajax tutorial
Ajax tutorialAjax tutorial
Ajax tutorialKat Roque
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8Martin Toshev
 
Kerberizing spark. Spark Summit east
Kerberizing spark. Spark Summit eastKerberizing spark. Spark Summit east
Kerberizing spark. Spark Summit eastJorge Lopez-Malla
 

Similar a Xml parsers (20)

Xm lparsers
Xm lparsersXm lparsers
Xm lparsers
 
SAX, DOM & JDOM parsers for beginners
SAX, DOM & JDOM parsers for beginnersSAX, DOM & JDOM parsers for beginners
SAX, DOM & JDOM parsers for beginners
 
Java and XML
Java and XMLJava and XML
Java and XML
 
Sax parser
Sax parserSax parser
Sax parser
 
Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...
Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...
Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...
 
Dom
DomDom
Dom
 
Reversing JavaScript
Reversing JavaScriptReversing JavaScript
Reversing JavaScript
 
DevNation'15 - Using Lambda Expressions to Query a Datastore
DevNation'15 - Using Lambda Expressions to Query a DatastoreDevNation'15 - Using Lambda Expressions to Query a Datastore
DevNation'15 - Using Lambda Expressions to Query a Datastore
 
Building node.js applications with Database Jones
Building node.js applications with Database JonesBuilding node.js applications with Database Jones
Building node.js applications with Database Jones
 
Lucene Introduction
Lucene IntroductionLucene Introduction
Lucene Introduction
 
Jquery dojo slides
Jquery dojo slidesJquery dojo slides
Jquery dojo slides
 
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
 
Apache Flink: API, runtime, and project roadmap
Apache Flink: API, runtime, and project roadmapApache Flink: API, runtime, and project roadmap
Apache Flink: API, runtime, and project roadmap
 
Processing XML with Java
Processing XML with JavaProcessing XML with Java
Processing XML with Java
 
Introduction to JQuery
Introduction to JQueryIntroduction to JQuery
Introduction to JQuery
 
24sax
24sax24sax
24sax
 
Ajax tutorial
Ajax tutorialAjax tutorial
Ajax tutorial
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8
 
Kerberizing spark. Spark Summit east
Kerberizing spark. Spark Summit eastKerberizing spark. Spark Summit east
Kerberizing spark. Spark Summit east
 
Ch23
Ch23Ch23
Ch23
 

Más de Manav Prasad (20)

Experience with mulesoft
Experience with mulesoftExperience with mulesoft
Experience with mulesoft
 
Mulesoftconnectors
MulesoftconnectorsMulesoftconnectors
Mulesoftconnectors
 
Mule and web services
Mule and web servicesMule and web services
Mule and web services
 
Mulesoft cloudhub
Mulesoft cloudhubMulesoft cloudhub
Mulesoft cloudhub
 
Perl tutorial
Perl tutorialPerl tutorial
Perl tutorial
 
Hibernate presentation
Hibernate presentationHibernate presentation
Hibernate presentation
 
Jpa
JpaJpa
Jpa
 
Spring introduction
Spring introductionSpring introduction
Spring introduction
 
Json
Json Json
Json
 
The spring framework
The spring frameworkThe spring framework
The spring framework
 
Rest introduction
Rest introductionRest introduction
Rest introduction
 
Exceptions in java
Exceptions in javaExceptions in java
Exceptions in java
 
Junit
JunitJunit
Junit
 
Xpath
XpathXpath
Xpath
 
Xslt
XsltXslt
Xslt
 
Xhtml
XhtmlXhtml
Xhtml
 
Css
CssCss
Css
 
Introduction to html5
Introduction to html5Introduction to html5
Introduction to html5
 
Ajax
AjaxAjax
Ajax
 
J query
J queryJ query
J query
 

Último

Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 

Último (20)

Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 

Xml parsers

  • 2. XML Parsers  What is a XML parser?  DOM and SAX parser API  Xerces-J parsers overview  Work with XML parsers (example)
  • 3. What is a XML Parser? It is a software library (or a package) that provides methods (or interfaces) for client applications to work with XML documents It checks the well-formattedness It may validate the documents It does a lot of other detailed things so that a client is shielded from that complexities
  • 4. What is a XML Parser? (continued)
  • 5. DOM and SAX Parsers in general DOM: Document Object Model SAX: Simple API for XML A DOM parser implements DOM API A SAX parser implement SAX API Most major parsers implement both DOM and SAX API’s
  • 6. DOM and SAX Parsers DOM parsers • DOM Document object • Main features of DOM parsers
  • 7. DOM and SAX Parsers DOM Document Object A DOM document is an object containing all the information of an XML document It is composed of a tree (DOM tree) of nodes , and various nodes that are somehow associated with other nodes in the tree but are not themselves part of the DOM tree
  • 8. DOM and SAX Parsers DOM Document Object There are 12 types of nodes in a DOM Document object Document node Element node Text node Attribute node Processing instruction node …….
  • 9. DOM and SAX Parsers DOM parsers – continued (Appendix) Sample XML document <?xml version="1.0"?> <?xml-stylesheet type="text/css" href=“test.css"?> <!-- It's an xml-stylesheet processing instruction. --> <!DOCTYPE shapes SYSTEM “shapes.dtd"> <shapes> …… <squre color=“BLUE”> <length> 20 </length> </squre> …… </shapes>
  • 10. DOM and SAX Parsers DOM parsers – continued (Appendix)
  • 11. DOM and SAX Parsers main features of DOM parsers A DOM parser creates an internal structure in memory which is a DOM document object Client applications get the information of the original XML document by invoking methods on this Document object or on other objects it contains DOM parser is tree-based (or DOM obj-based) Client application seems to be pulling the data actively, from the data flow point of view
  • 12. DOM and SAX Parsers main features of DOM parsers (cont.) Advantage: (1) It is good when random access to widely separated parts of a document is required (2) It supports both read and write operations Disadvantage: (1) It is memory inefficient (2) It seems complicated, although not really
  • 13. DOM and SAX Parsers SAX parsers It does not first create any internal structure Client does not specify what methods to call Client just overrides the methods of the API and place his own code inside there When the parser encounters start-tag, end- tag,etc., it thinks of them as events
  • 14. DOM and SAX Parsers SAX parsers (cont.) When such an event occurs, the handler automatically calls back to a particular method overridden by the client, and feeds as arguments the method what it sees SAX parser is event-based,it works like an event handler in Java (e.g. MouseAdapter) Client application seems to be just receiving the data inactively, from the data flow point of view
  • 15. DOM and SAX Parsers SAX parsers (cont.) Advantage: (1) It is simple (2) It is memory efficient (3) It works well in stream application Disadvantage: The data is broken into pieces and clients never have all the information as a whole unless they create their own data structure
  • 16. Appendix: Call back in Java class MyMouseListener extends java.awt.event.MouseAdapter { /** Overriding the method mousePressed(). */ public void mousePressed(java.awt.event.MouseEvent event) { ..…do something here after the mouse is pressed ……. } /** Overriding the method mousePressed(). */ public void mouseReleased(java.awt.event.MouseEvent event) { ..…do something here after the mouse is released ……. } } MyMouseListener Listener = new MyMouseListener(); java.awt.Button MyButton=new java.awt.Button("ok"); MyButton.addMouseListener(Listener);
  • 17. DOM and SAX Parsers
  • 18. Xerces-J Parser Overview It is a Java package Provides two parsers, one is a DOM parser and another is a SAX parser It is a validating parser It fully supports DOM2 and SAX2, and partially supports DOM3 (W3C XML Schema) It is very popular
  • 19. Xerces-J Parser Overview package structure java.lang.Object | +--org.apache.xerces.framework.XMLParser | +-- org.apache.xerces.parsers.DOMParser +-- org.apache.xerces.parsers.SAXParser
  • 20. Xerces-J Parser Overview DOMParser methods  Void parse (java.lang.String systemId) Parses the input source specified by the given system identifier. Document getDocument() Returns the document ……
  • 21. Xerces-J DOMParser DOM interfaces  Document  Element  Attr  NodeList  ProcessingInstruction  NamedNodeMap  . . . . .
  • 22. Xerces-J Parser Overview SAXParser methods  Void parse (java.lang.String systemId) Parses the input source specified by the given system identifier.  Void setContentHandler(Contenthandler handler) Allow an application to register a content event handler.  Void setErrorHandler(Errorhandler handler) Set error handler.  ……
  • 23. Xerces-J Parser Overview SAXParser interfaces  ContentHandler  DTDHandler  EntityResolver  ErrorHandler
  • 24. Work with XML Parsers Example Task: Extract all information about circles <?xml version="1.0"?> <!DOCTYPE shapes SYSTEM “shapes.dtd"> <shapes> <circle color=“BLUE”> <x> 20 </x> <y> 20 </y> <radius> 20 </radius> </circle> </shapes>
  • 25. Example DOMParser:create client class public class shapes_DOM { static int numberOfCircles = 0; static int x[] = new int[1000]; static int y[] = new int[1000]; static int r[] = new int[1000]; static String color[] = new String[1000]; public static void main(String[] args) { …… } }
  • 26. Example (DOMParser: create a DOMParser) import org.w3c.dom.*; import org.apache.xerces.parsers.DOMParser; public class shapes_DOM { …… public static void main(String [ ] args ) { try{ DOMParser parser=new DOMParser(); parser.parse(args[0]); Document doc=parser.getDocument(); …… } catch (Exception e) { e.printStackTrace(System.err); } }
  • 27. Example (DOMParser: get all the circle nodes) NodeList nodelist = doc.getElementsByTagName("circle"); numberOfCircles = nodelist.getLength();
  • 28. Example (DOMParser: iterate over circle nodes) for(int i=0; i<nodelist.getLength(); i++) { Node node = nodelist.item(i); . . . }
  • 29. Example (DOMParser: get color attribute) 25 NamedNodeMap attrs = node.getAttributes(); 26 if(attrs.getLength()!=0) 26’ color[i]= (String)attrs.getNamedItem("color").getNodeValue();
  • 30. Example (DOMParser: get child nodes) 27 // get the child nodes of a circle 28 NodeList childnodelist = node.getChildNodes(); 29 // get the x and y 30 for(int j=0; j<childnodelist.getLength(); j++) { 31 Node childnode = childnodelist.item(j); 32 Node textnode = childnode.getFirstChild(); 33 String childnodename = childnode.getNodeName(); 34 if(childnodename.equals("x")) 35 x[i]=Integer.parseInt(textnode.getNodeValue().trim()); 36 else if(childnodename.equals("y")) 37 y[i]=Integer.parseInt(textnode.getNodeValue().trim()); 38 else if(childnodename.equals("radius")) 39 r[i]=Integer.parseInt(texxtnode.getNodeValue().trim()) 40 }
  • 31. Example (SAXarser: create client class) public class shapes_SAX extends DefaultHandler { static int numberOfCircles = 0; static int x[] = new int[1000]; static int y[] = new int[1000]; static int r[] = new int[1000]; static String color[] = new String[1000]; public static void main(String[] args) { …… } }
  • 32. Example (SAXParser: create a SAXParser) import org.xml.sax.*; import org.xml.sax.helpers.DefaultHandler; import org.apache.xerces.parsers.SAXParser; public class shapes_SAX extends DefaultHandler { public static void main(String [ ] args ) { try{ shapes_SAX SAXHandler = new shapes_SAX(); SAXParser parser = new SAXParser(); parser.setContentHandler(SAXHandler); parser.parse(args[0]); } catch (Exception e) { … … } } }
  • 33. Example (SAXParser: override methods of interest) startDocument() endDocument() startElement() endElement() startCDATA() endCDATA() startDTD() endDTD() characters()  … …
  • 34. Example (SAXParser: override startElement() ) 21 public void startElement(String uri, String localName, String rawName, Attributes attributes) { 22 if(rawName.equals("circle" ) 23 color[numberOfCircles]=attributes.getValue("color"); 26 else if(rawName.equals("x")) 27 flagX = 1; 28 else if(rawName.equals("y")) 29 flagY = 1; 30 else if(rawName.equals("radius")) 31 flagR = 1; 32 }
  • 35. Example (SAXParser: override endElement() ) 33 public void endElement(String uri, String localName, String rawName) { 34 numberOfCircles += 1; 35 }
  • 36. Example (SAXParser: override characters() ) 36 public void characters(char characters[], int start, int length) { 38 String characterData = 39 (new String(characters,start,length)).trim(); 42 if(flagX==1) { 43 x[numberOfCircles] = Integer.parseInt(characterData); flagX=0; } 44 if(flagY==1) { 45 y[numberOfCircles] = Integer.parseInt(characterData); flagY=0; } 46 if(flagR==1) { 47 r[numberOfCircles] = Integer.parseInt(characterData); flagR=0; } 49 }
  • 37. Example (SAXParser: override endDocument() ) 50 public void endDocument() { 51 // print the result 52 System.out.println("circles="+numberOfCircles); 53 for(int i=0;i<numberOfCircles;i++) { 54 String line=""; 55 line=line+"(x="+x[i]+",y="+y[i]+",r="+r[i] +",color="+color[i]+")"; 56 System.out.println(line); 57 } 58 }