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
damoncher
Posts: 6
Registered: ‎03-18-2009
My Device: Not Specified
Accepted Solution

problem with getting some UTF-8 characters from xml

Hi,

 

Please help me to solve the following problem. I need to parse XML string which contains currency sign (for example pound sign, see the example code). In the result currSign variable contains "?". What is wrong?

 

 

  public static void test() {

    try {

      String data = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +

      "<amount unit=\"" + '\u00a3' + "\">0.00</amount>";

      ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes());

      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

      DocumentBuilder builder = factory.newDocumentBuilder();

      Document doc = builder.parse(is);

      Element root = doc.getDocumentElement();

      String currSign = root.getAttributes().getNamedItem( "unit" ).getNodeValue();

    }

    catch (Exception e) {

 

    }

  }

 

 

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

Re: problem with getting some UTF-8 characters from xml

That sounds like it might be a font problem. Check that the font can actually display the pound sign. (The "?" is often used to indicate an unrenderable character.)



Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
New Developer
damoncher
Posts: 6
Registered: ‎03-18-2009
My Device: Not Specified

Re: problem with getting some UTF-8 characters from xml

The problem is not in the font. When I try to debug and watch the source string contains pound sign but the result string doesn't.
Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: problem with getting some UTF-8 characters from xml

[ Edited ]
Ah. I think I see the problem. You're converting the String data to a byte array using the no-argument version of getBytes(). That uses the default encoding (which is ISO-8859-1). You need to either call getBytes( "UTF-8" ) or specify ISO-8859-1 in the xml declaration.
Message Edited by Ted_Hopp on 04-13-2009 12:42 PM



Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
New Developer
damoncher
Posts: 6
Registered: ‎03-18-2009
My Device: Not Specified

Re: problem with getting some UTF-8 characters from xml

Thank You very much! The solution You provided solved the problem!
Please use plain text.