Java 不使用NodeList获取XML标记值

Java 不使用NodeList获取XML标记值,java,xml,nodelist,Java,Xml,Nodelist,我将此响应XML设置为字符串 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <ns:SRVResponse> <ns:Response> <ns1:ServiceHeader> <ns1:rsHeader> &

我将此响应XML设置为字符串

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
  <ns:SRVResponse>
     <ns:Response>
        <ns1:ServiceHeader>
           <ns1:rsHeader>
              <ns1:status>
                 <ns1:finStatus>E</ns1:finStatus>
                 </ns1:status>
            </ns1:rsHeader>
        </ns1:ServiceHeader>
    </ns:Response>
</ns:SRVResponse>
</soapenv:Body>
</soapenv:Envelope>

我没有得到任何异常,但得到的是空值。这本书缺了什么?如果您有任何想法,我们将不胜感激。

您可以尝试,因为这在我的代码中起作用。您需要给出绝对路径

下面是我正在使用的xml

     * xmlInput =
     * "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n"
     * + "    <soap:Body>\r\n" +
     * "        <ns1:LoginProcesRequest xmlns:ns1=\"http://xmlns.oracle.com/BioRad_jws/BR_GetSFDCLogin/GetSFDCDetailsProcess\">\r\n"
     * + "           <ns1:Login>1000</ns1:Login>\r\n" +
     * "           <ns1:Company>1000</ns1:Company>\r\n" +
     * "        </ns1:LoginProcesRequest>\r\n" + "    </soap:Body>\r\n" +
     * "</soap:Envelope>";
     */

你可以试试这个,因为这在我的代码中起作用。你需要给出绝对路径

下面是我正在使用的xml

     * xmlInput =
     * "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n"
     * + "    <soap:Body>\r\n" +
     * "        <ns1:LoginProcesRequest xmlns:ns1=\"http://xmlns.oracle.com/BioRad_jws/BR_GetSFDCLogin/GetSFDCDetailsProcess\">\r\n"
     * + "           <ns1:Login>1000</ns1:Login>\r\n" +
     * "           <ns1:Company>1000</ns1:Company>\r\n" +
     * "        </ns1:LoginProcesRequest>\r\n" + "    </soap:Body>\r\n" +
     * "</soap:Envelope>";
     */

你把它通过调试器了吗?有时,最好的解决方法是创建一个独立的程序并进行调试。在这种情况下,应该很容易做到。el.getElementsByTagName(elemTag).item(0).getTextContent()这将作为null来调试摆脱链接并查看返回null的内容。-看这里,你把它通过调试器了吗?有时,最好的解决方法是创建一个独立的程序并进行调试。在这种情况下,应该很容易做到。el.getElementsByTagName(elemTag).item(0).getTextContent()这将作为null来调试摆脱链接并查看返回null的内容。-看到这里了吗?你想从response中获取什么?它是fetching sessionid。response的格式也与我的请求sharedOk Vikram的格式相似。让我检查一下并返回UCA,您是否可以共享您正在尝试的更新代码System.out.println((字符串)xpath.evaluate(“/Envelope/Body/SRVResponse/Response/ServiceHeader/rsHeader/status/finStatus”,doc,XPathConstants.String));您试图获取什么?它是来自response的Fetching sessionid。response的格式也与我的sharedOk Vikram请求的格式相似。让我检查一下并返回UCA,您是否可以共享您正在尝试的更新代码System.out.println((字符串)xpath.evaluate(“/Envelope/Body/SRVResponse/Response/ServiceHeader/rsHeader/status/finStatus”,doc,XPathConstants.String));
     * xmlInput =
     * "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n"
     * + "    <soap:Body>\r\n" +
     * "        <ns1:LoginProcesRequest xmlns:ns1=\"http://xmlns.oracle.com/BioRad_jws/BR_GetSFDCLogin/GetSFDCDetailsProcess\">\r\n"
     * + "           <ns1:Login>1000</ns1:Login>\r\n" +
     * "           <ns1:Company>1000</ns1:Company>\r\n" +
     * "        </ns1:LoginProcesRequest>\r\n" + "    </soap:Body>\r\n" +
     * "</soap:Envelope>";
     */
 StringWriter out = new StringWriter();
        TransformerFactory transformerFactory = TransformerFactory
                .newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        Source sourceContent = soapResponse.getSOAPPart().getContent();
        transformer.transform(sourceContent, new StreamResult(out));
        String response = out.toString();

DocumentBuilderFactory docFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

            Document doc = docBuilder.parse(new InputSource(new StringReader(
                    response)));

            XPathFactory factory = XPathFactory.newInstance();
            XPath xpath = factory.newXPath();

            header.setSessionId((String) xpath
                    .evaluate(
                            "/Envelope/Body/LoginProcessResponse/loginResponse/sessionId",
                            doc, XPathConstants.STRING));