SlideShare a Scribd company logo
1 of 22
Download to read offline
XML & JSON PARSERS

           IN JAVA
             Henry Addo
    E-mail: addhen@gmail.com
 Twitter: http://twitter.com/eyedol
WHAT IS PARSING A
XML / JSON DOCUMENT

Reading a xml / json document to determine its
        structure or extract its content.
DIFFERENT WAYS OF
         PARSING XML
          DOCUMENT
* DOM - Document Object Model - org.w3c.dom.*
* JDOM - Open source version of DOM - org.jdom.*
* SAX - Simple API for XML - org.xml.sax.*
* StAX - Streaming API for XML - java.xml.stream.*
DOM
* Document Object Model - An interface for working
with the structure of an xml document.

* Developed by w3c to make life easier for the
programmer.
DOM TREE
* XML is loaded in hierarchical data structure( the
DOM tree ).

* Everything in a DOM tree is a Node of one type or
another type.

* Element: Represent an XML element in the source
document.

* Attr: Represents an attribute of an XML element.

* Text: The content of an element of an XML element.
* Document: Represents the entire XML document.
COMMON DOM METHODS
* Document.getDocumentElement(); returns the root
of the DOM tree

* Node.getFirstChild() & Node.getLastChild(); returns
the first or last child of a given Node.

* Node.getNextSibling() & Node.getPreviousSibling();
returns the next or previous sibling of a given Node.

* Element.getAttribute( String attrName); returns the
value of the attribute named attrName of a given
Element.
SAX
Simple API for XML - Designed to address the issues
with DOM.

* Memory hog
* Excess objects
* Build entire document tree for code to work
HOW SAX WORKS
SAX parsers tells you when it finds things in the XML
document using events.

* It sends you events as it finds things in an XML
document.

* It gives the option for creating objects as when
needed.

* It sends events right on the onset, doesn’t have to
wait for the parser to finish reading the document.
COMMON SAX EVENTS
SAX API defines number of events. Write code to do
things in response to those events.

* startDocument(); prompts that it has found the start
of the document.
* endDocument(); prompts that it has found the end of
the document.
* startElement(...); found a start tag--
name,values,attributes of the element.
* characters(...); found some text. char array
* endElement(..); found an end tag--name + assoc.
name space
CHOOSING XML
            PARSER
DOM
  * You Need to know a lot about the document
structure
  * You Need to change the structure of the document
 SAX
  * You don’t have much machine memory
  * You only need a few elements or attributes from the
XML document
StAX
  * You need to do xml processing on mobile devices
EXAMPLE PARSING XML
     WITH DOM
* Get a document builder using document builder
factory and parse the xml file to create a DOM obj.

* Get a list of employee elements from the DOM.

* For each employee element get the id, name, age and
type.

* Iterate through the list and print the employees
details.
EXAMPLE PARSING XML
     WITH DOM

     Show some codes
EXAMPLE PARSING XML
     WITH SAX
* Create a SAX parser and parse the xml.
* In the event handler create the employee object.
* Print out the data.
EXAMPLE PARSING XML
     WITH SAX

     Show some codes
HOW TO GENERATE
     XML FROM JAVA
This time generate an xml file instead of reading it.
* Load Data.
* Get an instance of Document object using document
builder factory.
* Create root element Personnel.
* For each item in the list create a Book element.
* Serialize DOM to FileOutputStream to generate the
xml file.
HOW TO GENERATE
 XML FROM JAVA

   Show some codes
JSON
     JAVASCRIPT OBJECT
         NOTATION

 Is a language independent text format which is fast
               and easy to understand.
Its very easy to manipulate JSON document than say
                        XML
JSON
      PACKAGES FOR JAVA
org.json.* - http://www.json.org/java/index.html

Google-gson - http://code.google.com/p/google-gson/

Jackson Java JSON - Processor - http://jackson.codehaus.org/
Jettison - http://jettison.codehaus.org/
SAMPLE JSON
              DOCUMENT
 Syntax                   Sample
                   {
Object
                       “name”: “neem tree”
{
                       “height”: “3”,
   String: value
                       “benefits”: [
},
                          “medicine”,
array
                          “wind break”,
[
                         “shade”
   value
                       ]
],
                   }
EXAMPLE PARSING
 JSON DOCUMENT

   Show some codes
HOW TO GENERATE
JSON DOCUMENT FROM
        JAVA
     Show some codes
HOW TO GENERATE
JSON DOCUMENT FROM
        JAVA
     Show some codes

More Related Content

What's hot

Introductionto xslt
Introductionto xsltIntroductionto xslt
Introductionto xslt
Kumar
 
Xml serialization
Xml serializationXml serialization
Xml serialization
Raghu nath
 
06 xml processing-in-.net
06 xml processing-in-.net06 xml processing-in-.net
06 xml processing-in-.net
glubox
 
04 data accesstechnologies
04 data accesstechnologies04 data accesstechnologies
04 data accesstechnologies
Bat Programmer
 

What's hot (20)

Json
Json Json
Json
 
Introductionto xslt
Introductionto xsltIntroductionto xslt
Introductionto xslt
 
Python xml processing
Python   xml processingPython   xml processing
Python xml processing
 
Ajax
AjaxAjax
Ajax
 
5 xml parsing
5   xml parsing5   xml parsing
5 xml parsing
 
XML
XMLXML
XML
 
Connecting to a REST API in iOS
Connecting to a REST API in iOSConnecting to a REST API in iOS
Connecting to a REST API in iOS
 
Xml serialization
Xml serializationXml serialization
Xml serialization
 
Xml & Java
Xml & JavaXml & Java
Xml & Java
 
06 xml processing-in-.net
06 xml processing-in-.net06 xml processing-in-.net
06 xml processing-in-.net
 
Difference between xml and json
Difference between xml and jsonDifference between xml and json
Difference between xml and json
 
Using Webservice in iOS
Using Webservice  in iOS Using Webservice  in iOS
Using Webservice in iOS
 
Xml writers
Xml writersXml writers
Xml writers
 
Xslt attributes
Xslt attributesXslt attributes
Xslt attributes
 
Web Development Course - AJAX & JSON by RSOLUTIONS
Web Development Course - AJAX & JSON by RSOLUTIONSWeb Development Course - AJAX & JSON by RSOLUTIONS
Web Development Course - AJAX & JSON by RSOLUTIONS
 
Simple xml in .net
Simple xml in .netSimple xml in .net
Simple xml in .net
 
Xslt mule
Xslt   muleXslt   mule
Xslt mule
 
Lec 7
Lec 7Lec 7
Lec 7
 
Get docs from sp doc library
Get docs from sp doc libraryGet docs from sp doc library
Get docs from sp doc library
 
04 data accesstechnologies
04 data accesstechnologies04 data accesstechnologies
04 data accesstechnologies
 

Similar to Xml And JSON Java

java API for XML DOM
java API for XML DOMjava API for XML DOM
java API for XML DOM
Surinder Kaur
 
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
Hicham QAISSI
 
Xml Java
Xml JavaXml Java
Xml Java
cbee48
 
Working with XML and JSON Serializing
Working with XML and JSON SerializingWorking with XML and JSON Serializing
Working with XML and JSON Serializing
ssusere19c741
 
Xml Presentation-1
Xml Presentation-1Xml Presentation-1
Xml Presentation-1
Sudharsan S
 

Similar to Xml And JSON Java (20)

Ch23
Ch23Ch23
Ch23
 
Ch23 xml processing_with_java
Ch23 xml processing_with_javaCh23 xml processing_with_java
Ch23 xml processing_with_java
 
Dom
Dom Dom
Dom
 
Understanding XML DOM
Understanding XML DOMUnderstanding XML DOM
Understanding XML DOM
 
java API for XML DOM
java API for XML DOMjava API for XML DOM
java API for XML DOM
 
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
XMLXML
XML
 
Xml Java
Xml JavaXml Java
Xml Java
 
Processing XML with Java
Processing XML with JavaProcessing XML with Java
Processing XML with Java
 
JSON-(JavaScript Object Notation)
JSON-(JavaScript Object Notation)JSON-(JavaScript Object Notation)
JSON-(JavaScript Object Notation)
 
Basics of JSON (JavaScript Object Notation) with examples
Basics of JSON (JavaScript Object Notation) with examplesBasics of JSON (JavaScript Object Notation) with examples
Basics of JSON (JavaScript Object Notation) with examples
 
2310 b 12
2310 b 122310 b 12
2310 b 12
 
Xml
XmlXml
Xml
 
Working with XML and JSON Serializing
Working with XML and JSON SerializingWorking with XML and JSON Serializing
Working with XML and JSON Serializing
 
Xml11
Xml11Xml11
Xml11
 
Xml Presentation-1
Xml Presentation-1Xml Presentation-1
Xml Presentation-1
 
Xml
XmlXml
Xml
 
XML.ppt
XML.pptXML.ppt
XML.ppt
 
Xml processing in scala
Xml processing in scalaXml processing in scala
Xml processing in scala
 
Xml processing in scala
Xml processing in scalaXml processing in scala
Xml processing in scala
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
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
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
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
 
[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
 
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
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

Xml And JSON Java

  • 1. XML & JSON PARSERS IN JAVA Henry Addo E-mail: addhen@gmail.com Twitter: http://twitter.com/eyedol
  • 2. WHAT IS PARSING A XML / JSON DOCUMENT Reading a xml / json document to determine its structure or extract its content.
  • 3. DIFFERENT WAYS OF PARSING XML DOCUMENT * DOM - Document Object Model - org.w3c.dom.* * JDOM - Open source version of DOM - org.jdom.* * SAX - Simple API for XML - org.xml.sax.* * StAX - Streaming API for XML - java.xml.stream.*
  • 4. DOM * Document Object Model - An interface for working with the structure of an xml document. * Developed by w3c to make life easier for the programmer.
  • 5. DOM TREE * XML is loaded in hierarchical data structure( the DOM tree ). * Everything in a DOM tree is a Node of one type or another type. * Element: Represent an XML element in the source document. * Attr: Represents an attribute of an XML element. * Text: The content of an element of an XML element. * Document: Represents the entire XML document.
  • 6. COMMON DOM METHODS * Document.getDocumentElement(); returns the root of the DOM tree * Node.getFirstChild() & Node.getLastChild(); returns the first or last child of a given Node. * Node.getNextSibling() & Node.getPreviousSibling(); returns the next or previous sibling of a given Node. * Element.getAttribute( String attrName); returns the value of the attribute named attrName of a given Element.
  • 7. SAX Simple API for XML - Designed to address the issues with DOM. * Memory hog * Excess objects * Build entire document tree for code to work
  • 8. HOW SAX WORKS SAX parsers tells you when it finds things in the XML document using events. * It sends you events as it finds things in an XML document. * It gives the option for creating objects as when needed. * It sends events right on the onset, doesn’t have to wait for the parser to finish reading the document.
  • 9. COMMON SAX EVENTS SAX API defines number of events. Write code to do things in response to those events. * startDocument(); prompts that it has found the start of the document. * endDocument(); prompts that it has found the end of the document. * startElement(...); found a start tag-- name,values,attributes of the element. * characters(...); found some text. char array * endElement(..); found an end tag--name + assoc. name space
  • 10. CHOOSING XML PARSER DOM * You Need to know a lot about the document structure * You Need to change the structure of the document SAX * You don’t have much machine memory * You only need a few elements or attributes from the XML document StAX * You need to do xml processing on mobile devices
  • 11. EXAMPLE PARSING XML WITH DOM * Get a document builder using document builder factory and parse the xml file to create a DOM obj. * Get a list of employee elements from the DOM. * For each employee element get the id, name, age and type. * Iterate through the list and print the employees details.
  • 12. EXAMPLE PARSING XML WITH DOM Show some codes
  • 13. EXAMPLE PARSING XML WITH SAX * Create a SAX parser and parse the xml. * In the event handler create the employee object. * Print out the data.
  • 14. EXAMPLE PARSING XML WITH SAX Show some codes
  • 15. HOW TO GENERATE XML FROM JAVA This time generate an xml file instead of reading it. * Load Data. * Get an instance of Document object using document builder factory. * Create root element Personnel. * For each item in the list create a Book element. * Serialize DOM to FileOutputStream to generate the xml file.
  • 16. HOW TO GENERATE XML FROM JAVA Show some codes
  • 17. JSON JAVASCRIPT OBJECT NOTATION Is a language independent text format which is fast and easy to understand. Its very easy to manipulate JSON document than say XML
  • 18. JSON PACKAGES FOR JAVA org.json.* - http://www.json.org/java/index.html Google-gson - http://code.google.com/p/google-gson/ Jackson Java JSON - Processor - http://jackson.codehaus.org/ Jettison - http://jettison.codehaus.org/
  • 19. SAMPLE JSON DOCUMENT Syntax Sample { Object “name”: “neem tree” { “height”: “3”, String: value “benefits”: [ }, “medicine”, array “wind break”, [ “shade” value ] ], }
  • 20. EXAMPLE PARSING JSON DOCUMENT Show some codes
  • 21. HOW TO GENERATE JSON DOCUMENT FROM JAVA Show some codes
  • 22. HOW TO GENERATE JSON DOCUMENT FROM JAVA Show some codes