Java使用TCP协议获取XML及其处理【续】

来源:互联网 发布:下载excel软件 编辑:程序博客网 时间:2024/05/29 09:31

    上一篇文章《Java使用TCP协议获取XML及其处理》(见:http://blog.csdn.net/softwave/article/details/8947771)中,介绍了用Java模拟Socket服务器端,与客户端进行TCP通讯的例子。今天在现场中,客户使用C#制作的服务器端,与我们Java的客户端通讯出现了一个小问题,就是双方发送的XML信息出现乱码现象,客户端向服务器发送的请求信息头出现“/0”等乱字符,服务器端响应给客户端的XML中的中文出现乱码。我们考虑应该是编码不一致造成的问题,统一采用UTF-8编码。所以对上一篇文章中的代码进行了修正。

protected void onBoReadNoTest() throws Exception {Socket soc = null;String data = "";InetAddress addr = InetAddress.getByName("127.0.0.1");int serverPort = 1111;if (addr.isReachable(5000)) {System.out.println("SUCCESS - ping " + addr+ " with no interface specified");try {soc = new Socket(addr, serverPort);System.out.println("Socket Success!");DataInputStream in = new DataInputStream(soc.getInputStream());DataOutputStream out=new DataOutputStream(soc.getOutputStream());String str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><cmd>ReadWeight</cmd>";byte[] b = str.getBytes();out.write(b);byte[] b2 = new byte[1024];int count = in.read(b2, 0, b2.length);data = new String(b2,0,count,"UTF-8");this.DOM(data);} catch (UnknownHostException e) {System.out.println("Socket Error:" + e.getMessage());} catch (EOFException e) {System.out.println("EOF:" + e.getMessage());} catch (IOException e) {System.out.println("IO:" + e.getMessage());} finally {if (soc != null)try {soc.close();} catch (IOException e) {/* close failed */}}} else {System.out.println("FAILURE - ping " + addr+ " with no interface specified");}}
DOM解析如下:

//DOM解析方法public void DOM(String data) {          long lasting = System.currentTimeMillis();          System.out.println("解析XML开始...........");        try {          byte[] b = data.getBytes("UTF-8");        InputStream inp = new ByteArrayInputStream(b);            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();              DocumentBuilder builder = factory.newDocumentBuilder();              Document doc = builder.parse(inp);              NodeList nl = doc.getElementsByTagName("result");             WeightVO wvo = new WeightVO();              for (int i = 0; i < nl.getLength(); i++) {                  wvo.setCode(doc.getElementsByTagName("code").item(i).getFirstChild() == null ?null:doc.getElementsByTagName("code").item(i).getFirstChild().getNodeValue());                  wvo.setWeight(doc.getElementsByTagName("weight").item(i).getFirstChild()==null? null:doc.getElementsByTagName("weight").item(i).getFirstChild().getNodeValue());                  wvo.setErrmsg(doc.getElementsByTagName("errmsg").item(i).getFirstChild() == null ? null : doc.getElementsByTagName("errmsg").item(i).getFirstChild().getNodeValue());                  wvo.setUtil(doc.getElementsByTagName("unit").item(i).getFirstChild()==null?null:doc.getElementsByTagName("unit").item(i).getFirstChild().getNodeValue());              }              getBillCardPanelWrapper().getBillCardPanel().getHeadItem("nweight").setValue(wvo.getWeight());        } catch (Exception e) {              e.printStackTrace();          }         //        System.out.println("DOM RUNTIME 解析时间:"  //                + (System.currentTimeMillis() - lasting) + " MS");      }  
本文由小李专栏原创,转载请注明【转自http://blog.csdn.net/softwave/article/details/8950299】




原创粉丝点击