java - DOM 文档为空?

标签 java xml dom wsdl document

当尝试从以下 WSDL(xml 文档)解析方法返回类型、方法名称和参数时,每次尝试从 DOM 文档检索某些内容时都会得到 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。当尝试获取其他内容(例如根节点等)时,我也会得到空值。我在这里做错了什么?

最佳答案

节点firstChild似乎是一个有效的对象(不等于0),否则在调用firstChild.getNodeValue()时会得到空指针异常。

如果要访问节点名称,请使用“doc.getNodeName()”而不是“doc.getNodeValue()”。后一个为您提供在开始标签和结束标签之间列出的标签内容(没有任何子标签内容),但不提供标签名或任何属性。 此外,由于根标签“wsdl:definition”(在示例文件中)定义了子标签,因此不能同时包含任何文本内容。

关于java - DOM 文档为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7961198/

相关文章:

java - 如何从一个txt文件填充2个数组

c# - 在 C# 中,有没有一种方法可以在不先加载 XML 节点的情况下将其添加到磁盘上的文件中?

java - java中解析xml字符串

PHP Composer 不会使用 php 5.6 安装 Drush : phpunit/phpunit 4. 8.x 需要 ext-dom

javascript - 为什么jQuery或诸如getElementById之类的DOM方法找不到元素?

jQuery - 通过 $(this) 访问层次结构上五层的父 <li>

java - 如何手动部署war文件到tomcat?

java - JBehave - 一个故事中不可能有多个结果吗?

java - 如何在Android Studio中将小部件置于其他提升的小部件的前面

java - Android 带指针的圆形图像