2013-07-05 45 views
2

我正在嘗試使用jaxb解組肥皂響應。使用命名空間前綴解組肥皂響應

我嘗試使用下面的代碼來找到根元素

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     dbf.setNamespaceAware(false); 
     DocumentBuilder db; 
     try { 
      db = dbf.newDocumentBuilder(); 
      Document d = db.parse(new File("response.xml")); 

       Unmarshaller unmarshaller = null; 
       Results results = null; 
       unmarshaller = JAXBContext.newInstance(Results.class).createUnmarshaller(); 

        Node getNumberResponseElt = d.getElementsByTagName("results").item(0); 
        JAXBElement<Results> rs = unmarshaller.unmarshal(new DOMSource(getNumberResponseElt),Results.class); 
        results = rs.getValue(); 

      } 

這裏是我的樣本響應

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<soapenv:Header/> 
<soapenv:Body> 
<service:ServiceResponse xmlns:out="http://example.com/Person/PersonData/v1" xmlns:service="http://example.com/Person/PersonData/v1/" xmlns:xs4xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<results> 
<out:result> 
<out:person> 
<out:personName> 
<out:firstName>First</out:firstName> 
<out:lastName>Last</out:lastName> 
</out:personName> 
<out:gender>M</out:gender> 
</out:person></out:result></results></service:ServiceResponse></soapenv:Body></soapenv:Envelope> 

林抽到results對象,但是當我嘗試訪問resultresults它顯示爲空因爲前綴out:

任何人都可以幫助我getti這個呢?

UPDATE: 任何人都可以幫助我嗎?

我用stax xml解析器解析到results,但我看到它中的所有值爲空。

回答

1

我認爲問題是命名空間的位置。您可以使用StAX的XMLStreamReader的在這一點上解析從XMLStreamReader的XML並提前正確的元素,然後解組

http://blog.bdoughan.com/2012/08/handle-middle-of-xml-document-with-jaxb.html

+0

感謝您的答覆亞歷克斯。我嘗試過使用Stax,並且能夠從結果中捕獲結果,但不能捕獲personName中的值。它們顯示爲空。 – rahul