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
Contributor
jtr87
Posts: 38
Registered: ‎03-18-2010
My Device: 9000
My Carrier: Telekom.de/Vodafone.de
Accepted Solution

Opening XML, how to write the Path

Hi,

 

I'm new to Blackberry Development and I hope you can help me with my problem. I try to work with an XML file and until now everything did fine as long as I had the xml-file, I'm working on, in the folder of my java application and deyployed it using the Desktop-Software. Now I tried to put the file to another place on the Blackberry and I don't know, how to wirte the path in the source code. I've tried the following, but it seems to be wrong:

 

 

 

private static String _xmlFileName = "file:///store/home/user/documents/test.xml";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	        DocumentBuilder builder = factory.newDocumentBuilder();
	        InputStream inputStream = getClass().getResourceAsStream( _xmlFileName );
	        Document document = builder.parse( inputStream );

 

I hope you can give me a hint, how I have to write the correct path?

 

Thanks a lot!

 

regards, jtr87

 

PS: Sorry for my bad english, but I do not have to use it very often.

 

Please use plain text.
Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Opening XML, how to write the Path

getResourceAsStream() refers to a document that is embedded in the distribution.

 

You need to switch over to FileConnection to get an input stream from a file that is in the file system of the device.

Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Opening XML, how to write the Path

You need to do something like this to get your InputStream:

 

 

/**
* @return an open InputStream for a file, or null if file does not
* exist.
* @throws IOException if there is an error opening the file
*/
InputStream getInputStream(String name) throws IOException { FileConnection fc = null; try { fc = (FileConnection) Connector.open(name, Connector.READ); if (fc.exists()) { return fc.openInputStream(); } } finally { if (fc != null) { try { fc.close(); } catch (Exception ignored) { } } } return null; }

 

Note that you need to close the FileConnection at some point regardless of whether the call to openInputStream worked.

 




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
Contributor
jtr87
Posts: 38
Registered: ‎03-18-2010
My Device: 9000
My Carrier: Telekom.de/Vodafone.de

Re: Opening XML, how to write the Path

Hi,

 

thanks for your solution! Now my Application does very well :smileyhappy:

 

regards

Please use plain text.