Parsing XML & JSON in Apex


Published on

Slides about tips of XML and JSON parsing in Apex. These slides were used in chennai meetup.

Published in: Technology
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Your introductory or title slide should convey the overall “feeling” and focus of your presentation. For instance, I typically present about small-business trends, new business ideas, growth opportunities or other positive trends. In this sample presentation, I’m talking about new business ideas, so I used a sun graphic in this slide template to convey a positive feeling. Personalize this slide template with your company’s logo. To add a logo to all slides, place it on the Slide Master. To access the Slide Master, on the Themes tab of the Ribbon, click Edit Master and then click Slide Master.Disclaimer: You understand that Microsoft does not endorse or control the content provided in the following presentation. Microsoft provides this content to you for informational purposes only; it is not intended to be relied upon as business or financial advice. Microsoft does not guarantee or otherwise warrant the accuracy or validity of this information and encourages you to consult with a business or financial professional as appropriate.RIEVA LESONSKY Founder and President, GrowBiz Media RievaLesonsky is founder and president of GrowBiz Media, a content and consulting company specializing in covering small businesses and entrepreneurship. A nationally known speaker and authority on entrepreneurship, Lesonsky has been covering America’s entrepreneurs for nearly 30 years. Before co-founding GrowBiz Media, Lesonsky was Editorial Director of Entrepreneur Magazine.
  • Parsing XML & JSON in Apex

    1. 1. Motivation Classic approaches Better approaches AgendaMake XML/JSON Parsing Simple
    2. 2. Developers not aware of recent platform changes. Google gives pointers to obsolete APIs. Obsolete APIs are slow and hurt on governor limits like Motivation “script statements consumption” New APIs use platform capabilities for resource hungry tasks like parsing.Why we are discussing this ?
    3. 3. Native Lib ? Provided by platform aka System libraries, like XMLStreamReader Doesn’t hurt on resources likeNative Lib script statements consumption. vsApex Lib Apex Lib ? Wrapper on Native Lib, like XMLDOM.cls Written from scratch in Apex only, like JSONObject.cls
    4. 4. XML Parsing
    5. 5. <books> <book author="Chatty">Foo bar</book> <book author="Sassy">Baz </ book>Sample XML </books>
    6. 6. public class Book { String name; String author; }Apex ModelParse XML to this Model
    7. 7. XMLStream Classes Native Lib Stream based approach to parse XML Classic XMLDom.cls (OpenSource)Approaches Apex Lib, wrapper on above XMLStream classes Create in memory DOM for XML Structure. Written by Ron Hess (Salesforce)
    8. 8. Dom.Document/XmlNode Classes Native Lib Came in Spring’10 release DOM based approach to parse XML BetterApproaches FastXmlDom (OpenSource) Apex Lib, wrapper on above Dom.XmlNode classes Exposes pretty common DOM API for XML Parsing Tested & used in many projects.
    9. 9. Over to sublime code snippetSample CodeLets see some code about all of these approaches
    10. 10. Pros Native Lib (Not an Apex implementation) Heap/Memory efficient like usual stream parsers.XmlStream Good for big XML string, parse only the required part of XML. Classes Cons Complex code to parse XML. Not so developer friendly Sometimes can consume lots of script statements in Pros/Cons iterations for big XMLs.
    11. 11. Pros Simple W3C DOM library, which developers are used to of using in HTML DOM as well. Simple and more readable code for XML Parsing. ConsXMLDOM.cls Creates in memory DOM for complete XML structure, so heavy on heap Consumes too many script statements in the above process. Not suitable for large XML parsing.
    12. 12. Pros Native DOM Lib Relatively simpler to use as compared to XMLStreamReader. Dom class Consumes least scriptDocument & statements. Cons XmlNode DOM API is not following w3c model, so learning curve for developers. Mandates namespace usage, that complicates simple xml parsing.
    13. 13. Pros Exposes W3C DOM API (no learning curve for developers) Simpler to use as compared to XMLStreamReader and DOM Classes.FastXMLDom Consumes less script statements. Cons An Apex Lib, so consumes a bit more script statements and heap as compared to Dom.XmlNode Pros/Cons
    14. 14. JSON Parsing
    15. 15. JSONObject.cls Apex Lib Tokenizes the string to create in memory JSON Structure Written by Ron Classic Hess(Salesforce) System.JSONParserApproaches Native lib Streaming and token based parsing of JSON
    16. 16. JSON Native Lib Mapped strong and loose parsing with Apex UDT(User Defined Types) and Collections like Maps. BetterApproaches
    17. 17. JSONObject Not required and please don’t use it any more. JSONParser developer/docs/apexcode/ConSample Code tent/apex_methods_system_js onparser.htm JSON /winter12-jsonparser- serialize.html
    18. 18. Pros None as of now Cons Consumes script statementsJSONObject.cls and heap for large JSON files Not as stable as compared to Native libs.
    19. 19. Pros Native lib, hurts less on resources. Good option when JSON automated serialization/deserialization fails.JSONParser Came with Winter’12 release Cons Complex code and less developer friendly way to parse.
    20. 20. Pros Native lib, hurts less on resources. Serialize/De-serialize big JSON strings in one line of code. Easy transformation of JSON from String > UDT and vice versaJSON Gives option of loose parsing to Maps i.e. deserializeUntyped() Came with winter’12 release Improved a bit with every next release Cons Special care if required if JSON contains reserved words or conflicting stuff with Apex.
    21. 21. Searching for solutions in this order Consult platform guides, like apex dev guide. GoogleGeneral Tips Stay on latest and greatest API Don’t download pdf for documentation use online guides. Use latest API version of Apex class and VF page when creating new ones.
    22. 22. Q&A
    23. 23. Use “Apex Developer Guide” for native libs, like: XmlStreamReader Dom classes JSON Parser JSON XMLDOM.cls ge?id=a0630000002ahp5AAA Resources Fast XML DOM xml-dom/ JSONObject.cls will get all the link, as I will library/source/browse/trunk/JSONO share the slides  bject/src/classes/JSONObject.cls
    24. 24. @abhinavguptasThanks !
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.