2

I wrote a Ruby script to connect to an XML stream over a TCP socket. I want to parse this XML stream using LibXML). My problem is that I can't figure out how to pass this stream to LibXML. From the LibXML documentation XML::Document.io(io) this seems to be setup for HTTP, not a TCP socket. Any help on how to wrap my TCP socket so it can be used by LibXML? Or is there a better way to do parse this XML stream from my TCP socket? I'm learning Ruby now, so the code below isn't pretty in the way I authenticated either. Based on other questions on stackoverflow, it seems I will have to ensure that I have complete XML (proper closing tags) before sending that to the LibXML parser. Thanks in advance.

Here's what I have so far:

   require 'socket'
   require 'timeout'

   username = "username\r\n"
   password = "password\r\n"
   port = 4500
   server = 'xmlfeed.website.com'

   tcp_client = TCPSocket.new(server, port)


   all_data = []
   value = 0
   while true
     partial_data = tcp_client.recv(1012)

     if partial_data.length == 0
       break
     end
     all_data << partial_data
     puts(all_data)
     if value == 0
           tcp_client.puts(username)
           value = 1
     end
     if value == 1
           tcp_client.puts(password)
           value = 2
     end
     if all_data.to_s.chomp.casecmp( "Starting Feed")
           value = 3
           puts "Begin the feed"
     end
     if value == 3
           # Parse the data using LibXML
           # this is the part I'm not sure how to approach
           puts(all_data)
    end
   end
   tcp_client.close

   puts all_data.join()
FrankB
  • 21
  • 2

0 Answers0