Skip to content

Can't use PullParser to parse several valid messages in a row #214

Description

@DmitryPogrebnoy

I have the following use case:

require 'socket'
require 'rexml/parsers/pullparser'

# Server setup for demonstration purposes
server = TCPServer.new 2000
Thread.new do
  client = server.accept
  client.puts "<message>First valid and complete message</message>"
  sleep 1
  client.puts "<message>Second valid and complete message</message>"
  sleep 1
  client.puts "<message>Third valid and complete message</message>"
  client.close
end

# Client setup
socket = TCPSocket.new 'localhost', 2000

# REXML PullParser using socket
parser = REXML::Parsers::PullParser.new(socket)

begin
  while parser.has_next?
    event = parser.pull
    if event.start_element? and event[0] == 'message'
      text = parser.pull
      if text.text?
        puts "Received message: #{text[0]}"
      end
    end
  end
ensure
  socket.close
end

# Closing the server in this demonstration
server.close

Here I have a socket, and I pull messages from the server and parse it. Every message is valid and complete xml.
Before the release https://github.com/ruby/rexml/releases/tag/v3.3.2 this use case worked like a charm. After that it doesn't work with exception

'REXML::Parsers::BaseParser#pull_event': Malformed XML: Extra tag at the end of the document (got '<message') (REXML::ParseException)

This is a side effect after this changes - #161
It seems there is no proper solution for this use case now, and I can't use the actual version of this gem.

Possible solutions could be as follows:

  1. Addition parameter or flag to ignore the error above.
  2. Add a special call to refresh the parser after each pull event.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions