2010-06-01 80 views
1

我是一個XML和WebServices和類似的東西的新手。需要幫助,從XML寫入到SQL Server數據庫(詳細)

我正在使用GlassFish OpenESB安排一個項目,以便從web服務獲取一些信息並存儲到數據庫中。

的標準基本上是我必須使用GlassFish中的OpenESB或EJB模塊,我可以沿着這些線路暴露web服務什麼的,我必須使用SQL Server 2005的

到目前爲止,我已經能夠聊到web服務:並沿着這些線路收到東西

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.xmlsoap.org/soap/envelope/ http://schemas.xmlsoap.org/soap/envelope/"> 
    <SOAP-ENV:Body> 
    <m:entrypoint_getSettlementsOperationResponse xmlns:m="http://j2ee.netbeans.org/wsdl/BorgunTestBPEL/entrypoint_getSettlements"> 
     <part1> 
     <GetSettlementsByMerchantResponse xmlns="http://Borgun.Services.Gateway/2010/04/Settlement"> 
      <GetSettlementsByMerchantResult xmlns:a="http://schemas.datacontract.org/2004/07/Borgun.Library.Common" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:msgns="http://Borgun.Services.Gateway/2010/04/Settlement" xmlns:ns0="http://j2ee.netbeans.org/wsdl/BorgunTestBPEL/entrypoint_getSettlements"> 
      <a:CreditCardSettlement> 
       <a:amexAmount>XXX</a:amexAmount> 
       <a:amount>XXXX</a:amount> 
       <a:batches> 
       <a:CreditCardBatch> 
        <a:batchdate>xxx</a:batchdate> 
        <a:batchnumber>XXXX</a:batchnumber> 
        <a:currencyCode>xxxx</a:currencyCode> 
        <a:merchantnumber>xxxx</a:merchantnumber> 
        <a:settlementRunNumber>xx4</a:settlementRunNumber> 
        <a:settlementdate>2010-04-06T00:00:00</a:settlementdate> 
        <a:slips>2</a:slips> 
        <a:sum>xxxx</a:sum> 
       </a:CreditCardBatch> 
       <a:CreditCardBatch> 
        <a:batchdate>xxx</a:batchdate> 
        <a:batchnumber>xxxxx</a:batchnumber> 
        <a:currencyCode>xxxx</a:currencyCode> 
        <a:merchantnumber>xxxx</a:merchantnumber> 
        <a:settlementRunNumber>xxxx</a:settlementRunNumber> 
        <a:settlementdate>xxxx</a:settlementdate> 
        <a:slips>x</a:slips> 
        <a:sum>xxx</a:sum> 
       </a:CreditCardBatch> 
       </a:batches> 
       <a:commission>xx</a:commission> 
       <a:currencyCode>xxx</a:currencyCode> 
       <a:deduction>-xxx</a:deduction> 
       <a:deductionItems> 
       <a:CrediCardSettlementDeduction> 
        <a:amount>-xxx</a:amount> 
        <a:code>VIÐSKF</a:code> 
        <a:currencyCode>ISK</a:currencyCode> 
        <a:merchantnumber>xxx</a:merchantnumber> 
        <a:settlementrunnumber>xxx</a:settlementrunnumber> 
        <a:text>Afsláttur v/ekorta</a:text> 
       </a:CrediCardSettlementDeduction> 
       <a:CrediCardSettlementDeduction> 
        <a:amount>-335.00</a:amount> 
        <a:code>ÁLAGKREK</a:code> 
        <a:currencyCode>ISK</a:currencyCode> 
        <a:merchantnumber>xxx</a:merchantnumber> 
        <a:settlementrunnumber>xxx</a:settlementrunnumber> 
        <a:text>xxx</a:text> 
       </a:CrediCardSettlementDeduction> 
       </a:deductionItems> 
      </a:CreditCardSettlement> 
      </GetSettlementsByMerchantResult> 
     </GetSettlementsByMerchantResponse> 
     </part1> 
    </m:entrypoint_getSettlementsOperationResponse> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

我可以訪問哪些是遠程的SQL Server 2005服務器,我知道我可以插入,但考慮到現在我有一個一對多關係我希望能夠在事情失敗時回滾。

因此總之,我怎樣才能從這個XML插入數據庫,最好不用手動遍歷XML樹?

我很確定我應該能夠使用實體和會話Bean或JAXB綁定,但我根本沒有成功。

其中的一個原因可能有一些做的肥皂響應包含CreditCardSettlements的陣列,並且其中的每一個事實包含批次的陣列和DeductionItems

這將是最好的,如果有人能幫助我做這通過GlassFish OpenESB中的BPEL,但對Java解決方案的任何提示都非常感謝。

回答

0

您將要使用JAXB。您應該先下載架構,然後運行
xjc -d <directory> <schema-name>
該目錄應該是一個源文件夾(例如src )和架構名稱是無論你下載的架構,這將產生一堆源文件的對應於模式的文件名,那麼你可以使用JAXB的Unmarshaller工具是這樣的:。
JAXBContext ctx = JAXBContext.newInstance(Envelope.class.getPackage().getName();
Unmarshaller u = ctx.createUnmarshaller();
JAXBElement<Envelope> root = (JAXBElement<Envelope>) u.unmarshall(xmlStr);
Envelope envelope = root.getValue();

信封將代表你必須寫入SQL的數據結構的根(我不知道答案是什麼)。xmlStr需要是帶有xml內容的StringBuffer。 doc for JAXB are at java.sun.com/javase/6/docs/api/javax/xml/bind/package-summary.html