简体   繁体   English

在服务器和客户端套接字之间传输XML数据的最佳方法

[英]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. 现在,我想在客户端和服务器之间发送XML,而不是字符串。 I have used DOM to parse XML data on server and client side. 我已经使用DOM来解析服务器和客户端上的XML数据。

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. 是否有一种有效的方法在客户端和服务器之间传输XML,而不是将XML转换为字符串并删除换行符(因为readLine一次读取一行,并且XML的开始标记和结束标记不会以XML格式出现在一行中)数据并将其通过Printwriter发送给客户端。

Thanks 谢谢

PS: This is not a homework question. PS:这不是作业问题。 I am learning java sockets for interviews. 我正在学习Java套接字进行面试。

Look into StAX api. 查看StAX API。 It is designed for transferring XML data over the internet. 它旨在用于通过Internet传输XML数据。

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. 流是指一种编程模型,在该模型中,XML信息集通常在应用程序运行时以实时方式并通常是从动态源中动态地传输和解析,而动态源的内容事先无法精确知道。
[...] [...]
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. 当您的应用程序具有严格的内存限制时(例如运行Java平台,微型版(Java ME平台)的手机),或者当您的应用程序需要同时处理多个请求(例如应用程序服务器)时,用于XML处理的流模型特别有用。

More in the docs: http://docs.oracle.com/javase/tutorial/jaxp/stax/why.html docs中的更多信息: http : //docs.oracle.com/javase/tutorial/jaxp/stax/why.html

You can convert your XML to compressed binary data by using Exificient 您可以使用Exificient将XML转换为压缩的二进制数据

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. EXI格式是可扩展标记语言(XML)信息集的非常紧凑的表示形式,旨在同时优化性能和计算资源的利用率。

Actually, XML data is nothing more than string (but you can treat it as binary too). 实际上,XML数据只不过是字符串(但是您也可以将其视为二进制)。 Your problem is sending many lines of xml string. 您的问题是发送多行xml字符串。 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 . 如果您不想使用第三方库,只需手动操作即可,这里是一个不错的起点: https : //stackoverflow.com/a/2441736/719212
This example uses the byte stream to send and receive xml data with parsing xml. 本示例使用byte stream通过解析xml发送和接收xml数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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