2017-05-18 42 views
0

這是印刷SOAP響應:無法檢索從節點在SOAP響應文本內容

<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <soapenv:Body> 
     <ns0:Get_People_Operation xmlns:ns0="urn:PeopleInterface" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
      <ns0:Full_Name>asdf - Full Name</ns0:Full_Name> 
     </ns0:Get_People_Operation> 
    </soapenv:Body> 
</soapenv:Envelope> 

的方法來提取節點值:

String assigneeInput = getNode(responseElementAssignee, "Full_Name"); 

private static String getNode(Element responseElement, String nodeValue) { 
     Node x = (Node) responseElement.getElementsByTagName(nodeValue); 
     x.getTextContent(); 

     // Test list output 
     System.out.println(""); 
     System.out.println(nodeValue + " Value: " + x.toString()); 
     System.out.println(""); 

     return x.getTextContent(); 
    } 

所有我想要的是返回的該文本內容節點<ns0:Full_Name>asdf - Full Name</ns0:Request_ID>

我在SoapUI測試,我也成功打印響應,所以它沒有任何意義,價值爲空,除非當然我不正確地處理響應。我該怎麼辦?

ClassCastException異常:

java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl cannot be cast to org.w3c.dom.Node 
    at app.controller.TableViewController.printSOAPResponse(TableViewController.java:225) 
    at app.controller.TableViewController.initialize(TableViewController.java:67) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104) 
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097) 
    at app.Main.start(Main.java:14) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863) 
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) 
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) 
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) 
    at java.lang.Thread.run(Thread.java:745) 
+0

沒有很好地形成的XML。你的開頭''標籤用''關閉。 –

+0

這是一個轉錄錯誤。打印結果是一致的。對於那個很抱歉。 – santafebound

+0

即使我解決了這個問題,我只是得到了類拋出異常。 –

回答

1

你得到了class cast exception因爲getElementsByTagName()返回類型是NodeList,這不是一個Node。因此,你不能沮喪到Node

你應該這樣做

NodeList nodeList = responseElement.getElementsByTagName(nodeValue); 
Node x = nodeList.item(0); 

您將有進一步的問題,在這裏,由於命名空間。如果您只是提供標記名稱("Full_Name"),則需要確保解析器具有名稱空間感知功能,並使用getElementsByTagNameNS("urn:PeopleInterface", nodeValue)。您可以使用名稱空間的通配符"*",該名稱空間將匹配任何名稱空間。否則,你應該提供完整的標籤名稱

下面是與命名空間感知的一個方法SSCCE(即做getNode(responseElementAssignee, "ns0:Full_Name")):

import java.io.StringReader; 

import javax.xml.parsers.DocumentBuilderFactory; 

import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.InputSource; 

public class XMLParsingTest { 

    private static final String XML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" 
      + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" 
      + "<soapenv:Body><ns0:Get_People_Operation xmlns:ns0=\"urn:PeopleInterface\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" 
      + "<ns0:Full_Name>asdf - Full Name</ns0:Full_Name>" 
      + "</ns0:Get_People_Operation>" 
      + "</soapenv:Body>" 
      + "</soapenv:Envelope>"; 

    public static void main(String[] args) throws Exception { 
     DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 
     documentBuilderFactory.setNamespaceAware(true); 
     Document doc = documentBuilderFactory.newDocumentBuilder().parse(new InputSource(new StringReader(XML))); 
     Element elt = doc.getDocumentElement(); 
     System.out.println(getNode(elt, "Full_Name")); 
    } 

    private static String getNode(Element responseElement, String nodeValue) { 

     NodeList nodeList = responseElement.getElementsByTagNameNS("urn:PeopleInterface",nodeValue); 
     Node x = nodeList.item(0); 

     // Test list output 
     System.out.println(""); 
     System.out.println(nodeValue + " Value: " + x.toString()); 
     System.out.println(""); 

     return x.getTextContent(); 
    } 

} 
+0

這工作。謝謝。然而,在此之前,我的問題是''responseElement''參數本身是空的,因此節點值的文本內容甚至不能被註冊。兩個解決方案都解決了我的問題乾杯。 – santafebound