This version of cb2xml contains fix for 88 levels on comp/comp-3 variables + new python example program.
- Uses of cb2xml
- cb2xml Xml File description and schemas
- How to use cb2xml
- Useful software for Cobol Data
- New Jaxb Jar
- Change Summary
In version 0.95.7,the format of the value tag has changed. Basically
**Cobol Value clause** | **Xml value tag** |
value 121 | value="121" |
value zero | value="zero" |
value 'some text' | value="'some text'" |
value "some other text" | value=""some other text"" |
There are three main uses of the cb2xml -
- Converting a Cobol Copybook to an equivalent Xml-Copybook. There are many ways of processing this Xml:
-
A sample of how to process this Xml file using jaxb can be found in Demo2.java.
-
The JRecord project processes a cb2xml Xml document using a java program. JRecord
-
While the cobol2j project uses an XSL transform to convert the cb2xml-file-description to its own Xml file-description-format.
-
CopybookUtils is a Ruby Gem for Ruby gem for reading Cobol Text files using cb2xml-Xml copybooks
-
cbgen Written Scala, it uses it's own Copybook processor to extract Cobol details
-
- Cobol-data-file to Xml file (Dat2Xml program). There is an enhance version in JRecords Data2Xml
- Xml File to Cobol-data-file (Xml2Dat program). There is an enhance version in JRecords Xml2Data
Note: The Dat2Xml and Xml2Dat have the following limitations:
- only handle text files.
- Xml2Dat does not handle arrays !!!.
- The size of the files they can handle is fairly limited because both the Cobol-Data file and Xml File (DOM format) are stored in memory during the conversion process
All these issues are resolved in JRecords Data2Xml / Xml2Data.
cb2xml.xsd and cb2xml.dtd xml schemas can be found in the xmlSchema directory.
Following is sample Cobol copybook :
1 03 f1 **pic** x(3). 2 03 f2-comp **pic** s9(4) **comp**. 3 03 f3-comp-3 **pic** s9(6)V99 **comp-3**.
once the Cobol Copybook is processed by cb2xml:
3 <itemdisplay-length="3"level="03"name="f1"picture="x(3)"position="1"storage-length="3"/> 4 <itemdisplay-length="4"level="03"name="f2-comp"numeric="true"picture="s9(4)"position="4"signed="true"storage-length="2"usage="computational"/> 5 <itemdisplay-length="8"level="03"name="f3-comp-3"numeric="true"picture="s9(6)V99"position="6"scale="2"signed="true"storage-length="5"usage="computational-3"/>
A more compilcated example:
1 01 Vendor. 2 03 Brand **Pic** x(3). 3 03 Location-details. 4 05 Location-Number **Pic** 9(4). 5 05 Location-Type **Pic** XX. 6 05 Location-Name **Pic** X(35). 7 03 Address-Details. 8 05 actual-address. 9 10 Address-1 **Pic** X(40). 10 10 Address-2 **Pic** X(40). 11 10 Address-3 **Pic** X(35). 12 05 Postcode **Pic** 9(4). 13 05 Empty **pic** x(6). 14 05 State **Pic** XXX. 15 03 Location-Active **Pic** X.
This is converted into the following Xml by cb2xml, please note Nested Groups in the Cobol get converted to Nested Items in the Xml:
1 <copybookfilename="cbl2xml_Test110.cbl"> 2 <itemdisplay-length="173"level="01"name="Ams-Vendor"position="1"storage-length="173"> 3 <itemdisplay-length="3"level="03"name="Brand"picture="x(3)"position="1"storage-length="3"/> 4 <itemdisplay-length="41"level="03"name="Location-details"position="4"storage-length="41"> 5 <itemdisplay-length="4"level="05"name="Location-Number"picture="9(4)"position="4"storage-length="4"numeric="true"/> 6 <itemdisplay-length="2"level="05"name="Location-Type"picture="XX"position="8"storage-length="2"/> 7 <itemdisplay-length="35"level="05"name="Location-Name"picture="X(35)"position="10"storage-length="35"/> 8 </item> 9 <itemdisplay-length="128"level="03"name="Address-Details"position="45"storage-length="128"> 10 <itemdisplay-length="115"level="05"name="actual-address"position="45"storage-length="115"> 11 <itemdisplay-length="40"level="10"name="Address-1"picture="X(40)"position="45"storage-length="40"/> 12 <itemdisplay-length="40"level="10"name="Address-2"picture="X(40)"position="85"storage-length="40"/> 13 <itemdisplay-length="35"level="10"name="Address-3"picture="X(35)"position="125"storage-length="35"/> 14 </item> 15 <itemdisplay-length="4"level="05"name="Postcode"numeric="true"picture="9(4)"position="160"storage-length="4"/> 16 <itemdisplay-length="6"level="05"name="Empty"picture="x(6)"position="164"storage-length="6"/> 17 <itemdisplay-length="3"level="05"name="State"picture="XXX"position="170"storage-length="3"/> 18 </item> 19 <itemdisplay-length="1"level="03"name="Location-Active"picture="X"position="173"storage-length="1"/> 20 </item> 21 </copybook>
The attributes name, numeric, picture and signed are all fairly obvious. The other attributes include:
-
position - position in the record (1 is the first byte).
-
storage-length - length in bytes of the field.
-
scale - scaling factor, used when there is an assumed decimal point (Cobol V picture) or assumed zero (Cobol P picture).
- scale=-1 - multiply by 10 (e.g. Picture of S9(4)P)
- scale=1 - divide by 10 (e.g. Picture of S9(4)V9)
- scale=2 - divide by 100 (e.g. Picture of S9(4)V99)
Background information: - Cobol uses an "assumed decimal" in representing numbers for expample a dollar amount could be represented in Cobol as Pic s9(7)V99 comp this is implemented as a binary integer but where binary 1 actually represent 0.01, i.e. integer values need to be scaled
-
usage, Cobol usage attribute; typically computational is Big-Endian-Binary, computational-3 is packed decimal, computational-5 is Machines-Binary-Integer (Little endian on Intel Hardware, big-endian on the Mainframe and Large-servers).
-
editted-numeric - Wether this field is an editted-numeric or not. In Cobol s9(4)V99 is a true numeric and can be used in numeric calculations while -(3)9.99 is called a editted-numeric but strictly speaking it is a charactger field and can not be used in calculations, only for formatting. In cb2xml both s9(4)V99 and -(3)9.99 are flagged as numeric but only -(3)9.99 is flagged as editted-numeric.
-
inherited-usage - Normally in cobol the usage is specified on each field:
The examples directory holds sample rexx, bat and shell scripts that call cb2xml.jar to:
- Convert Cobol Copybooks to Xml-File-Descriptions (cobol2xmlFileDescription.*)
- Convert Cobol Data files to Xml-Files (data2xml.*)
- Convert Xml-Files to Cobol Data files (xml2data.*)
Basically to convert a Cobol Copybook to Xml (bat / shell script):
7 java -jar ../../lib/cb2xml.jar -cobol cbl2xml_Test102.cbl -indentXml -xml cbl2xml_Test102_new3.cbl.xml 8 java -jar ../../lib/cb2xml.jar -cobol cpyUtf8.cbl -indentXml -font utf-8 -xml cpyUtf8_new4.cbl.xml
The parameters to cb2xml are:
- -cobol - input Cobol copybook
- -xml - output Xml translation.
- -font - Copybook font.
- -indentXml - wether to indent (pretty print) the Xml.
- -debug - wether
To load a Cobol Copybook in Java / JVM languages:
43 Copybook copybook **=** CobolParser.newParser**(****)** 44 .parseCobol**(**copybookName**)**;
To create an Xml-Documnet in java:
1 //Ifyouwantprocessacobolcopybookwithcolumnspecifiedinthecb2xml.propertiesfile 2 Document doc **=** Cb2Xml.convertToXMLDOM**(**cobolCopybookFile**)**; 3 //or 4 Document doc **=** Cb2Xml2.convertToXMLDOM**(**cobolCopybookFile, false, Cb2xmlConstants.USE_PROPERTIES_FILE**)**;
By default Cobol uses columns 6 --> 71 but some Cobol-Compilers allow other columns to be used. To cover for this cb2xml programs has a
Copybook Format | Description |
---|---|
**Cb2xmlConstants.USE_PROPERTIES_FILE** | Get the line lengths from the cb2xml.properties file |
**Cb2xmlConstants.USE_STANDARD_COLUMNS** | Standard Cobol Columns (6->71) |
**Cb2xmlConstants.USE_COLS_6_TO_80** | Columns 6->80 |
**Cb2xmlConstants.USE_LONG_LINE** | Use columns 6 to whatever Columns (essentially 6->Max-Int) |
**Cb2xmlConstants.FREE_FORMAT** | Free Formet (any column essentially 1->Max-Int) |
Groovy Code to load and print Cobol copybook:
9 //ParsingaCobolcopybook 10 **def** cpy **=** CobolParser.newParser**(****)**.parseCobol**(**"cbl2xml_Test110.cbl"**)** 11 12 //Nowletsprintit 13 **print****(**"", cpy.getItem**(****)****)** 14 15 //PrintCobolitem(anditschilditems 16 **def** **print****(**String indent, List items**)** { 17 18 **for** **(**Item item : items**)** { 19 String s **=** indent **+** item.getName**(****)** **+** "" 20 s **=** s.substring**(**0, 50**)** 21 **println** s **+** item.getPosition**(****)** **+** "\t" **+** item.getStorageLength**(****)** **+** "\t" **+** item.getDisplayLength**(****)** **+** cn**(**item.getPicture**(****)****)** **+** cn**(**item.getUsage**(****)****)** **+** cn**(**item.isNumeric**(****)****)** **+** cn**(**item.isSigned**(****)****)** 22 23 **print****(**indent **+** "", item.getItem**(****)****)** 24 } 25 } 26
Python Code to load and print cb2xml - Xml
29 **def** printItem**(**indent, item**)****:** 30 n **=** indent **+** item.attrib['level'] **+** "" **+** item.attrib['name'] **+** "" 31 n **=** n[**:**50] 32 33 **print** n, '\t', item.attrib['position'], '\t', item.attrib['storage-length'], '\t', getAttr**(**item, 'display-length'**)**, getAttr**(**item, 'picture'**)**, getAttrId**(**item, 'usage'**)**, getAttrId**(**item, 'numeric'**)**, getAttrId**(**item, 'signed'**)** 34 **for** child **in** item.findall**(**'item'**)****:** 35 printItem**(**indent **+** "", child**)** 36 37 ######################################################################### 38 39 tree **=** ET.parse**(**'cbl2xml_Test110.cbl.xml'**)** 40 root **=** tree.getroot**(****)** 41 42 **print** ">>", root.tag, root.attrib['filename'] 43 44 **for** child **in** root.findall**(**'item'**)****:** 45 printItem**(**"", child**)** 46
Ruby Code to load and print cb2xml - Xml
10 **def** printXml indent, tree 11 12 tree.each **do** **|**node**|** 13 **puts** "**#{**indent**}****#{**node**[**"level"**]****}****#{**node**[**"name"**]****}**\t**#{**node**[**"position"**]****}**\t**#{**node**[**"storage-length"**]****}**\t**#{**node**[**"picture"**]****}**\t**#{**node**[**"usage"**]****}**" 14 printXml "**#{**indent**}**", node.children 15 **end** 16 **end** 17 18 doc **=** Nokogiri.XML**(**File.open**(**"cb2xml_Output110.xml"**)****)** 19 20 printXml "", doc.root.children
To convert a Cobol-Data-file to a DOM Document:
1 String sourceFileContents**=** FileUtils.readFile**(**dataFileName**)**.toString**(****)**; 2 Document copyBookXml **=** XmlUtils.fileToDom**(**copybookFileName**)**; 3 4 Document xmlDoc **new** MainframeToXml**(****)**.convert**(**sourceFileContents, copyBookXml**)**;
To convert Xml file to Cobol-Data file:
1 Document sourceFileXml **=** XmlUtils.fileToDom**(**dataFileName**)**; 2 Document copyBookXml **=** XmlUtils.fileToDom**(**copybookFileName**)**; 3 4 String fileData **=** **new** XmlToMainframe**(****)**.convert**(**sourceFileXml, copyBookXml**)**;
Examples of processing the cb2xml generated DOM / Xml:
Example | Description |
---|---|
[Examples](examples/) | There are very basic [python](examples/PythonExample/), [ruby](examples/RubyExample/), [groovy](examples/GroovyExample/), [jython](examples/JythonExample/) and [jruby](examples/JRubyExample/) programs to print a _cb2xml Xml_ file |
[Demo.java](source/cb2xml_examples/src/net/sf/cb2xml/example/Demo.java) | A basic program that loads _cb2xml Xml_ via JAXB and then prints the copybook |
[Demo2.java](source/cb2xml_examples/src/net/sf/cb2xml/example/Demo2.java) | A basic program that converts a _Cobol Copybook_ to a DOM document, loads this via JAXB then prints the copybook |
[DemoCobolJTreeTable.java](source/cb2xml_examples/src/net/sf/cb2xml/example/DemoCobolJTreeTable.java) | A basic program to display a Cobol copybook in a TreeTable. |
[source/cb2xml_tests/src/tests](source/cb2xml_tests/src/tests) | Holds examples of calling cb2xml from java. In particular _TstCb2xml02_ has examples of calling Cb2Xml to get get a _DOM Document_ and TstCblDataToXml01 call Dat2Xml and Xml2Dat. |
[MainframeToXml.java](source/cb2xml/src/net/sf/cb2xml/convert/MainframeToXml.java) | Uses a **cb2xml-Xml** file to convert a _Cobol-Text_ file to Xml |
[XmlToMainframe.java](source/cb2xml/src/net/sf/cb2xml/convert/XmlToMainframe.java) | Uses a **cb2xml-Xml** file to convert a _Xml_ file to a _Cobol-Text_ file. |
[JRecord - XmlCopybookLoader](http://jrecord.cvs.sourceforge.net/viewvc/jrecord/jrecord/src/net/sf/JRecord/External/XmlCopybookLoader.java?view=markup) | JRecords code to process a _cb2xml Xml_ Document. |
[XSL transform](http://cobol2j.cvs.sourceforge.net/viewvc/cobol2j/cobol2j/src/main/xml/cb2xml2cobol2j.xsl?view=markup) | Xsl Transform used in the cobol2j package to convert **cb2xml-Xml** ot its own Xml format. This method does not work for all possible Cobol Copybooks. |
[Pretty Print](source/cb2xml_examples/src/net/sf/cb2xml/example/PrettyPrintCopybook.java) | uses a StAX parser to process the Xml. |
If you need to work with Cobol files, you may find some of this software useful:
- The RecordEditor lets you view/edit Cobol-data files with a cobol copybook (you have to import the cobol copybook into the RecordEditor).
- If you need to process binary Cobol files in Java consider JRecord, cobol2j or one of several other packages.
- For Cobol Data to/from Csv, have a look at JRecords JRecords Data2Xml / Xml2Data. Cobol2Csv and Csv2Cobol programs.
- For Cobol to/from Xml look at JRecords
- Another processing option is the legstar packages.
A new Jar cb2xml_Jaxb.jar has been crated. This jar holds Java Copybook and Item classes + code to create them from either a Cobol Copybook or a cb2xml-Xml file.
Longer term if there is enough interest in the jars contents, it will be included in the standard cb2xml.jar. The trouble with including it now is I will be stuck with it even hardly any one uses it.
- I am hoping to bring support for other Cobol Dialects (e.g. Gnu-Cobol) to cb2xml.
- I will also add a tag for the actual field type (e.g packed-decimal Mainframe-Zoned-Decimal, Fujitsu-zoned decimal, Packed-decimal etc).
- Option to return a copyook and Item class so you users of Cb2Xml do not need to pass the Document.
-
Updated the value tag. Text values are now in quotes to distinguish them from numeric and cobol constants (like zero, spaces etc).
**Cobol Value clause** **Xml value tag** value 121 value="121" value zero value="zero" value 'some text' value="'some text'" value "some other text" value=""some other text"" -
Adding a justified tag (for when justified or justified right is used in the Cobol Copybook).
Cobol move copybook
- Fix for 88 levels on comp/comp-3 values.
- Added toTextCopybook.py example program. It will create a Text version of a copybook + Cobol move copybook to move values from the Text-Copybook
- Fix for position after redefines.
- Added ObscureCobol example program. It will "Obfuscate" a Cobol copybook.
- Added support for the use of Reader (instead of stream) in the library
- Added -font and -indentXml options
- Fix for usage specified at the Group Level
- Fix for dropping quotes in value tag
- Added support continuation of value across multiple lines
- New JAXB jar for accessing cb2xml - Xml.
- Added examples in Ruby, JRuby, Jython and Groovey. Updated python example.
- Added 2 new Xml tags: editted-numeric and inherited-usage.
- Updated documentation.
- Fix for USE_SUPPLIED_COLUMNS option.
- Created a new class Cb2Xml2 which is like the Cb2Xml class except it does not trap errors !!
- Minor improvements in the error messages from the Cobol Parser.
- Created DTD and Xsd schemas for the Xml created by cb2xml.
- Created Java Jaxb example programs for processing cb2xml Xml.
- Created a basic python program to print cb2xml Xml.
- Improved support for P picture character, new assumed digits tag + Scale can be negative (due to P picture option).
- Introduced constant class for the attributes used by Cb2xml. This allows other projects to integrate better with cb2xml
- Most methods accept stream as well File (Feature request 1)
- Support for Hex literals (patch 1)
- Support for sync keyword
- Fixed issue with fields starting with numerics
- Fixed issue with comments at start of copybook in dat2xml and xml2dat
- Fixed issue with “anonymous” REDEFINES (problem 12)
- Fixed issue with sign seperate (problem 6)
- Option to pass columns to CobolPreprocessor. This means when the calling program passes the start/end column the file cb2xml.properties is not needed anymore.
- Added some basic Automated Tests, this will allow regular releases in the future.
- dat2xml - trim numerics
- dat2xml - Fixed issue with occurs (problem 4 and 10)
- xml2dat - Changed to set field to spaces (when tag is absent in the Xml and no initialise is set). (problem 11).
- xml2dat - fix for when incoming data is not the expected cobol length