我是XML和WebServices之类的新手。
我正在使用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插入数据库呢?
我很确定我应该能够使用Entity和Session Bean或JAXB绑定,但我只是没有成功。
原因之一可能与肥皂响应包含一个CreditCardSettlements数组且每个包含一个Batchs和DeductionItems数组有关。
最好是有人可以通过GlassFish OpenESB中的BPEL帮助我做到这一点,但是对Java解决方案的任何提示都是值得的。
最佳答案
您将要使用JAXB。在/ bin中找到xjc工具(应该已经在您的路径中,然后在http://schemas.xmlsoap.org/soap/envelope/的架构上使用它。您应该首先下载架构,然后运行xjc -d <directory> <schema-name>
该目录应为源文件夹(例如src),schema-name是将架构下载到的文件名。这将生成一堆与模式相对应的源文件。然后您可以使用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。 JAXB的文档位于java.sun.com/javase/6/docs/api/javax/xml/bind/package-summary.html