Java 分析SOAP响应时返回null String response=“…”; DocumentBuilderFactory dbf=null; DocumentBuilder db=null; org.w3c.dom.Document Document=null; 试一试{ dbf=DocumentBuilderFactory.newInstance(); db=dbf.newDocumentBuilder(); InputSource is=新的InputSource(新的ByteArrayInputStream(response.getBytes(“UTF-8”)); document=db.parse(is); }捕获(ParserConfiguration异常e){} 捕获(SAXE){}

Java 分析SOAP响应时返回null String response=“…”; DocumentBuilderFactory dbf=null; DocumentBuilder db=null; org.w3c.dom.Document Document=null; 试一试{ dbf=DocumentBuilderFactory.newInstance(); db=dbf.newDocumentBuilder(); InputSource is=新的InputSource(新的ByteArrayInputStream(response.getBytes(“UTF-8”)); document=db.parse(is); }捕获(ParserConfiguration异常e){} 捕获(SAXE){},java,xml,parsing,soap,Java,Xml,Parsing,Soap,文档返回为空。我尝试了不同的方法来传递到InputSource,但文档仍然返回null。知道为什么会发生这种情况吗?我刚刚试过,我可以得到元素的名称和值 String response = "<?xml version='1.0'?><soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope'><soap:Body><exch:Response xmlns:exch='http:/

文档返回为空。我尝试了不同的方法来传递到InputSource,但文档仍然返回null。知道为什么会发生这种情况吗?

我刚刚试过,我可以得到元素的名称和值

String response = "<?xml version='1.0'?><soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope'><soap:Body><exch:Response xmlns:exch='http://applicant.ffe.org/exchange/1.0'>...</exch:Response></soap:Body></soap:Envelope>";

DocumentBuilderFactory dbf = null;
DocumentBuilder db = null;
org.w3c.dom.Document document = null;
try {
   dbf = DocumentBuilderFactory.newInstance();
   db = dbf.newDocumentBuilder();
   InputSource is = new InputSource(new ByteArrayInputStream(response.getBytes("UTF-8")));
   document = db.parse(is);

 } catch(ParserConfigurationException e){}
   catch(SAXException e){}
试试看{
dbf=DocumentBuilderFactory.newInstance();
db=dbf.newDocumentBuilder();
InputSource is=新的InputSource(新的ByteArrayInputStream(response.getBytes(“UTF-8”));
document=db.parse(is);
System.out.println(document);//这里我们得到null;
System.out.println(document.getNodeName());//这里是文档;

对于(int i=0;我能指定在文档中获取null的位置吗?@yogsmaIt有效。我试图检索子节点,但它从未有任何值。在调试代码时实现。
        try {
                   dbf = DocumentBuilderFactory.newInstance();
                   db = dbf.newDocumentBuilder();
                   InputSource is = new InputSource(new ByteArrayInputStream(response.getBytes("UTF-8")));
                   document = db.parse(is);
                   System.out.println(document);//here we get null;
                       System.out.println(document.getNodeName());//here we get document;
                      for(int i =0 ; i<document.getChildNodes().getLength();i++)
                   System.out.println(document.getChildNodes().item(i).getChildNodes().item(i).getNodeName());
                }


    Output :

    [#document: null]
    document
    soap:Body