Generate Java objects for FpML using JAXB and Maven: The Easy Way!
http://www.stephennimmo.com/generate-java-objects-for-fpml-using-jaxb-and-maven-the-easy-way/
http://www.fpml.org/dev/modules/newbbex/viewtopic.php?topic_id=135&forum=4
http://www.whatis.com.cn/word_1665.htm
金融產品標記語言(Financial Products Markup Language,FpML)是一種基于可擴展標記語言(XML)的商業信息交換標準,它使用互聯網進行商業對商業柜臺市場(場外市場,over-the-counter,OTC)的金融衍生交易。金融產品標記語言可用來在參與公司之間交流柜臺市場交易詳細資料,它也可以在公司內部分享柜臺市場交易信息,也可用于在參與公司和外部公司之間提供關于柜臺買賣交易的服務。金融產品標記語言(FpML)是免費使用的,因為它獨立于軟件或硬件被參與公司用來確保他們協同工作能力。FpML強調利率互換和遠期利率協議(FRA),但是最終它將用于柜臺市場(OTC)交易的各個方面。
兩個公司之間共有的柜臺市場(OTC)合同在滿足雙方需求的基礎下被高度用戶化。由于這一原因,在使用互聯網和可擴展標記語言(XML)之前,有效地在線傳輸OTC合同是不可能的。現在,公司能夠通過網絡構建和談判OTC合同條款、完成和確認這個合同,溝通結算細節,并用金融產品標記語言(FpML)分析風險。
美國大通曼哈頓銀行審查并在它們柜臺市場利率衍生工具應用中使用金融產品標記語言(FpML)。富士資本市場公司使用FpML的定義來設計它們基于XML的遠期利率協議認證模型。摩根公司開發了一種FpML利率交換模型應用程序。參與FpML的開發和應用的機構包括美國銀行、花旗集團、德意志銀行、IBM公司、普華永道公司、摩根公司、路透社和瑞銀華寶。
?
First, go get the schemas from?http://www.fpml.org/spec/latest.php. You will have to register. When you log in, make sure your have Specifications radio selected.
We are going to work with the latest version. Download the zip file for whichever group of FpML you want.
Once that file is downloaded, fire up eclipse and create a generic maven project.
For?generating?the JAXB objects, we are going to use a maven plugin from Codehaus. Documentation for the plugin can be found at http://mojo.codehaus.org/jaxb2-maven-plugin/index.html. Notice that I put the url for the plugin into a comment at the top of the plugin.
To use the plugins out of the box (mostly), let’s start by creating a directory for the xsds. Because there are four sets of xsds for FpML, I organize them by creating the normal src/main/xsd folder and then unzip the contents of the FpML download into the folder. It should unzip to a directory for whatever download you selected (xml_confirmation).
I like making my objects serializable for JAXB, so I add an xjb binding. Create the folder src/main/xjb and add a jaxb-bindings.xml file in there with:
?| <?xml version="1.0" encoding="UTF-8"?> <bindings xmlns="http://java.sun.com/xml/ns/jaxb"? ????xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" ????xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" ????xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd" version="2.1"> ????<globalBindings> ????????<serializable uid="54" /> ????</globalBindings> </bindings> |
Next we are going to add the plugin. Again, because FpML could be four different downloads and if you want to generate objects for all 4 flavors, we will want to use the “Multiple schemas with different configuration” which has documentation at http://mojo.codehaus.org/jaxb2-maven-plugin/usage.html. For our purposes, we will implement like this.
?| <plugin> ????<!-- http://mojo.codehaus.org/jaxb2-maven-plugin/index.html --> ????<groupId>org.codehaus.mojo</groupId> ????<artifactId>jaxb2-maven-plugin</artifactId> ????<version>1.5</version> ????<executions> ????????<execution> ????????????<id>xml_confirmation-xjc</id> ????????????<goals> ????????????????<goal>xjc</goal> ????????????</goals> ????????????<configuration> ????????????????<schemaDirectory>${project.basedir}/src/main/xsd/xml_confirmation</schemaDirectory> ????????????????<packageName>org.fpml.confirmation</packageName> ????????????????<staleFile>${project.build.directory}/jaxb2/.confirmationXjcStaleFlag</staleFile> ????????????</configuration> ????????</execution> ????????<execution> ????????????<id>xml_recordkeeping-xjc</id> ????????????<goals> ????????????????<goal>xjc</goal> ????????????</goals> ????????????<configuration> ????????????????<schemaDirectory>${project.basedir}/src/main/xsd/xml_recordkeeping</schemaDirectory> ????????????????<packageName>org.fpml.recordkeeping</packageName> ????????????????<staleFile>${project.build.directory}/jaxb2/.recordkeepingXjcStaleFlag</staleFile> ????????????</configuration> ????????</execution> ????????<execution> ????????????<id>xml_reporting-xjc</id> ????????????<goals> ????????????????<goal>xjc</goal> ????????????</goals> ????????????<configuration> ????????????????<schemaDirectory>${project.basedir}/src/main/xsd/xml_reporting</schemaDirectory> ????????????????<packageName>org.fpml.reporting</packageName> ????????????????<staleFile>${project.build.directory}/jaxb2/.reportingXjcStaleFlag</staleFile> ????????????</configuration> ????????</execution> ????????<execution> ????????????<id>xml_transparency-xjc</id> ????????????<goals> ????????????????<goal>xjc</goal> ????????????</goals> ????????????<configuration> ????????????????<schemaDirectory>${project.basedir}/src/main/xsd/xml_transparency</schemaDirectory> ????????????????<packageName>org.fpml.transparency</packageName> ????????????????<staleFile>${project.build.directory}/jaxb2/.transparencyXjcStaleFlag</staleFile> ????????????</configuration> ????????</execution> ????</executions> ????<configuration> ????????<outputDirectory>${project.basedir}/src/main/java</outputDirectory> ????</configuration> </plugin> |
Once everything is there, simply run the generate-sources target with Maven (I use the IDE because I am lazy) and it will generate your entire object model. Start coding….
But wait! There’s more. One last trick. The JAXB generation does not create and @XmlRootElement annotations on ANY of the objects. Why? Travel back to 2006 and have the answer. http://weblogs.java.net/blog/2006/03/03/why-does-jaxb-put-xmlrootelement-sometimes-not-always
So how do we marshall? Use the ObjectFactory. Here’s some example code which took me way too long to figure out.
?| JAXBContext jc = JAXBContext.newInstance("org.fpml.recordkeeping"); ObjectFactory objectFactory = new ObjectFactory(); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); ?? NonpublicExecutionReport report = objectFactory.createNonpublicExecutionReport(); RequestMessageHeader header = new RequestMessageHeader(); MessageId messageId = objectFactory.createMessageId(); messageId.setValue(UUID.randomUUID().toString()); header.setMessageId(messageId); report.getHeader().getMessageId().setValue(UUID.randomUUID().toString()); ?? //Use the ObjectFactory to create the JAXB<Object> wrapped!!! This is the key! marshaller.marshal(objectFactory.createNonpublicExecutionReport(report), System.out); |
Alright, get started.
?
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的Generate Java objects for FpML using JAXB and Maven: The Easy Way!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: QuickFIX/N入门
- 下一篇: 如何在 Outlook 中使用外出时的助