对于 XML 和 Web 服务以及类似的东西,我还是个新手。
我正在开发一个项目,使用 GlassFish OpenESB 来安排一个流程,从 Web 服务获取一些信息,然后存储在数据库中。
标准基本上是我必须使用 GlassFish OpenESB 或 EJB 模块,在其中我可以公开 Web 服务或类似的东西,并且我必须使用 SQL Server 2005。
到目前为止,我已经能够与网络服务对话:并收到类似的内容
<?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 树?
我很确定我应该能够使用实体和 session Bean 或者 JAXB 绑定(bind),但我根本没有成功。
原因之一可能与以下事实有关:soap 响应包含一组 CreditCardSettlements,并且每个组都包含一组 Batches 和 DeductionItems
如果有人可以帮助我通过 GlassFish OpenESB 中的 BPEL 来完成此操作,那就最好了,但非常感谢任何有关 java 解决方案的提示。
最佳答案
您将需要使用 JAXB。在/bin 中找到 xjc 工具(这应该已经在您的路径中,然后在 http://schemas.xmlsoap.org/soap/envelope/ 的架构上使用它。您应该首先下载架构,然后运行
xjc -d <directory> <schema-name>
该目录应该是源文件夹(例如 src),模式名称是您将模式下载到的位置的文件名。这将生成一堆与模式相对应的源文件。然后您可以像这样使用 JAXB Unmarshaller 工具:
JAXBContext ctx = JAXBContext.newInstance(Envelope.class.getPackage().getName();<br/>
Unmarshaller u = ctx.createUnmarshaller();<br/>
JAXBElement<Envelope> root = (JAXBElement<Envelope>) u.unmarshall(xmlStr);<br/>
Envelope envelope = root.getValue();
envelope 将代表数据结构的根,您必须以某种方式将其写入 SQL(我不知道答案)。 xmlStr 需要是包含 xml 内容的 StringBuffer。 JAXB 的文档位于 java.sun.com/javase/6/docs/api/javax/xml/bind/package-summary.html
关于java - 需要从 XML 写入 SQL Server 数据库的帮助(详细),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2951829/