读书人

懂XML报文是咋回事的进

发布时间: 2013-08-06 16:47:25 作者: rapoo

懂XML报文是怎么回事的进
现在做一个系统,给另一个公司提供服务,他们传递一个XML字符串报文我们,我们做处理

不是很清楚,报文为什么传递的直接是XML的内容字符串。难道这就是所谓的报文?

还有我们返回的也是XML报文。JSON现在这么流行 为什么不用JSON传递呢??

由于不是直接参与开发的,我看不了源码,好像我们这边用到了dom4j


麻烦懂的,给我讲解下
[解决办法]
我觉的是soap技术
[解决办法]
xml进行数据传输,是J2EE的一种标准。也就是用上层http传协议。通过request,response来实现。我给个例子
package nc.plugin.test;

/**
* 此处插入类型说明。
* 创建日期:(2004-12-5 15:49:22)
* @author:Administrator
*/
import java.lang.*;
import java.util.*;
import java.sql.*;
import java.io.*;
import java.net.*;

public class PostFile {
HttpURLConnection connection = null;
/**
* PostFile 构造子注解。
*/
public PostFile() {
super();
}
/**
* PostFile 构造子注解。
*/
//获得HTTP连接
public HttpURLConnection getConnection(String url) throws Exception{

try {
URL realURL = new URL(url);
URLConnection conn = realURL.openConnection();

conn.setRequestProperty("Content-type", "text/xml");
connection = (HttpURLConnection) conn;
connection.setDoOutput(true);
connection.setRequestMethod("POST");
System.out.println("获得连接"+url);
} catch (MalformedURLException mex) {
mex.printStackTrace();
throw mex;
} catch (ProtocolException pex) {
pex.printStackTrace();
throw pex;
} catch (IOException iex) {
iex.printStackTrace();
throw iex;
}
return connection;
}



//调用过程,方法入口.
public static void main(String[] args){
PostFile pf = new PostFile();

String str=null;
String url="http://192.168.16.46:9088/service/XChangeServlet?account=004&receiver=1@1-0002";


String[] m_allSelectedFiles = {"D:/voucher/101200012009070063.xml"};

for (int i = 0; i < m_allSelectedFiles.length; i++) {
//获得连接
try{
pf.getConnection(url);
pf.sendFile(new File(m_allSelectedFiles[i]),"gb2312");
str += "\n" + pf.receiveResponse("gb2312");

}catch(Exception e){
System.out.println("getConnection Exception");
System.out.println("aaaaaaaaaaaaaa");
}
//发送文件
//接收回执

System.out.println(str);


} // end for
}

//发送文件
public BufferedReader readFile(String file) throws Exception{
BufferedReader in1=null;
try {
//从文件获得输入
in1=new BufferedReader(new InputStreamReader(new FileInputStream(file),"gb2312"));
//循环获得文件中的内容,并out发出

} catch (Exception ex) {
ex.printStackTrace();
throw ex;
}
return in1;
}


//获取数据响应
public String receiveResponse(String output_coding) {
BufferedReader in;
StringBuffer response = new StringBuffer();
//File file=null;
try {
//此处获得响应回执
in = new BufferedReader(new InputStreamReader(connection.getInputStream(),output_coding));
String line = "";

while ((line = in.readLine()) != null) {
//line = new String(line.getBytes("utf-8"),"gb2312");
response.append(line + "\n");
//System.out.println(line);
}
in.close();
} catch (Exception exception) {
exception.printStackTrace();
}
return response.toString();
}


//发送文件
public void sendFile(File file,java.lang.String input_coding) throws Exception{
try {
PrintWriter out = new PrintWriter(connection.getOutputStream());//获得一个输出流
//从文件获得输入
BufferedReader in1=new BufferedReader(new InputStreamReader(new FileInputStream(file),input_coding));
//循环获得文件中的内容,并out发出

String s;
while ((s = in1.readLine()) != null) {
out.println(s);

out.flush();
}
out.close();
System.out.println("已经成功发送XML文件!");


}catch (UnknownHostException uhe){
uhe.printStackTrace();
throw uhe;
} catch (Exception ex) {
ex.printStackTrace();
throw ex;
}
}

}
这是用http调用servlet的方法.
服务器端写一个servlet处理xml文件就行.
[解决办法]
例如:客户端那别的代码已经做好了,只是给你提供了一个接口,只要你传入的数据时对的他就会给你返回一个xml,如果你传入的数据时错误的,那么他同时还会给你返回xml这就是接口的好处,他们的意思就是对外开放一个接口让咱们通过这个接口去开发。

还有一种可能性就是:不同的语言去开发要同过一个标准去共享数据,那就是xml,比如说在他的那别是用c语言编写的程序,要和咱们这里的java进行结合,就是用过xml进行的,

c语言把数据写成xml然后再通过java解析xml拿到我们想要的东西,如果java想和c进行交互那么就得通过java写成xml然后c在解析xml获得想要的数据。这就是他们的交流过程!
明白了吗:有什么可以给我留言

读书人网 >J2EE开发

热点推荐