Xml And JSON Java

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

    Xml And JSON Java - Presentation Transcript

    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
    SlideShare Zeitgeist 2009

    + Henry AddoHenry Addo Nominate

    custom

    1007 views, 0 favs, 0 embeds more stats

    How to manipulate XML and JSON document

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 1007
      • 1007 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 24
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories

    Tags