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

New Developer
Posts: 1
Registered: ‎04-03-2009
My Device: Not Specified

XML Attachment



 Looking for a way to retireve an xml attachment then automatically process the xml document and display it. I am stuck on how to retreive the xml attachment into a inputstream.

Here is what I have so far:



import java.io.InputStream; import org.w3c.dom.Document; import net.rim.blackberry.api.mail.Folder; import net.rim.blackberry.api.mail.Message; import net.rim.blackberry.api.mail.Multipart; import net.rim.blackberry.api.mail.event.FolderEvent; import net.rim.device.api.notification.NotificationsManager; import net.rim.device.api.system.RuntimeStore; import net.rim.device.api.ui.component.Dialog; import net.rim.device.api.xml.parsers.DocumentBuilder; import net.rim.device.api.xml.parsers.DocumentBuilderFactory; public class test extends UiApplication implements FolderListener{ ... public void messagesAdded(FolderEvent e) { // Create a store to save the xml document into RuntimeStore store = RuntimeStore.getRuntimeStore(); long ATTACHMENT_ID = 0x227cdf69ca6b6ce8L; // ATTACHMENT_ID // get the message and folder it was added to Message msg = e.getMessage(); Folder messageFolder = msg.getFolder(); if (msg.getSubject().equalsIgnoreCase("XML TEST")) { Object content = msg.getContent(); if (content instanceof Multipart) { Multipart mp = (Multipart) content; for (int i = 0; i < mp.getCount(); i++) { try { // Build a document based on the XML file. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); // InputStream inputStream = // mp.getBodyPart(i).getInputStream(); Document document = builder.parse((InputStream) mp.getBodyPart(i).getContent()); store.put(ATTACHMENT_ID, document); } catch (Exception ex) { Dialog.alert("Error: " + ex.getMessage()); } } // delete the message messageFolder.deleteMessage(msg); NotificationsManager.triggerImmediateEvent(ShowXML.NOTIFICATION_ID, 0, this, null); } } } }



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

Re: XML Attachment

There are 2 approaches you can take to work with email attachments.  You can implement a content handler using JSR 172 for the content type of the attachment.  This provides the user with a Download Attachment menu option when they open the message.  Download Attachment downloads the attachment and then invokes your application to process it.


Alternatively you can use an AttachmentHandler as described here:


How to - Create a custom Attachment Handler on the BlackBerry device
Article Number: DB-00143


Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.