简体   繁体   中英

Best way to transfer XML data between server and client sockets

Server side:

PrintWriter outputStream = new PrintWriter(client.getOutputStream(),true);
BufferedReader inputStream = new BufferedReader(newInputStreamReader(client.getInputStream()));
outputStream.println("hello client");
System.out.println("server got: " + inputStream.readLine());
outputStream.println("to the client");

Client Side:

try{
    serverSocket = new Socket("machineName", 4444);
    out = new PrintWriter(serverSocket.getOutputStream(), true);
    in = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
}
catch(Exception e){
    e.printStackTrace();
}

if ((fromServer = in.readLine()) != null) {
    System.out.println("Server: " + fromServer);
    fromUser = "I am the user";
    out.println(fromUser);
    System.out.println("Server: " + in.readLine());
}

I am using this format for communicating between server and client sockets. Now I would like to send XML between client and server instead of strings. I have used DOM to parse XML data on server and client side.

Is there an efficient way to transfer XML between client and server instead of converting the XML to string and removing newline (because readLine reads one line at a time and XML start tag & end tag wont come in a single line in XML format) from XML data and sending it to client through Printwriter.

Thanks

PS: This is not a homework question. I am learning java sockets for interviews.

Look into StAX api. It is designed for transferring XML data over the internet.

Streaming refers to a programming model in which XML infosets are transmitted and parsed serially at application runtime, often in real time, and often from dynamic sources whose contents are not precisely known beforehand.
[...]
Streaming models for XML processing are particularly useful when your application has strict memory limitations, as with a cellphone running the Java Platform, Micro Edition (Java ME platform), or when your application needs to process several requests simultaneously, as with an application server.

More in the docs: http://docs.oracle.com/javase/tutorial/jaxp/stax/why.html

You can convert your XML to compressed binary data by using Exificient

The EXI format is a very compact representation for the Extensible Markup Language (XML) Information Set that is intended to simultaneously optimize performance and the utilization of computational resources.

Actually, XML data is nothing more than string (but you can treat it as binary too). Your problem is sending many lines of xml string. If you don't want to use third party library, just do it by hand, here is a good place to start: https://stackoverflow.com/a/2441736/719212 .
This example uses the byte stream to send and receive xml data with parsing xml.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM