Sax Parser problem : xml encoding of string??

Discussion in 'Java' started by brightoceanlight@hotmail.com, Sep 15, 2005.

  1. Guest

    I'm trying to parse an XML string with Java's SaxParser. The program
    fails at the end of an element or at the beginning of a new element.

    Is my XML string okay?

    <?xml version="1.0" encoding="utf-8"
    ?><DTYPE>OVL</DTYPE><DTYPE>IMG</DTYPE>. . .

    If fails after </DTYPE> and before the next <DTYPE>

    The Sax Parser works fine on files.

    Here is the code I use to send the string to the parser :

    // Parse the input
    SAXParser saxParser = factory.newSAXParser();
    InputStream is = new ByteArrayInputStream(stringToParse.getBytes());
    saxParser.parse( is, handler );

    Here is the error message I get :

    org.xml.sax.SAXParseException: Unzulässiges Zeichen am Dokumentende,
    <
    at org.apache.crimson.parser.Parser2.fatal(Parser2.java:3376)
    at org.apache.crimson.parser.Parser2.fatal(Parser2.java:3370)
    at org.apache.crimson.parser.Parser2.parseInternal(Parser2.java:673)
    at org.apache.crimson.parser.Parser2.parse(Parser2.java:337)
    at
    org.apache.crimson.parser.XMLReaderImpl.parse(XMLReaderImpl.java:448)
    at javax.xml.parsers.SAXParser.parse(SAXParser.java:345)
    at javax.xml.parsers.SAXParser.parse(SAXParser.java:143)
    at EchoSaxParser.parse(EchoSaxParser.java:51)
    at StringParser.parse(StringParser.java:23)
    at Servant.stringparse(Servant.java:30)
    at TEST._TestImplBase._invoke(_TestImplBase.java:43)
    at
    com.sun.corba.se.internal.corba.ServerDelegate.dispatch(ServerDelegate.java:353)
    at com.sun.corba.se.internal.iiop.ORB.process(ORB.java:280)
    at
    com.sun.corba.se.internal.iiop.RequestProcessor.process(RequestProcessor.java:81)
    at
    com.sun.corba.se.internal.orbutil.ThreadPool$PooledThread.run(ThreadPool.java:106)

    Any help would be greatly appreciated!

    Thank you!

    Gil
    , Sep 15, 2005
    #1
    1. Advertising

  2. jan V Guest

    >Here is the error message I get :
    > org.xml.sax.SAXParseException: Unzulässiges Zeichen am Dokumentende,

    <

    And what does this mean in English? Seems like a key piece of info..
    jan V, Sep 15, 2005
    #2
    1. Advertising

  3. Guest

    Re: Sax Parser problem : xml encoding of string??

    jan V wrote:
    > >Here is the error message I get :
    > > org.xml.sax.SAXParseException: Unzulässiges Zeichen am Dokumentende,

    > <
    >
    > And what does this mean in English? Seems like a key piece of info..


    Inadmissible sign at the document end
    , Sep 15, 2005
    #3
  4. wrote:
    Hi Gil!

    > I'm trying to parse an XML string with Java's SaxParser. The program
    > fails at the end of an element or at the beginning of a new element.
    >
    > Is my XML string okay?
    >
    > <?xml version="1.0" encoding="utf-8"
    > ?><DTYPE>OVL</DTYPE><DTYPE>IMG</DTYPE>. . .
    >
    > If fails after </DTYPE> and before the next <DTYPE>

    If I recall it correctly, XML must have exactly *one* top-level element.
    Your top-level element is DTYPE and you have more than one.
    The solution could be to invent a top-level element surrounding all your
    DTYPE elements. For example:
    <?xml version="1.0" encoding="utf-8"?>
    <BLA>
    <DTYPE>OVL</DTYPE><DTYPE>IMG</DTYPE>
    </BLA>

    >
    > The Sax Parser works fine on files.
    >
    > Here is the code I use to send the string to the parser :
    >
    > // Parse the input
    > SAXParser saxParser = factory.newSAXParser();
    > InputStream is = new ByteArrayInputStream(stringToParse.getBytes());

    One further aside (not your posted problem):
    You shouldn't call getBytes() without argument and thus rely on the
    system's default-encoding whatever that might be. Doing so would provoke
    very obscure problems, if the XML's encoding is different from the
    system-default-encoding *and* the XML constains non-ASCII characters.
    Instead call getBytes("utf-8") in accordance with the encoding given in
    the XML-header.

    > saxParser.parse( is, handler );
    >
    > Here is the error message I get :
    >
    > org.xml.sax.SAXParseException: Unzulässiges Zeichen am Dokumentende,
    > <

     is '<', so your analysis (in line 2 of your post) is correct.
    > at org.apache.crimson.parser.Parser2.fatal(Parser2.java:3376)
    >
    > Any help would be greatly appreciated!
    >
    > Thank you!
    >
    > Gil
    >

    --
    "Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
    Thomas Fritsch, Sep 15, 2005
    #4
  5. Guest

    Re: Sax Parser problem : xml encoding of string??

    >If I recall it correctly, XML must have >exactly *one* top-level element.
    >Your top-level element is DTYPE and you >have more than one.
    >The solution could be to invent a top-level >element surrounding all your
    >DTYPE elements. For example:
    > <?xml version="1.0" encoding="utf-8"?>
    > <BLA>
    > ><DTYPE>OVL</DTYPE><DTYPE>IMG</DTYPE>

    > </BLA>



    Yes! It worked! Thank you so much for your help!

    >Instead call getBytes("utf-8") in >accordance with the encoding given in
    >the XML-header.


    Thanks for the tip!

    Gil
    , Sep 15, 2005
    #5
  6. In article <>,
    wrote:

    > I'm trying to parse an XML string with Java's SaxParser. The program
    > fails at the end of an element or at the beginning of a new element.
    >
    > Is my XML string okay?
    >
    > <?xml version="1.0" encoding="utf-8"
    > ?><DTYPE>OVL</DTYPE><DTYPE>IMG</DTYPE>. . .
    >
    > If fails after </DTYPE> and before the next <DTYPE>
    >


    [ snip ]

    >
    > Thank you!
    >
    > Gil


    The example above demonstrates why it *should* fail. Every XML document
    is required to have one and only one root element. So the closing DTYPE
    tag should signify the end of the document and not be followed by
    anything else.
    --
    Steve W. Jackson
    Montgomery, Alabama
    Steve W. Jackson, Sep 15, 2005
    #6
    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. Martin Schlatter

    Encoding problem with SAX parser

    Martin Schlatter, Dec 10, 2003, in forum: Java
    Replies:
    2
    Views:
    828
    Martin Schlatter
    Dec 14, 2003
  2. Sanjeev
    Replies:
    4
    Views:
    1,414
    Stanimir Stamenkov
    May 4, 2008
  3. Michel Demazure

    Nokogiri SAX parser encoding problem

    Michel Demazure, Aug 24, 2010, in forum: Ruby
    Replies:
    6
    Views:
    327
    Michel Demazure
    Aug 25, 2010
  4. arne
    Replies:
    0
    Views:
    339
  5. Erik Wasser
    Replies:
    5
    Views:
    417
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page