Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse XML file on BlackBerry

I want to know how to parse XML data on a BlackBerry.

I read somewhere that JSON is good method to parse xml data.

Are there any tutorials to parse XML data using JSON, or any other mechanism?

like image 384
MohammedYakub Moriswala Avatar asked May 03 '10 12:05

MohammedYakub Moriswala


Video Answer


2 Answers

Parsing XML in Blackberry

Simple API for XML (SAX) was developed by the members of a public mailing list (XML-DEV).It gives an event based approach to XML parsing. It means that instead of going from node to node, it goes from event to event. SAX is an event driven interface. Events include XML tag, detecting errors etc, J2ME SAX - see BlackBerry/J2ME - SAX parse collection of objects with attributes

XML pull parser - It is optimal for applications that require fast and a small XML parser. It should be used when all the process has to be performed quickly and efficiently to input elements kXML - J2ME pull parser - see Better approach for XML Creation in Blackberry

Parsing XML with JSON

Blackberry standard for JSON parsing is JSON ME

No idea... JSON can be represented and transported as a XML, but not vice versa.

XML (Extensible Markup Language) is a set of rules for encoding documents electronically. It is defined in the XML 1.0 Specification produced by the W3C, and several other related specifications, all gratis open standards.

XML sample:

<?xml version="1.0" encoding='UTF-8'?>
<painting>
  <img src="madonna.jpg" alt='Foligno Madonna, by Raphael'/>
  <caption>This is Raphael's "Foligno" Madonna, painted in
    <date>1511</date>–<date>1512</date>.
  </caption>
</painting>

JSON (an acronym for JavaScript Object Notation) is a lightweight text-based open standard designed for human-readable data interchange. It is derived from the JavaScript programming language for representing simple data structures and associative arrays, called objects (the “O” in “JSON”). Despite its relationship to JavaScript, it is language-independent, with parsers available for virtually every programming language.

JSON sample:

{
     "firstName": "John",
     "lastName": "Smith",
     "age": 25,
     "address": {
         "streetAddress": "21 2nd Street",
         "city": "New York",
         "state": "NY",
         "postalCode": "10021"
     },
     "phoneNumber": [
         { "type": "home", "number": "212 555-1234" },
         { "type": "fax", "number": "646 555-4567" }
     ]
 }

Basically if your XML is a strong equivalent of JSON, like:

<Person>
  <firstName>John</firstName>
  <lastName>Smith</lastName>
  <age>25</age>
  <address>
    <streetAddress>21 2nd Street</streetAddress>
    <city>New York</city>
    <state>NY</state>
    <postalCode>10021</postalCode>
  </address>
  <phoneNumber type="home">212 555-1234</phoneNumber>
  <phoneNumber type="fax">646 555-4567</phoneNumber>
</Person>

there is a possibility to parse such XML with JSON.

like image 99
Maksym Gontar Avatar answered Oct 04 '22 05:10

Maksym Gontar


Parsing is usually done using a 3rd party library that can be loaded into a project. If you're using XML, I've used a library called the kXML parser. Setting it up can be a pain, but there are instructions on how to set it up here -

http://supportforums.blackberry.com/t5/Java-Development/Tutorial-How-To-Use-3rd-Party-Libraries-in-your-Applications/m-p/177543

http://www.craigagreen.com/index.php?/Blog/blackberry-and-net-webservice-tutorial-part-1.html

Using kXML is pretty straight forward. This tutorial here explains how to parse an XML file -

http://www.roseindia.net/j2me/kxml/j2me-xml-parser.shtml

Edit: Whoops, the first tutorial in the next post has a pretty comprehensive overview on xml parsing on kxml2. So my post's kinda redundant.

like image 40
Tejaswi Yerukalapudi Avatar answered Oct 04 '22 06:10

Tejaswi Yerukalapudi