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
Regular Contributor
Posts: 86
Registered: ‎03-06-2010
My Device: 9000/9700
My Carrier: AT&T
Accepted Solution

Parsing XML from a String Variable

I've looked at the docs and spent the afternoon looking around the web and can't seem to figure this out.  It is a combination SAX parser and java.io question.  The SAX parser will take either an InputStream or an InputSource, but I can't seem to find out how to turn a String containing XML into either of these to pass to the parser.  Anyone know how to do this?

 

TIA,

Allen

 

TIA,
Allen
Regular Contributor
Posts: 86
Registered: ‎03-06-2010
My Device: 9000/9700
My Carrier: AT&T

Re: Parsing XML from a String Variable

Never mind!  I finally found it:

 

 

String xml;
ByteArrayInputStream ba= new ByteArrayInputStream(xml.getBytes());

 

 

Thanks for looking!!

Allen

 

TIA,
Allen
Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Parsing XML from a String Variable

Unless you know that the XML is encoded as ISO-8859-1 (the default encoding for getBytes), you should use getBytes("UTF-8") (or some other encoding, if you know otherwise). The default encoding for XML is UTF-8.

 

The two encodings agree for the first 128 characters (ASCII 0-127), but after that, watch out! (Try your code with XML text that contains, for instance, a copyright symbol, ©, or an accented vowel, as in "à la carte".)




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Regular Contributor
Posts: 86
Registered: ‎03-06-2010
My Device: 9000/9700
My Carrier: AT&T

Re: Parsing XML from a String Variable

Hey, Ted!  Thanks for the reply.  The application more or less ensures that the encoding will be UTF-8.  I'm serializing a String that represents an XML encoding of app defaults, then, when the app starts up again, reads it to get the default information.  In this case, I'm not reading anything I didn't write, so I have control over both sides.

TIA,
Allen