我用saaj访问axis2服务器提供的web Service,结果返回的soap消息左尖括号被转义成<我用dom解析的时候无法解析,各位老大有没遇到过类似问题?要怎么解决,
REST风格访问:
void get() { try {
URL url = new URL("。");
URLConnection urlConnection = url.openConnection();
//urlConnection.setRequestProperty("accept", "text/xml");
InputStream in = urlConnection.getInputStream();
//saveReturnstream(in); //xml文件保存
excxml(in); //xml解析
/*
BufferedReader in = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream()));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
in.close();
*/
public static void saveReturnstream(InputStream in){
try{
FileOutputStream fos=new FileOutputStream("e:\\Rs7.xml");
int data=in.read();
while(data!=-1){
fos.write(data);
data=in.read();
}
fos.close();
in.close();
}catch (Exception e) {
System.out.println(e);
}
} public static void excxml(InputStream in){
try{
DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
DocumentBuilder dombuilder=domfac.newDocumentBuilder();
Document doc = dombuilder.parse(in);
Element root=doc.getDocumentElement();
Node nd1 = root.getFirstChild();
System.out.println(root.getFirstChild().getTextContent());
System.out.println(root.getTextContent());
/*
NodeList infos=nd1.getElementsByTagName("Unit");
System.out.println(infos.getLength());
for(int i=0;i<infos.getLength();i++){
System.out.println(infos.item(i).getTextContent());
}
*/
in.close();
}catch (Exception e) {
System.out.println(e);
}
}保存返回的xml文件内容大致如下:
<ns:GetXmlUnitsResponse xmlns:ns="http://****/">
<ns:return>
<Units>
<Unit>
<UnitName>xxx
</UnitName>
......
</Unit>
</Units>
</ns:return>
</ns:GetXmlUnitsResponse>
解析的时候,外层标签正常,内部的业务数据dom解析不了,全部被当成了一块文本内容;用soap1.2结果类似 SOAPConnection con =null;
String endpoint = "http://。。";
String target = "GetXmlUs";
String Code="8888";
String outputFile="e:/reced.xml";
try
{
//创建链接
SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
con = scf.createConnection();
//创建soap消息
//MessageFactory mf = MessageFactory.newInstance();
MessageFactory mf = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
SOAPMessage msg = mf.createMessage();
SOAPPart sp = msg.getSOAPPart();
SOAPEnvelope envelope = sp.getEnvelope();
//SOAPHeader hdr = envelope.createSOAPHeader();
SOAPBody body = envelope.getBody();
// AttachmentPart attachment = msg.createAttachmentPart();
Name bodyName=envelope.createName(target,null,"http:///");
SOAPBodyElement gpp=body.addBodyElement(bodyName);
if(Code!=null)
{
gpp.addChildElement("Code").addTextNode(Code);
}
msg.saveChanges();
//Savesoapmessage(msg,"Sended.xml");
//发送soap消息
SOAPMessage reply=con.call(msg,new URL(endpoint));
if(reply!=null){
//Savesoapmessage(reply,outputFile);
SOAPBody sb = (reply.getSOAPPart().getEnvelope().getBody());
NodeList elms = sb.getFirstChild().getFirstChild().getChildNodes();
System.out.println(sb.getFirstChild().getChildNodes().getLength());
System.out.println(elms.getLength());
System.out.println(elms.item(0).getTextContent());
System.out.println(sb.getTagName());
System.out.println(sb.getFirstChild().getFirstChild().getFirstChild().getNodeValue());
/*
for(int i=0;i<elms.getLength();i++){
// System.out.println(elms.item(3).getNodeValue());
// System.out.println(elms.item(3).getNodeName());
// System.out.println(elms.item(3).getNodeType());
// System.out.println(((javax.xml.soap.Node)elms.item(i)).getValue());
System.out.println(elms.item(i).getTextContent());
System.out.println("**********");
}
*/请各位老大帮我看看,到底问题出在哪里,是我客户端问题还是服务端提供的服务有问题,要怎么解决?我测试了一些网络上公开的ws,都很正常,被搞晕死了,也找不到其他类似资料,分不够另开帖。
REST风格访问:
void get() { try {
URL url = new URL("。");
URLConnection urlConnection = url.openConnection();
//urlConnection.setRequestProperty("accept", "text/xml");
InputStream in = urlConnection.getInputStream();
//saveReturnstream(in); //xml文件保存
excxml(in); //xml解析
/*
BufferedReader in = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream()));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
in.close();
*/
public static void saveReturnstream(InputStream in){
try{
FileOutputStream fos=new FileOutputStream("e:\\Rs7.xml");
int data=in.read();
while(data!=-1){
fos.write(data);
data=in.read();
}
fos.close();
in.close();
}catch (Exception e) {
System.out.println(e);
}
} public static void excxml(InputStream in){
try{
DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
DocumentBuilder dombuilder=domfac.newDocumentBuilder();
Document doc = dombuilder.parse(in);
Element root=doc.getDocumentElement();
Node nd1 = root.getFirstChild();
System.out.println(root.getFirstChild().getTextContent());
System.out.println(root.getTextContent());
/*
NodeList infos=nd1.getElementsByTagName("Unit");
System.out.println(infos.getLength());
for(int i=0;i<infos.getLength();i++){
System.out.println(infos.item(i).getTextContent());
}
*/
in.close();
}catch (Exception e) {
System.out.println(e);
}
}保存返回的xml文件内容大致如下:
<ns:GetXmlUnitsResponse xmlns:ns="http://****/">
<ns:return>
<Units>
<Unit>
<UnitName>xxx
</UnitName>
......
</Unit>
</Units>
</ns:return>
</ns:GetXmlUnitsResponse>
解析的时候,外层标签正常,内部的业务数据dom解析不了,全部被当成了一块文本内容;用soap1.2结果类似 SOAPConnection con =null;
String endpoint = "http://。。";
String target = "GetXmlUs";
String Code="8888";
String outputFile="e:/reced.xml";
try
{
//创建链接
SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
con = scf.createConnection();
//创建soap消息
//MessageFactory mf = MessageFactory.newInstance();
MessageFactory mf = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
SOAPMessage msg = mf.createMessage();
SOAPPart sp = msg.getSOAPPart();
SOAPEnvelope envelope = sp.getEnvelope();
//SOAPHeader hdr = envelope.createSOAPHeader();
SOAPBody body = envelope.getBody();
// AttachmentPart attachment = msg.createAttachmentPart();
Name bodyName=envelope.createName(target,null,"http:///");
SOAPBodyElement gpp=body.addBodyElement(bodyName);
if(Code!=null)
{
gpp.addChildElement("Code").addTextNode(Code);
}
msg.saveChanges();
//Savesoapmessage(msg,"Sended.xml");
//发送soap消息
SOAPMessage reply=con.call(msg,new URL(endpoint));
if(reply!=null){
//Savesoapmessage(reply,outputFile);
SOAPBody sb = (reply.getSOAPPart().getEnvelope().getBody());
NodeList elms = sb.getFirstChild().getFirstChild().getChildNodes();
System.out.println(sb.getFirstChild().getChildNodes().getLength());
System.out.println(elms.getLength());
System.out.println(elms.item(0).getTextContent());
System.out.println(sb.getTagName());
System.out.println(sb.getFirstChild().getFirstChild().getFirstChild().getNodeValue());
/*
for(int i=0;i<elms.getLength();i++){
// System.out.println(elms.item(3).getNodeValue());
// System.out.println(elms.item(3).getNodeName());
// System.out.println(elms.item(3).getNodeType());
// System.out.println(((javax.xml.soap.Node)elms.item(i)).getValue());
System.out.println(elms.item(i).getTextContent());
System.out.println("**********");
}
*/请各位老大帮我看看,到底问题出在哪里,是我客户端问题还是服务端提供的服务有问题,要怎么解决?我测试了一些网络上公开的ws,都很正常,被搞晕死了,也找不到其他类似资料,分不够另开帖。
解决方案 »
- 请说明 java什么时候使用 数组 什么时候使用 arraylist?
- Spring AOP 学习探讨
- 求 spring 教材
- 为什么在hibernate中执行修改的时候ID被限制参数?
- 请问我想做一个网页模板,要求这个模板根据传参的不同显示不一样的内容,请问怎么办?(比如参数1网页只显示模板中的表格,参数2则显示表格+选择项)
- 100分 求高人编个 提取搜狐社区某个网页下的所有图片链接 的程序
- JDBC连接MYSQL问题.
- JAVA中调用存储过程 ?={call myproc}
- 问EJB高手一个问题
- 求解决
- 奇怪的struts2问题.............进来有分
- 如何将Excle表里的数据导入到数据库表里???
保存返回的xml文件内容大致如下:
<ns:GetXmlUnitsResponse xmlns:ns="http://****/">
<ns:return>
<Units>
<Unit>
<UnitName>xxx
</UnitName>
......
</Unit>
</Units>
</ns:return>
</ns:GetXmlUnitsResponse>
返回的数据是经过转义的。应该没什么问题,如果参数为xml字符串的话,否则就有问题