DocumentBuilder.parse(InputSource is)

Discussion in 'Java' started by David, Jun 5, 2004.

  1. David

    David Guest

    Hello guys,

    I have an InputSource from a socket where XML data is poured. Id like to
    interprete each XML data message separatly. But the
    DocumentBuilder.parse(InputSource is) won't parse until the socket is
    closed (hence when the InputSource is closed)

    Anyone can give me some direction to solve this problem please?

    --
    David.
     
    David, Jun 5, 2004
    #1
    1. Advertising

  2. David

    Filip Larsen Guest

    David wrote

    > I have an InputSource from a socket where XML data is poured. Id like

    to
    > interprete each XML data message separatly. But the
    > DocumentBuilder.parse(InputSource is) won't parse until the socket is
    > closed (hence when the InputSource is closed)


    If you have full control over both client and server the easiest is
    perhaps to use your own protocol to separate each XML message from each
    other, for instance by sending how many characters (or bytes) the next
    message is. You then just read in that many characters fully into a
    buffer and parse that as a complete XML document. ByteArrayOutputStream
    and ByteArrayInputStream are your friends here. Alternatively you can
    scan for the xml header string and end tag (easy if it is uniqely named,
    complicated if it is not) and then extract the section of data including
    both into a buffer for parsing.

    If your messages are sufficiently simple in structure and generated on
    the fly, it may be even easier and less resource using to let the
    complete stream be one valid XML stream where each message is a child of
    the top level tag. The complete stream can then be parsed using SAX, as
    it can handle never-ending-streams just fine. This is good for long
    simple messages, but not so good if your messages have a complicated
    structure, or if the rest of the application need the messages as a DOM
    tree anyway.


    Regards,
    --
    Filip Larsen
     
    Filip Larsen, Jun 6, 2004
    #2
    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.

Share This Page