Random thoughts shooting out of volatile mind
Managing data using XML in Blackberry
XML files are good medium for storing and exchanging the data over the network and they are widely used in many technologies to store and exchange the data. A good example which I can provide here are the web services, configuration files of many java based application servers and java based web projects and also in the API's of famous social network sites like twitter.
It is easy to store the data as a XML file in application that you develop for the blackberry. Though there are storage options like record store and persistent objects available in Blackberry I prefer the XML files over them. Blackberry provides SAX parser for parsing and modifying XML but I felt little difficult to use SAX api so I choose KXML as best alternative for SAX. I'm going to tell how to create a XML file using KXML without loosing a sweat and parsing the already existing XML files.
1. Creating a simple XML file using KXML2 api's

Consider a simple XML file as shown below.
<? xml version="1.0" encoding="UTF-8" ?>
<company> <employee id="1"> <fname>Vasudev</fname> <lname>Kamath</lname> <address>Karkala</address> </employee> ... </company>


Use the below mentioned code for creating a xml file like above, the code itself is self explanatory.
Document d = new Document();
Element root = d.createElement("","company");
Element employee = d.createElement("","employee");
e.setAttribute("","id","1");

Element fname = d.createElement("","fname");
fname.addChild(Node.TEXT,"Vasudev");


Element lname = d.createElement("","lname");
lname.addChild(Node.TEXT,"Kamath");

Element address = d.createElement(Node.TEXT,"address");
address.addChild(Node.TEXT,"Karkala");

employee.addChild(Node.ELEMENT,fname);
employee.addChild(Node.ELEMENT,lname);
employee.addChild(Node.ELEMENT,address);

root.addChild(Node.ELEMENT,employee);
d.addChild(Node.ELEMENT,root);

Here first i'm creating a Document which is going to represent my XML file and then i'm creating the nodes required in my XML. The Node.ELEMENT represents and entire XML tag and its contents. For example is an XML Element and if you look at tag the content b/w opening and closing tag is simple text and is represented using Node.TEXT.

Next step is to write this document to the file for which we need KXmlSerializer which is part of KXML2 apis. Below is the code to write the document to file
String fileName = "file:///SDCard/Blackberry/company.xml"
DataOutputStream os = null;
FileConnection fc = null

try
{
fc = (FileConnection)Connector.open(filename,Connector.READ_WRITE);
if (! fc.exists())
fc.create();

os = fconn.openDataOutputStream();
KXmlSerializer serializer = new KXmlSerializer();
serializer.setOutput(os, "UTF-8");
d.write(serializer);
}

catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
As you can see it is very simple to create XML files using KXML2 api's and possibility of missing few ending tags while writing XML file is very less since API itself takes care of creating XML files.

Ok next step is parsing the XML file and it is also pretty simple.I'll tell how to read the XML file into a Document() object and parse it and functions required to get a particular node and its child node remaining logic you need to implement depending on your requirements. Below is the code for reading a file to Document and parsing it.
Document d= new Document();
FileConnection fc = null;
DataInputStream is = null;
try {
fc = (FileConnection) Connector.open(fileName, Connector.READ);
is = fc.openDataInputStream();

KXmlParser parser = new KXmlParser();
parser.setInput(is, "UTF-8");
d.parse(parser);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This code gets the entire XML file to Document() object d and also parses it !!. Amazing rite you didn't even do a file read operation and yet have the whole content into Document() object. Well this is why I liked KXML more than SAX. If you are interested in seeing how it does you can get the source code of KXML2 api's and see. Now you have XML file in Document() object next how to get the required node? It is very simple you have function called getElement(String namespace,name) which gets particular node from its name in the XML file. There are many such functions available in KXML2 apis for more information look at its java doc here.

Last thing which I want to mention here is KXML library is meant not only for Blackberry but all those phones which supports J2ME. You need to have either KXML2 jar as library in your project or if you want to debug the KXML2 then you need to KXML2 source code and XML Pull parser source code. For more information on adding external library to your project please look at KB article in Blackberry website.
Reference : Stack Overflow forums

Posted by: copyninja on Sunday, 11 October 2009

blog comments powered by Disqus
Fork me on GitHub