java - 通过 TCP 发送转换为字节数组的 XML,然后将响应转换回可读的 XML 的最佳方法是什么?

标签 java arrays xml sockets byte

我被要求编写一个解决方案,将 XML 转换为字节数组,以便通过直接 TCP 以及服务所需的其他参数作为请求发送到 .Net 服务。该服务以字节数组格式返回 XML。在以下解决方案中,我尝试实现这些原则:

  1. 打开套接字。
  2. 打开套接字的输入流和输出流。
  3. 根据服务器协议(protocol)读取和写入流。
  4. 关闭流。
  5. 关闭套接字。

请注意,我从主程序调用下面的代码,该主程序将 XML 文件转换为 requestData 参数的字节数组,并关闭通过创建该类的实例打开的套接字。在输出中,我得到了一个类似于 [B@60076007.

的值列表。

我的问题是:上面的输出示例是否为需要转换回 XML 的字节数组格式?如果是,是否有关于如何执行此操作的建议?有没有更好的方法来组织这段代码?就像发送和接收应该在不同的函数中吗?感谢任何愿意回复的人的时间。

public class  TCPTest implements Closeable {


private static final Logger logger = Logger.getLogger( TCPTest.class);

private Socket socket;

public  TCPTest(String host, int port) throws IOException {
    this(new Socket(host, port));
}

public  TCPTest(Socket s) {
    this.socket = s;
}

public void send TCPRequest(int testId, String method, byte[] requestData, boolean sendLength) throws IOException {

    socket.setSoTimeout(40000);

    DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
    DataInputStream dis = new DataInputStream(socket.getInputStream());

    try 
    {
        dos.write(testId);
        dos.writeBytes(method);

        dos.write(requestData); // write the message
        System.out.println("length of request: " + requestData.length);

        if (sendLength) {
            String lengthDesc = "LEN*";
            byte[] lengthDescBytes = lengthDesc.getBytes("US-ASCII");

            dos.write(lengthDescBytes);

            dos.write(requestData.length);
        }

    } catch (UnknownHostException e) {
        System.err.println("Unable to find host: 3125");
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for the connection to: 3125");
    }

    try 
    {
        int lengthResponse = dis.read(); // get length of incoming message
        System.out.println("length of response: " + lengthResponse);

        while (lengthResponse > 0) {
            byte[] message = new byte[lengthResponse];
            System.out.println(message);

            dos.write(message, 0, lengthResponse);

        }

    } catch (EOFException e) {
        System.err.println("Input stream ended before all bytes read");
    } catch (IOException e) {
        System.err.println("I/O failed on the connection to: 3125");
    }

    dos.flush();
    dos.close();
}

@Override
public void close() throws IOException {
    this.socket.close();
}

}

最佳答案

因此,基本上传入的数据是一个 byte[],因为这是字符串形式表示该对象 [B@60076007,至少是 [B 部分。

现在,由于 XML 只是简单的 String,您可以使用 String(byte[] bytes, Charset charset) 将字节数组转换为 String,其中您必须提供用于转换的编码。

如果这还不够的话,您可以从字符串形式创建您需要的任何其他 XML 表示形式(w3c、dom4j 等)。

除了查看您的代码后,我发现您根本没有读取服务器输出。

int lengthResponse = dis.read(); // get length of incoming message

不,该行仅从流中读取第一个字节(不是整数)。整数的长度为 4 个字节。

 byte[] message = new byte[lengthResponse];
    System.out.println(message);

您正在创建新的字节数组(缓冲区),这很好,但您永远不会写入它(通过从输入流读取)。之后,您将打印 message.toString() 方法的结果。

您需要先阅读整条消息,稍后进行转换并存储。

伪代码:

    InputStream in;
    byte[] buff=new byte[your_length]

    in.read(buff) //this line will read your_length bytes from the input stream
    String message=new String(buff,ProperCharset); //convert to string
    sysout(message); // print it
    //do whatewer you like eg, store to your DataOutputStream with 
    // out.write(message.getBytes(ProperCharset));
    // or directly out.write(buff);

关于java - 通过 TCP 发送转换为字节数组的 XML,然后将响应转换回可读的 XML 的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30738966/

相关文章:

java - 用于调试的可嵌入运行时 Java 对象检查器

java - 膨胀 map fragment 时出错

c - 使用C语言中的递归函数搜索数组中的元素

javascript - 使我的函数可重用

xml - Oracle - 验证日期格式 (yyyy-mm-ddThh24 :mi:ssZ) in XML against XSD

java - JAXB:为共享一个公共(public) XSD 的两个 XSD 生成类

java - 如何在 IntelliJ IDEA 中禁用 JavaDoc 的格式化

来自文件的 Java 迷宫

xml - 在Marklogic中,如何有效地深度比较两个xml文档?

c# - 可空 xml 列的 Linq to sql 错误 "Data at the root level is invalid "