Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
Posts: 59
Registered: ‎03-10-2009
My Device: Not Specified

How to create xml file using RIM api

Hi my requriement is that i need to create xml file using RIM api.

 

XML File:

 

 

<?xml version="1.0" encoding="UTF-8"?>
<traffic>
<sessionID>A6EEA5FFFCCC2D7D953A28528A41D446</sessionID>

<phone_call>

<id>3595570111509312612500</id><imsi>238012950711722</imsi><dme_user></dme_user><roaming>false</roaming><mcc><![CDATA[238]]></mcc><mnc><![CDATA[01]]></mnc><operator_name><![CDATA[TDC MOBIL]]></operator_name><phone_number><![CDATA[+4530642229]]></phone_number><direction>0</direction><started><![CDATA[2009-05-26T10:55:20+02:00]]></started><duration><![CDATA[3]]></duration>

</phone_call>
</traffic>

 

Code:

 

private static DocumentBuilderFactory dBFactory = null;

 

if(dBFactory == null) {

dBFactory = DocumentBuilderFactory.newInstance();
}

DocumentBuilder dBuilder = dBFactory.newDocumentBuilder();
Document myDocument = dBuilder.newDocument();
Element traffic = myDocument.createElement("traffic");

 

can you please help me in this how to create xml file as mentioned above. i want to create a file not for parsing.

BlackBerry Development Advisor
Posts: 15,784
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: How to create xml file using RIM api

You can create a string that contains your XML content and then use a FileConnection to write the string out to a file that is stored on the BlackBerry smartphone.  Refer to the fileexmplorerdemo included with the BlackBerry JDE for an example on the use of a FileConnection.
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: How to create xml file using RIM api

Since you're using a DOM representation, you can do something like this:

 

 

Document myDocument = /* something */;

OutputStream os = /* where you want the XML to go */;

XMLWriter writer = new XMLWriter(os); writer.setPrettyPrint(); // or setPrintCompressedOutput();

// configure writer as desired by calling one or more of:

// writer.setPrettyPrint()

// writer.setPrintCompressedOutput()

// writer.setPreserveSpacing()

// writer.setExpandingEntities(boolean)

// writer.setEntityResolver(EntityResolver)

DOMInternalRepresentation.parse(myDocument, writer);

 

 

 




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
New Developer
Posts: 59
Registered: ‎03-10-2009
My Device: Not Specified

Re: How to create xml file using RIM api



Hi



Felt very happy Smiley Happy by seeing your reply. Thank you very much for your response.


I have one more doubt that to create an xml file i'm using below logic, each and every element i'm creating by using Document object is it correct way i'm proceeding or not?


Code:

DocumentBuilder dBuilder = dBFactory.newDocumentBuilder();
            Document myDocument = dBuilder.newDocument();
            Element e_traffic = myDocument.createElement("traffic");
            Element e_sessionID = myDocument.createElement("sessionID");
            e_sessionID.setTextContent("123");
            e_traffic.appendChild(e_sessionID);



Element e_message = myDocument.createElement("message");
               
                Element e_id = myDocument.createElement("id");
                Element e_messageType = myDocument.createElement("message_type");
                Element e_imsi = myDocument.createElement("imsi");
                Element e_dmeUser = myDocument.createElement("dme_user");
                Element e_roaming = myDocument.createElement("roaming");
                Element e_mcc = myDocument.createElement("mcc");
                Element e_mnc = myDocument.createElement("mnc");
                Element e_operatorName = myDocument.createElement("operator_name");
                Element e_started = myDocument.createElement("started");
                Element e_direction = myDocument.createElement("direction");
                Element e_phoneNumber = myDocument.createElement("phone_number");
                Element e_parts = myDocument.createElement("parts");
               
                e_id.setTextContent(id);
                e_messageType.setTextContent(messageType);
                e_imsi.setTextContent(imsi);
                e_dmeUser.setTextContent(dmeUser);
                e_roaming.setTextContent(roaming);
                e_mcc.setTextContent(mcc);
                e_mnc.setTextContent(mnc);
                e_operatorName.setTextContent(operatorName);
                e_started.setTextContent(started);
                e_direction.setTextContent(direction);
                e_phoneNumber.setTextContent(phoneNumber);
                e_parts.setTextContent(parts);
               
                e_message.appendChild(e_id);
                e_message.appendChild(e_messageType);
                e_message.appendChild(e_imsi);
                e_message.appendChild(e_dmeUser);
                e_message.appendChild(e_roaming);
                e_message.appendChild(e_mcc);
                e_message.appendChild(e_mnc);
                e_message.appendChild(e_operatorName);
                e_message.appendChild(e_started);
                e_message.appendChild(e_direction);
                e_message.appendChild(e_phoneNumber);
                e_message.appendChild(e_parts);
               
                e_traffic.appendChild(e_message);
Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: How to create xml file using RIM api

Your approach is generally correct. However, some of the methods you are using (like setTextContent), are not available before DOM level 3. I think anything earlier than JDE 4.6 only supports DOM level 2. If you are only targeting the more recent versions of the device software, you're ok, but If you plan to port to earlier versions, you'll have to work at a lower level (explicitly create Text nodes, etc.)

 

Also, I think you need to call myDocument.append(e_traffic) at some point. Smiley Happy




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
New Developer
Posts: 59
Registered: ‎03-10-2009
My Device: Not Specified

Re: How to create xml file using RIM api

 

Hi Thank you very much for your information. 

 

i'm using JDE version  4.7.0.41, when i'm trying to create XML file with above code suggested by you, still XML file is not getting create. Below is my code can you please check and correct my code if any changes requried.

 

 

CODE:


import net.rim.device.api.xml.parsers.DocumentBuilderFactory;
import net.rim.device.api.xml.parsers.DocumentBuilder;
import net.rim.device.api.xml.jaxp.XMLWriter;
import net.rim.device.api.xml.jaxp.DOMInternalRepresentation;

import javax.microedition.io.file.*;
import javax.microedition.io.*;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

class XMLTest {
    private static DocumentBuilderFactory dBFactory = null;
   
    FileConnection fconn = null;
    OutputStream os = null;

    XMLTest() {            //    Constructor
        if(dBFactory == null) {
                dBFactory = DocumentBuilderFactory.newInstance();
            }

            try {
                fconn = (FileConnection) Connector.open("file:///SDCard/BlackBerry/DME.xml", Connector.READ_WRITE);
                if (!fconn.exists())
                    fconn.create();
                   
                os = fconn.openOutputStream();
            }
            catch (Exception e) {
                System.out.println("Oh noes!!1! " + e.toString());
            }       
    }

    public void CreateXMLFile() {
        Element e_message = myDocument.createElement("message");
               
                Element e_id = myDocument.createElement("id");
                Element e_messageType = myDocument.createElement("message_type");
                Element e_imsi = myDocument.createElement("imsi");
                Element e_dmeUser = myDocument.createElement("dme_user");
                Element e_roaming = myDocument.createElement("roaming");
                Element e_mcc = myDocument.createElement("mcc");
                Element e_mnc = myDocument.createElement("mnc");
                Element e_operatorName = myDocument.createElement("operator_name");
                Element e_started = myDocument.createElement("started");
                Element e_direction = myDocument.createElement("direction");
                Element e_phoneNumber = myDocument.createElement("phone_number");
                Element e_parts = myDocument.createElement("parts");
               
                e_id.setTextContent(id);
                e_messageType.setTextContent(messageType);
                e_imsi.setTextContent(imsi);
                e_dmeUser.setTextContent(dmeUser);
                e_roaming.setTextContent(roaming);
                e_mcc.setTextContent(mcc);
                e_mnc.setTextContent(mnc);
                e_operatorName.setTextContent(operatorName);
                e_started.setTextContent(started);
                e_direction.setTextContent(direction);
                e_phoneNumber.setTextContent(phoneNumber);
                e_parts.setTextContent(parts);
               
                e_message.appendChild(e_id);
                e_message.appendChild(e_messageType);
                e_message.appendChild(e_imsi);
                e_message.appendChild(e_dmeUser);
                e_message.appendChild(e_roaming);
                e_message.appendChild(e_mcc);
                e_message.appendChild(e_mnc);
                e_message.appendChild(e_operatorName);
                e_message.appendChild(e_started);
                e_message.appendChild(e_direction);
                e_message.appendChild(e_phoneNumber);
                e_message.appendChild(e_parts);
               
                e_traffic.appendChild(e_message);

        try {
            XMLWriter writer = new XMLWriter(os);
            writer.setPrintCompressedOutput();
            DOMInternalRepresentation.parse(myDocument, writer);
        }
        catch (Exception e) {
            e.getMessage();
        }
        finally {
            try {
                if(os != null)
                os.close();
                if(fconn != null)
                fconn.close();
            }
            catch (Exception e) {
                e.getMessage();
            }
        }
    }
}