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

Posts: 137
Registered: ‎06-01-2009
My Device: Not Specified

How to convert document to a String

How can I convert XML document into a String. I need to save it as string. Can you please post sample code.



Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: How to convert document to a String

Check the example 'xmldemo'  (C:\Program Files\Research In Motion\BlackBerry JDE 4.3.0\samples\com\rim\samples\device)

that is provided with the JDE. 








Posts: 47
Registered: ‎06-05-2009
My Device: Not Specified

Re: How to convert document to a String

[ Edited ]


       Looking at the XMLDemo it doesn't look like something that is a production quality solution. 

Is there no API or library or something available from RIM that would provide a solution for extracting the contents of an XML DOM Document to a string?


It looks like my only option is to string together substrings of XML tags etc.



Message Edited by aleitzinger on 07-28-2009 10:48 PM
Posts: 47
Registered: ‎06-05-2009
My Device: Not Specified

Re: How to convert document to a String

Hi, I'll answer my own post since it took 3HRS of Googling to find an example.


I am using JDE 4.5 and it is possible to extract the XML in a DOM Document.If you want to output to something other string swap the ByteArrayOutputStream with another OutputStream subclass.





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 net.rim.device.api.system.*;
import java.util.Vector;
import java.io.*;
import net.rim.device.api.i18n.*;
import net.rim.blackberry.api.mail.*;
import net.rim.blackberry.api.mail.event.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import net.rim.device.api.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import java.util.*;


    public void ConvertToString(Document pdocXMLDocument) {
        Document myDocument = pdocXMLDocument;/* something */;
        ByteArrayOutputStream os = new ByteArrayOutputStream();/* 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.setPrintCompressedOutput()
        // writer.setPreserveSpacing()
        // writer.setExpandingEntities(boolean)
        // writer.setEntityResolver(EntityResolver)
        DOMInternalRepresentation.parse(myDocument, writer);
        return os.toString();    // Here's the string containing the XML

New Member
Posts: 1
Registered: ‎06-14-2012
My Device: qwerty
My Carrier: qwe

Re: How to convert document to a String

thanks a lot:-) u r awesome.....u saved my time....so sweet of you....code works charm