我用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>
 &lt;Units>
&lt;Unit>
&lt;UnitName>xxx
&lt;/UnitName>
......
&lt;/Unit>
&lt;/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,都很正常,被搞晕死了,也找不到其他类似资料,分不够另开帖。

解决方案 »

  1.   


    保存返回的xml文件内容大致如下:
    <ns:GetXmlUnitsResponse xmlns:ns="http://****/">
    <ns:return>
    &lt;Units>
    &lt;Unit>
    &lt;UnitName>xxx
    &lt;/UnitName>
    ......
    &lt;/Unit>
    &lt;/Units>
    </ns:return>
    </ns:GetXmlUnitsResponse> 
    返回的数据是经过转义的。应该没什么问题,如果参数为xml字符串的话,否则就有问题
      

  2.   

    可是这样我无法用dom解析,是吧?好像只认到最外层两个正常的标签。问题是要怎么处理这样的返回数据
      

  3.   

    你能确认服务器方面返回的没有问题吗?我建议在服务器端打印返回的结果。我怀疑是你在服务器端组织数据的时候,将Units以后的内容,当作了文本而不是XML标签设置到了ns:return标签下。
      

  4.   

    服务端返回的应该就是我前面列的,我有把返回流写到文本文件里检查,我的想法和你一样,但是接口厂商的对应技术人员我个人认为是个sb,死不承认,让他帮忙分析啃啃兹兹半天放不出个屁来,反反复复就是一句反正没问题,自己再看看,我只能接受他这个结果,看看怎么解析这个所谓xml文档