Unicode and SAXParser

Discussion in 'XML' started by Bobo, Dec 3, 2003.

  1. Bobo

    Bobo Guest

    I am getting the following error while trying to process an XML string with
    unicode in its content.

    [Fatal Error] :12:0: An invalid XML character (Unicode: 0x0) was found in
    the eement content of the document.
    SAXException: org.xml.sax.SAXParseException: An invalid XML character
    (Unicode:0x0) was found in the element content of the document.

    The piece of code that generates this error is the following:

    DOMParser parser = new DOMParser();
    ByteArrayInputStream bais = new
    ByteArrayInputStream(xmlString.getBytes());
    InputSource input = new InputSource(bais) ;
    XMLDocWriter docwriter = new XMLDocWriter();
    parser.parse(input); // <== right here is where I get this error at
    run time

    Could you please give me a piece of advise what to do? I am pretty new to
    XML. I am using apache xerces java API.

    Any help or hint would be appreciated!

    Yung
    Bobo, Dec 3, 2003
    #1
    1. Advertising

  2. In article <bqli67$>,
    Bobo <> wrote:

    >SAXException: org.xml.sax.SAXParseException: An invalid XML character
    >(Unicode:0x0) was found in the element content of the document.


    Well somehow you've got a null in the input stream, which is not
    allowed. Assuming that xmlString didn't contain a null:

    > ByteArrayInputStream bais = new ByteArrayInputStream(xmlString.getBytes());


    the obvious way for this could happen would be for getBytes() to be
    encoding the string in a different encoding from that used to parse
    the resulting byte stream. That's quite likely - the parser is
    presumably using UTF-8 by default and getBytes uses the platform's
    default encoding which is probably not UTF-8, but I can't see how
    you're likely to get a null with the common default encodings. Is
    there an XML declaration at the start of the string, and if so is it
    specifying the encoding?

    -- Richard
    --
    Spam filter: to mail me from a .com/.net site, put my surname in the headers.

    FreeBSD rules!
    Richard Tobin, Dec 3, 2003
    #2
    1. Advertising

  3. Bobo

    Bobo Guest

    Thanks for the reply. No. This happens be an error message from a telnet
    session which has a lot of escape characters. Right now I am thinking about
    ignoring them and escape them. If there's a way to pass all those
    characters, it would be great to know. Do you have any thoughts on this?

    Thanks,
    Yung

    "Richard Tobin" <> wrote in message
    news:bqls7s$1i1c$...
    > In article <bqli67$>,
    > Bobo <> wrote:
    >
    > >SAXException: org.xml.sax.SAXParseException: An invalid XML character
    > >(Unicode:0x0) was found in the element content of the document.

    >
    > Well somehow you've got a null in the input stream, which is not
    > allowed. Assuming that xmlString didn't contain a null:
    >
    > > ByteArrayInputStream bais = new

    ByteArrayInputStream(xmlString.getBytes());
    >
    > the obvious way for this could happen would be for getBytes() to be
    > encoding the string in a different encoding from that used to parse
    > the resulting byte stream. That's quite likely - the parser is
    > presumably using UTF-8 by default and getBytes uses the platform's
    > default encoding which is probably not UTF-8, but I can't see how
    > you're likely to get a null with the common default encodings. Is
    > there an XML declaration at the start of the string, and if so is it
    > specifying the encoding?
    >
    > -- Richard
    > --
    > Spam filter: to mail me from a .com/.net site, put my surname in the

    headers.
    >
    > FreeBSD rules!
    Bobo, Dec 4, 2003
    #3
  4. In article <bqno7j$>,
    Bobo <> wrote:

    >Thanks for the reply. No. This happens be an error message from a telnet
    >session which has a lot of escape characters. Right now I am thinking about
    >ignoring them and escape them. If there's a way to pass all those
    >characters, it would be great to know. Do you have any thoughts on this?


    You certainly need to do something about them!

    Most control characters are illegal in XML 1.0. 1.1 is more liberal,
    but you probably don't want to rely on 1.1 just for this, and it won't
    help with nuls. If you want to preserve the characters you could:

    (a) translate them into elements, e.g. <control code="0"/>
    (b) translate them into other characters, perhaps ones in the
    unicode private use area (you would probably translate them
    into character references, e.g. ).

    -- Richard

    --
    Spam filter: to mail me from a .com/.net site, put my surname in the headers.

    FreeBSD rules!
    Richard Tobin, Dec 6, 2003
    #4
    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. Per Magnus L?vold
    Replies:
    0
    Views:
    2,014
    Per Magnus L?vold
    Nov 16, 2004
  2. SnooPac
    Replies:
    8
    Views:
    13,209
    Stanimir Stamenkov
    Jan 6, 2005
  3. User
    Replies:
    0
    Views:
    878
  4. -saarland.de
    Replies:
    2
    Views:
    911
    Magnus Henriksson
    Jan 25, 2007
  5. Peter Higgins

    libxml's SaxParser and UTF-8 problem

    Peter Higgins, Mar 2, 2007, in forum: Ruby
    Replies:
    2
    Views:
    113
    Jenda Krynicky
    Mar 7, 2007
Loading...

Share This Page