SAXParser and preserving special characters

Discussion in 'XML' started by User, Oct 31, 2003.

  1. User

    User Guest

    I am trying to use JDOM's SAXBuilder to parse an XML document that contains
    encoded latin-1 characters. After I parse the document, the special
    character Strings seem to be replaced with their unicode characters (e.g.,
    the String "®" is replaced with a character that has a decimal value of
    174); I was expecting that the SAXBuilder would preserve the String
    "®". Is it possible to instruct the SAX parser to preserve the special
    character encodings?

    The following is sample code that illustrates the issue that I am observing:

    import java.io.ByteArrayInputStream;

    import org.jdom.Document;
    import org.jdom.input.SAXBuilder;
    import org.jdom.output.XMLOutputter;

    public class TestProductBuilder {

    public static void main(String[] args) {
    ByteArrayInputStream bis = null;
    try {
    String product = "<?xml version=\"1.0\"?>" +
    "<product>" +
    " <name>My Product ®</name>" +
    "</product>";

    bis = new ByteArrayInputStream(product.getBytes());
    SAXBuilder builder = new SAXBuilder(false);
    Document productDoc = builder.build(bis);

    XMLOutputter outputter = new XMLOutputter("\t", true);
    String productFromSAXBuilder = outputter.outputString(productDoc));
    } catch (Exception e) {
    System.err.println(e.getMessage());
    } finally {
    if (bis != null) { try { bis.close(); } catch (Exception e) {}}
    }
    }
    }

    The following is the value for "productFromSAXBuilder":
    <?xml version="1.0" encoding="UTF-8"?>
    <product>
    <name>My Product ®</name>
    </product>
    User, Oct 31, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Mueller
    Replies:
    3
    Views:
    32,985
    Stefan Mueller
    Jul 23, 2006
  2. TTroy
    Replies:
    16
    Views:
    787
    Peter Nilsson
    Jan 31, 2005
  3. Replies:
    2
    Views:
    1,084
    Ingo Menger
    May 31, 2007
  4. rvino
    Replies:
    0
    Views:
    4,649
    rvino
    Aug 14, 2007
  5. Replies:
    4
    Views:
    518
Loading...

Share This Page