parsing xml & json in apex
DESCRIPTION
Slides about tips of XML and JSON parsing in Apex. These slides were used in Force.com chennai meetup.TRANSCRIPT
Handling XML / JSON in Apex
t: @abhinavguptas
www: tgerm.com
Agenda
Motivation
Classic approaches
Better approaches
Make XML/JSON Parsing Simple
Motivation
Developers not aware of recent platform changes.
Google gives pointers to obsolete APIs.
Obsolete APIs are slow and hurt on governor limits like “script statements consumption”
New APIs use platform capabilities for resource hungry tasks like parsing.
Why we are discussing this ?
Native Lib vs
Apex Lib
Native Lib ?
Provided by Force.com platform aka System libraries, like XMLStreamReader
Doesn’t hurt on resources like script statements consumption.
Apex Lib ?
Wrapper on Native Lib, like XMLDOM.cls
Written from scratch in Apex only, like JSONObject.cls
XML Parsing
Sample XML
<books>
<book author="Chatty">Foo bar</book>
<book author="Sassy">Baz </ book>
</books>
Apex Model
public class Book {
String name;
String author;
}
Parse XML to this Model
Classic Approaches
XMLStream Classes
Native Lib
Stream based approach to parse XML
XMLDom.cls (OpenSource)
Apex Lib, wrapper on above XMLStream classes
Create in memory DOM for XML Structure.
Written by Ron Hess (Salesforce)
BetterApproaches
Dom.Document/XmlNode Classes
Native Lib
Came in Spring’10 release
DOM based approach to parse XML
FastXmlDom (OpenSource)
Apex Lib, wrapper on above Dom.XmlNode classes
Exposes pretty common DOM API for XML Parsing
Tested & used in many projects.
Sample Code
Over to sublime code snippet
Lets see some code about all of these approaches
XmlStream Classes
Pros
Native Lib (Not an Apex implementation)
Heap/Memory efficient like usual stream parsers.
Good for big XML string, parse only the required part of XML.
Cons
Complex code to parse XML.
Not so developer friendly
Sometimes can consume lots of script statements in iterations for big XMLs.
Pros/Cons
XMLDOM.cls
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.
Cons
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.
Dom class Document &
XmlNode
Pros
Native DOM Lib
Relatively simpler to use as compared to XMLStreamReader.
Consumes least script statements.
Cons
DOM API is not following w3c model, so learning curve for developers.
Mandates namespace usage, that complicates simple xml parsing.
FastXMLDom
Pros
Exposes W3C DOM API (no learning curve for developers)
Simpler to use as compared to XMLStreamReader and DOM Classes.
Consumes less script statements.
Cons
An Apex Lib, so consumes a bit more script statements and heap as compared to Dom.XmlNodePros/Cons
JSON Parsing
Classic Approaches
JSONObject.cls
Apex Lib
Tokenizes the string to create in memory JSON Structure
Written by Ron Hess(Salesforce)
System.JSONParser
Native lib
Streaming and token based parsing of JSON
BetterApproaches
JSON
Native Lib
Mapped strong and loose parsing with Apex UDT(User Defined Types) and Collections like Maps.
Sample Code
JSONObject
Not required and please don’t use it any more.
JSONParser
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_jsonparser.htm
JSON
http://www.tgerm.com/2011/10/winter12-jsonparser-serialize.html
JSONObject.cls
Pros
None as of now
Cons
Consumes script statements and heap for large JSON files
Not as stable as compared to Native libs.
JSONParser
Pros
Native lib, hurts less on resources.
Good option when JSON automated serialization/deserialization fails.
Came with Winter’12 release
Cons
Complex code and less developer friendly way to parse.
JSON
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 versa
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.
General Tips
Searching for solutions in this order
Consult platform guides, like apex dev guide.
developer.force.com
Salesforce.stackexchange.com
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.
Q & A
Resources
You will get all the link, as I will share the slides
Use “Apex Developer Guide” for native libs, like:
XmlStreamReader
Dom classes
JSON Parser
JSON
XMLDOM.cls
http://developer.force.com/projectpage?id=a0630000002ahp5AAA
Fast XML DOM
http://code.google.com/p/apex-fast-xml-dom/
JSONObject.cls
http://code.google.com/p/apex-library/source/browse/trunk/JSONObject/src/classes/JSONObject.cls
Thanks !@abhinavguptas
www.tgerm.com