[Bug 205] New: How to handle continous stream of XML

[email protected] Sat, 28 Aug 2004 15:49:00 -0500 (EST)
Newsgroups gmane.text.xml.xmlpull.devel
Message-ID <[email protected]>
http://www.extreme.indiana.edu/bugzilla/show_bug.cgi?id=205

           Summary: How to handle continous stream of XML
           Product: XmlPull.org
           Version: 1.1.2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P3
         Component: Design
        AssignedTo: [email protected]
        ReportedBy: [email protected]


reported by rspydir

I'm not having much luck getting this to work. Perhaps I misunderstand
the concept. I want to have a server receive XML messages thru a
socket. The socket will remain open indefinitly, and messages will
come in asynchronously.

The server is responsible for receiving and parsing the messages. My
thought was to use XmlPull to parse the steam, identify document
start/end tags, buffer everything in between and stuff into a recieve
queue for processing. Additional tags would be parsed, but for now
just separating documents is my goal.

We'll, it's not working like I would have hoped. I can parse the 1st
message OK, but subsequent messages cause an exception. 

On the client side I'm sending the roses.xml twice with a 1 second
delay between transmissions

<poem xmlns="http://www.megginson.com/ns/exp/poetry">
  <title>Roses are Red</title>
  <line>Roses are red,</line>
  <line>Violets are blue;</line>
  <line>Sugar is sweet,</line>
  <line>And I love you.</line>
</poem>

Although this file is line oriented, there is no guarantee that the
actual client will transmit a line oriented stream.

On the server side, I don't see the END_DOCUMENT event until the 2nd 
message from the client is sent....then the exception

org.xmlpull.v1.XmlPullParserException: start tag not allowed in epilog
but got m (position: END_TAG seen ...


Here's the server code...

import java.io.*;
import java.net.*;
import org.xmlpull.v1.*;
import org.apache.log4j.Logger;


public class XPP3Server {
	
  static Logger logger = Logger.getLogger(XPP3Server.class);
  static final int PORT = 1234;
	
  public static void main(String[] args) {

    Socket socket = null;
    ServerSocket server;
    InputStream inNet = null;

    logger.debug("Server Started");
		
    try {

      server = new ServerSocket(PORT);

      XmlPullParserFactory factory = XmlPullParser
Factory.newInstance();
      factory.setNamespaceAware(true);

      XmlPullParser parser = factory.newPullParser();
        
      while(true) {

	try {

	  // Blocks until a connection occurs:
	  socket = server.accept();

	  logger.debug("Load the parser");

	  inNet = socket.getInputStream();
	  parser.setInput(inNet, null);

	  logger.debug("Handle the datastream");
	  int event;
	  while(true) {
	    event = parser.next();

	    if ( event == XmlPullParser.START_DOCUMENT)
	      logger.debug("Start document");

	    else if ( event == XmlPullParser.END_DOCUMENT) {
	      logger.debug("End document");
	    }
	  
	    else if ( event ==  XmlPullParser.START_TAG)
	      logger.debug("Tag: " + parser.getName());

	  } // end while
	}
      
	catch(Exception e) {
	  logger.error("EXCEPTION: " + e); 
	}
	finally {
	  inNet.close();
	}
      }
    } 
    catch  (Exception e) {
      logger.error("Exception: " + e);
      System.exit(1);
    }
    finally {
			
    }
  } 
}
-------------

here's the client code
import java.io.*;
import java.net.*;
import java.util.List;
import java.util.Iterator;
import org.apache.commons.cli.*;
 
public class XMLClient {

  Socket         socket = null;
  OutputStream   netOut = null;
  InputStream    netIn = null;

  String file;
  String host = "localhost";
  int port = 1234;

  static List argList;
   

  public void connect() {
    try {
      socket = new Socket("localhost", port);
      netOut = socket.getOutputStream();
      netIn  = socket.getInputStream();

    } catch (UnknownHostException e) {
      System.out.println("Unknown host: localhost");
      System.exit(1);

    } catch  (IOException e) {
      System.out.println("Cannot connect to host");
      System.exit(1);
    }
  }

  public void sendXML(String file) {

    final int BUF_SIZE = 1<<10<<3;
    byte[] buffer = new byte[BUF_SIZE];

    try {

      FileInputStream infile = new FileInputStream(file);
      
      int count = -1;
      while ((count = infile.read(buffer)) > -1) {
	netOut.write(buffer,0,count);
      }
      netOut.flush();

      Thread.currentThread().sleep(1000);

    }
    catch (Exception e) {
      System.out.println("sendXML error");
    }
  }

  public void handleCommandLine(String[] args) {
    
    // Define the options
    Option optHost = new Option ("host", "name of remote host");
    optHost.setArgs(1);
    optHost.setArgName("host");

    Option optPort = new Option ("port", "port on the remote host");
    optPort.setArgs(1);
    optPort.setArgName("port");

    Options options = new Options();
    options.addOption(optHost);
    options.addOption(optPort);

    CommandLineParser parser = new BasicParser();
    CommandLine cmd = null;


    // Parse the arguments
    try {
      cmd = parser.parse(options, args);
    } catch (ParseException e) {
      System.out.println("***ERROR: " 
			 + e.getClass() + ": " 
			 + e.getMessage());
      HelpFormatter formatter = new HelpFormatter();
      formatter.printHelp( "parameters:", options );
      System.exit(-1);
    }


    if (cmd.hasOption("file"))
      file = cmd.getOptionValue("file");

    if (cmd.hasOption("port"))
      port = Integer.parseInt(cmd.getOptionValue("port"));
    
    if (cmd.hasOption("host"))
      host = cmd.getOptionValue("host");

    argList = cmd.getArgList();

  }
      
  public static void main(String[] args){
    XMLClient client = new XMLClient();

    client.handleCommandLine(args);

    client.connect();

    for (Iterator arg = argList.iterator(); arg.hasNext(); ) {
      String file = (String)arg.next();
      client.sendXML(file);
    }
  }
}

Client and Server default to port 1234 on localhost.

Any help would be appreciated

thanks


 




 
Yahoo! Groups Links






------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.


------------------------ Yahoo! Groups Sponsor --------------------~--> 
$9.95 domain names from Yahoo!. Register anything.
http://us.click.yahoo.com/J8kdrA/y20IAA/yQLSAA/2U_rlB/TM
--------------------------------------------------------------------~-> 

 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/xmlpull-dev/

<*> To unsubscribe from this group, send an email to:
    [email protected]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/