When reading XML over a socket, generically, you need to use a protocol which denotes when the XML stops. Looking for the last > doesn't work since comments, whitespace and processing instructions can occur outside of the root element. So if you were to send multiple documents on the same socket, the end of one document could invalidate the begiinning of the next document (since the XML declaration must be the first line)
 
So try using, say, HTTP or MIME encoding or writing the length of the XML file first then you know how many bytes to read etc.
 
This has been discussed in this thread...
 
 
Details of how to disable DTD validation, or at least to fetch DTDs from local files or inside jars on the classpath are here...
 
----- Original Message -----
Sent: Thursday, September 05, 2002 2:16 PM
Subject: [dom4j-user] TCP/IP stream to XML document ?

Hi there,
I would like to read XML messages from a TCP/IP stream. One issue
is determining the end of an XML message. My latest solution just
tries to parse the message everytime it receives a ">".
Now the Problem: the parsing always automatically tries to retrieve
the DTD and do a validation. But if the DTD does not exist, every
parsing attempt takes forever.
Question: How can I completely disable DTD retrieval and validation ?
Any sugegstion how I should modify my sample code below ?
thanx Mike
-------------------- cut here --------------------
import java.io.*;
import java.net.*;
import java.util.*;
import org.dom4j.*;
import org.dom4j.io.*;
import org.apache.log4j.*;

/**
* Read XML from TCP/IP connection and create a document.
*/
public class TCP2XML {
static Category log = Category.getInstance(TCP2XML.class.getName());
public static void main(String[] args) {
BasicConfigurator.configure();
int port = 9999;
try {
// open server socket
ServerSocket myServerSocket = new ServerSocket(port);
boolean loop = true;
while (loop) {
log.debug("run() - waiting for a new socket connection");
// wait for an incoming connection
Socket sock = myServerSocket.accept();
log.info("run() - new connection from " + sock.getInetAddress());
InputStream input = sock.getInputStream();
StringBuffer str = new StringBuffer();
int i;
// read every character from input stream
while ((i = input.read()) >= 0) {
str.append((char)i);
if ('>' == (char)i) {
try {
Document doc = DocumentHelper.parseText(str.toString());
// if I get here, I have a complete document
log.info("COMPLETE:" + str);
} catch (DocumentException de) {
log.info("INCOMPLETE:" + str, de);
}
}
}

}
}
catch (IOException ioe) {
log.warn("IOEException", ioe);
}
}
}


Changed your e-mail? Keep your contacts! Use this free e-mail change of address service from Return Path. Register now!

Reply via email to