2012-07-25 107 views
5

我試圖解析Java編寫,但代碼SOAP請求的Java命名空間沒有返回任何節點 這裏是代碼有誰能夠找到錯誤解析XML使用XPath的

 String xml="<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://services.web.post.list.com\"><soapenv:Header><authInfo xsi:type=\"soap:authentication\" xmlns:soap=\"http://list.com/services/SoapRequestProcessor\"><!--You may enter the following 2 items in any order--><username xsi:type=\"xsd:string\">[email protected]</username><password xsi:type=\"xsd:string\">PfasdfRem91</password></authInfo></soapenv:Header></soapenv:Envelope>"; 
    System.out.println(xml); 
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); 
    domFactory.setNamespaceAware(true); 
    DocumentBuilder builder = domFactory.newDocumentBuilder(); 
    Document doc = builder.parse(new InputSource(new StringReader(xml))); 
    XPath xpath = XPathFactory.newInstance().newXPath(); 
    // XPath Query for showing all nodes value 

    try 
    { 
    XPathExpression expr = xpath.compile("/soapenv:Envelope/soapenv:Header/authInfo/password"); 
    Object result = expr.evaluate(doc, XPathConstants.NODESET); 
    NodeList nodes = (NodeList) result; 
    System.out.println("Got " + nodes.getLength() + " nodes"); 
// System.out.println(nodes.item(0).getNodeValue()); 
    } 
    catch(Exception E) 
    { 
     System.out.println(E); 
    } 

+0

嘗試這些: http://stackoverflow.com/questions/5673708/parsing-xml-with-xpath-in-java-get-data-from-xml-file-with-xpath-and-nodelist http://stackoverflow.com /疑問句tions/5519766/search-in-xml-file-with-xpath-in-android/5664438#5664438 – alibenmessaoud 2012-07-25 07:50:47

+0

謝謝我會試試這些 – Raheel 2012-07-25 07:54:51

回答

8

您需要設置一個NamespaceContextXPath

演示

package forum11644994; 

import java.io.StringReader; 
import java.util.Iterator; 

import javax.xml.namespace.NamespaceContext; 
import javax.xml.parsers.*; 
import javax.xml.xpath.*; 

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

public class Demo { 

    public static void main(String[] args) throws Exception { 
     String xml = "<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://services.web.post.list.com\"><soapenv:Header><authInfo xsi:type=\"soap:authentication\" xmlns:soap=\"http://list.com/services/SoapRequestProcessor\"><!--You may enter the following 2 items in any order--><username xsi:type=\"xsd:string\">[email protected]</username><password xsi:type=\"xsd:string\">PfasdfRem91</password></authInfo></soapenv:Header></soapenv:Envelope>"; 
     System.out.println(xml); 
     DocumentBuilderFactory domFactory = DocumentBuilderFactory 
       .newInstance(); 
     domFactory.setNamespaceAware(true); 
     DocumentBuilder builder = domFactory.newDocumentBuilder(); 
     Document doc = builder.parse(new InputSource(new StringReader(xml))); 
     XPath xpath = XPathFactory.newInstance().newXPath(); 
     xpath.setNamespaceContext(new NamespaceContext() { 

      @Override 
      public Iterator getPrefixes(String arg0) { 
       return null; 
      } 

      @Override 
      public String getPrefix(String arg0) { 
       return null; 
      } 

      @Override 
      public String getNamespaceURI(String arg0) { 
       if("soapenv".equals(arg0)) { 
        return "http://schemas.xmlsoap.org/soap/envelope/"; 
       } 
       return null; 
      } 
     }); 
     // XPath Query for showing all nodes value 

     try { 
      XPathExpression expr = xpath 
        .compile("/soapenv:Envelope/soapenv:Header/authInfo/password"); 
      Object result = expr.evaluate(doc, XPathConstants.NODESET); 
      NodeList nodes = (NodeList) result; 
      System.out.println("Got " + nodes.getLength() + " nodes"); 
      // System.out.println(nodes.item(0).getNodeValue()); 
     } catch (Exception E) { 
      System.out.println(E); 
     } 

    } 
} 

輸出

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.web.post.list.com"><soapenv:Header><authInfo xsi:type="soap:authentication" xmlns:soap="http://list.com/services/SoapRequestProcessor"><!--You may enter the following 2 items in any order--><username xsi:type="xsd:string">[email protected]</username><password xsi:type="xsd:string">PfasdfRem91</password></authInfo></soapenv:Header></soapenv:Envelope> 
Got 1 nodes 
+1

謝謝,幫幫我 – Mufanu 2015-04-28 11:50:59

2

添加一些最新的答覆.. 要獲取特定節點的值可以使用below-- [System.out.println(nodes.item(0).getTextContent());]