2011-11-01 57 views
1

當試圖解析方法的返回類型,方法名,並從以下WSDL(XML文檔)的參數,我得到空,每次我試圖找回從DOM文檔的內容:DOM Document null?

<?xml version="1.0" encoding="UTF-8"?> 
<wsdl:definitions targetNamespace="http://math" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://math" xmlns:intf="http://math" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<!--WSDL created by Apache Axis version: 1.4 
Built on Apr 22, 2006 (06:55:48 PDT)--> 
<wsdl:types> 
    <schema elementFormDefault="qualified" targetNamespace="http://math" xmlns="http://www.w3.org/2001/XMLSchema"> 
    <element name="addTwoInts"> 
    <complexType> 
    <sequence> 
     <element name="int1" type="xsd:int"/> 
     <element name="int2" type="xsd:int"/> 
    </sequence> 
    </complexType> 
    </element> 
    <element name="addTwoIntsResponse"> 
    <complexType> 
    <sequence> 
     <element name="addTwoIntsReturn" type="xsd:int"/> 
    </sequence> 
    </complexType> 
    </element> 
    <element name="multiplyTwoFloats"> 
    <complexType> 
    <sequence> 
     <element name="float1" type="xsd:float"/> 
     <element name="float2" type="xsd:float"/> 
    </sequence> 
    </complexType> 
    </element> 
    <element name="multiplyTwoFloatsResponse"> 
    <complexType> 
    <sequence> 
     <element name="multiplyTwoFloatsReturn" type="xsd:float"/> 
    </sequence> 
    </complexType> 
    </element> 
    </schema> 
</wsdl:types> 

    <wsdl:message name="addTwoIntsResponse"> 

     <wsdl:part element="impl:addTwoIntsResponse" name="parameters"> 

     </wsdl:part> 

    </wsdl:message> 

    <wsdl:message name="addTwoIntsRequest"> 

     <wsdl:part element="impl:addTwoInts" name="parameters"> 

     </wsdl:part> 

    </wsdl:message> 

    <wsdl:message name="multiplyTwoFloatsRequest"> 

     <wsdl:part element="impl:multiplyTwoFloats" name="parameters"> 

     </wsdl:part> 

    </wsdl:message> 

    <wsdl:message name="multiplyTwoFloatsResponse"> 

     <wsdl:part element="impl:multiplyTwoFloatsResponse" name="parameters"> 

     </wsdl:part> 

    </wsdl:message> 

    <wsdl:portType name="MathServices"> 

     <wsdl:operation name="addTwoInts"> 

     <wsdl:input message="impl:addTwoIntsRequest" name="addTwoIntsRequest"> 

     </wsdl:input> 

     <wsdl:output message="impl:addTwoIntsResponse" name="addTwoIntsResponse"> 

     </wsdl:output> 

     </wsdl:operation> 

     <wsdl:operation name="multiplyTwoFloats"> 

     <wsdl:input message="impl:multiplyTwoFloatsRequest" name="multiplyTwoFloatsRequest"> 

     </wsdl:input> 

     <wsdl:output message="impl:multiplyTwoFloatsResponse" name="multiplyTwoFloatsResponse"> 

     </wsdl:output> 

     </wsdl:operation> 

    </wsdl:portType> 

    <wsdl:binding name="MathServicesSoapBinding" type="impl:MathServices"> 

     <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> 

     <wsdl:operation name="addTwoInts"> 

     <wsdlsoap:operation soapAction=""/> 

     <wsdl:input name="addTwoIntsRequest"> 

      <wsdlsoap:body use="literal"/> 

     </wsdl:input> 

     <wsdl:output name="addTwoIntsResponse"> 

      <wsdlsoap:body use="literal"/> 

     </wsdl:output> 

     </wsdl:operation> 

     <wsdl:operation name="multiplyTwoFloats"> 

     <wsdlsoap:operation soapAction=""/> 

     <wsdl:input name="multiplyTwoFloatsRequest"> 

      <wsdlsoap:body use="literal"/> 

     </wsdl:input> 

     <wsdl:output name="multiplyTwoFloatsResponse"> 

      <wsdlsoap:body use="literal"/> 

     </wsdl:output> 

     </wsdl:operation> 

    </wsdl:binding> 

    <wsdl:service name="MathServicesService"> 

     <wsdl:port binding="impl:MathServicesSoapBinding" name="MathServices"> 

     <wsdlsoap:address location="http://localhost:8080/WSDLServer"/> 

     </wsdl:port> 

    </wsdl:service> 

</wsdl:definitions> 

這裏就是我試圖以編程方式:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 

     try { 
      DocumentBuilder db = dbf.newDocumentBuilder(); 

      //parse using builder to get DOM representation of the XML file 
      Document doc = db.parse("MathServices.xml"); 

      Node firstChild = doc.getFirstChild(); 
      System.out.println(firstChild.getNodeValue()); 


     }catch(ParserConfigurationException pce) { 
      pce.printStackTrace(); 
     }catch(SAXException se) { 
      se.printStackTrace(); 
     }catch(IOException ioe) { 
      ioe.printStackTrace(); 
     } 

正如我所提到的,firstNode.getNodeValue()返回null。在嘗試獲取其他內容時,例如根節點等,我也會得到空值。我在這裏做錯了什麼?

回答

2

節點firstChild似乎是一個有效的對象(不等於零),否則在調用firstChild.getNodeValue()時會得到空指針異常。

如果要訪問節點名稱,請使用「doc.getNodeName()」而不是「doc.getNodeValue()」。後者爲您提供了在開始和結束標籤之間列出的標籤內容(沒有任何子標籤內容),但不包括標籤名或任何屬性。 此外,由於根標籤「wsdl:definition」(在示例文件中)定義了子標籤,因此同時不能有任何文本內容。

相關問題