Title: DT050 Using XML in COBOL applications
1DT050Using XML in COBOL applications
- Barry Rosetti
- 01 March 04
2What is this presentation
- Overview of XML and how it is used.
- Handling XML in Micro Focus COBOL
- Syntax elements and their usage
- Basic and advanced manipulation
- Handling XML with IBM extensions
- Syntax elements
- Comparison with MF extensions
- Resources, etc
3What is XML?
- A (very) flexible data encoding schema
- An XML document has
- Optionally, an XML version and (explicit)
encoding - Optionally, a document type definition (DTD)
- A hierarchy of tags and values with one root tag
- Optionally, associated with tags attributes and
their values - Optionally, comments and extra info (processing
instr) - A basic XML document
- Tag docs
value
DT050 Barry Rosetti 01 March 04
4How is XML used?
- Extended, leveraged and used in n different ways
- Namespaces, Schemas, XSLT, XPATH, XLINK,
Xanything - As time goes by, n grows (and grows)
- Basic extensions for XML usage
- Namespaces
- Allow complex documents from multiple sources
- Schemas
- Allow rigorous checking of document validity
- Some other extensions push the envelope
- Making XML an awkward programming language
DT050 Barry Rosetti 01 March 04
5How does one handle XML in COBOL?
- Theres nothing new under the sun
- XML Document types have strong similarities to
- COBOL Record Structure
- Pascal Records
- C Structs
- But XML Document are more dynamic
- Unlimited occurrences
- Recursive
DT050 Barry Rosetti 01 March 04
6How does one handle XML in COBOL?
- Use syntax to handle XML documents and schemas
- Define file that represents an XML I/O stream
- Define records that correspond to XML document
structure - Open file, and perform I/O operations on it
- Single-shot or dynamic document manipulation
- Use IBMs XML parse syntax (mainframe)
- With a fixed buffer, start a SORT style input
procedure - Retrieve document tag, attribute and value
at-a-time - Low-level parsing of document
DT050 Barry Rosetti 01 March 04
7MF Syntax - SELECT
- SELECT statement is the foundation
- Just like COBOL File I/O
- Assign a COBOL name to the file
- Assigns a source or sink for the I/O stream
- Describes what the file is all about
- Organization type for validation
- Tells you where you get the file status
- SELECT xml-bookdb ASSIGN "cbookrw.xml
- ORGANIZATION IS xml
- DOCUMENT-TYPE IS "bookdb.xsd"
- FILE STATUS IS xml-bookdb-status.
DT050 Barry Rosetti 01 March 04
8MF Syntax - SELECT
- ASSIGN is source or sink
- Can be a literal or variable file - c\foo or
fname-data-item - Can be a memory buffer address of mybuff
- Can be a pointer to a buffer ptr-to-buf length
is buf-len - DOCUMENT-TYPE is Schema (not DTD!)
- Can be a literal or variable xsd foo.xsd or
xsd-data-item - On input is not validated (will be optional in
future) - On output decorates the root tag with attributes
- xmlnsxsi"http//www.w3.org/2001/XMLSchema-instan
ce" xsischemaLocationfoo.xsd - FILE STATUS is PIC S9(9), values 0 are success
- You must declare in your working storage section
DT050 Barry Rosetti 01 March 04
9MF Syntax - XD
- XD and all of its records
- The association between XML and COBOL records
- The following
- xd xml-bookdb.
- 01 xml-bookdb-query identified by
"query. - 05 xml-bq-author pic x(40) identified by
"author". - 05 xml-bq-title pic x(40) identified by
"title". - represents an XML document that has structure
-
- Author Name
- Book Title
-
DT050 Barry Rosetti 01 March 04
10MF Syntax - XD
- Lots and lots of things you can do in an XD
record - IDENTIFIED BY either literal or variable
- identifies tag this item will produce or receive
- IS ATTRIBUTE
- specifies that this isnt a tag, but is a
contained attribute - COUNT IN implicitly defined data item
- specifies occurences read or written
- NAMESPACE IN either literal or variable
- allows qualification of namespace
- IS PROCESSING-INSTRUCTION
- allows reading or writing of items
DT050 Barry Rosetti 01 March 04
11MF Syntax - XD
- Worthwhile discussing variable tag/attr/ns names
- 01 foo identified by foo-tag-name namespace is
foo-ns-name. - 05 foo-tag-name pic x(80).
- 05 foo-ns-name pic x(80).
- 05 foo-value pic x(80).
- 05 foo-attr identified by foo-attr-name
- is attribute.
- 10 foo-attr-name pic x(80).
- 10 foo-attr-value pic x(80).
- Variable tags are directly contained in group
DT050 Barry Rosetti 01 March 04
12MF Syntax Basic Verbs
- OPEN inputoutputi-o file-name
- surprise, surprise, surprise
- READ file-name
- Read entire XML document
- WRITE file-record
- Write entire XML document
- CLOSE file-name
- more surprises
DT050 Barry Rosetti 01 March 04
13MF Syntax Extended Verbs
- These are where the power is
- When an XML document is read
- Internal representation of document created
- Record populated as possible
- When an XML document is written
- Internal representation of document is written
- In basic WRITE, from record to internal to output
- In extended WRITE, from record to internal
- Final WRITE goes from internal to output
DT050 Barry Rosetti 01 March 04
14MF Syntax Extended Verbs
- READ, WRITE, REWRITE, DELETE, START
- All have KEY IS phrase
- KEY IS an IDENTIFIED BY item
- With every IDENTIFIED BY item
- Corresponds to a node in internal
representation - There is a current node in internal
representation - Operation happens wrt this current node
- Operation then updates this current node
DT050 Barry Rosetti 01 March 04
15MF Syntax Extended Verbs example
- 01 foo-record identified by tag.
- 05 foo-attr identified by foo-attr-name
- is attribute.
- 10 foo-attr-name pic x(80).
- 10 foo-attr-value pic x(80).
- OPEN foo-file
- READ foo-file populate internal representation
- START foo-file KEY IS foo-attr INDEX IS 2
start at second - READ foo-file NEXT KEY IS foo-attr
get name val - DISPLAY foo-attr-name foo-attr-value ---
cool!!
DT050 Barry Rosetti 01 March 04
16MF Syntax Extended Verbs example
- Given input of
- /
- Will display
- attr2nameattr2value --- cool!!
- Same sort of thing works on OUTPUT and I/O
- Dynamically move through internal representation
- And modify as you go
- And a final write will create the document
DT050 Barry Rosetti 01 March 04
17MF Syntax Extra bits
- WRITE KEY IS PROCESSING-INSTRUCTION
- Allows dynamic insertion of a processing
instruction, eg -
- READ Pis with IS PROCESSING-INSTRUCTION in record
- WRITE KEY IS PLAIN-TEXT
- Allows dynamic insertion of plain text, eg
- Content-type text/xml
- These work outside of document body
- and also inside from position of last operation
DT050 Barry Rosetti 01 March 04
18MF Syntax Things to know
- Syntax implemented as preprocessor (for now)
- set preprocess(prexml) endp
- If error in XML source code
- Preprocessor will abandon source substitution
and - Checker will give a lot (lot) more errors
- Always (always) use WARN directive
- set preprocess(prexml) WARN endp
- set preprocess(prexml) WARN OUT(ppcode.cbl) endp
DT050 Barry Rosetti 01 March 04
19MF Syntax Interesting example
- Parse an arbitrary XML document
- Allowing maximum depth of 1 nested tags (for
simplicity) - Replace all attribute values yes with no
- Write document
- Watch it go
DT050 Barry Rosetti 01 March 04
20IBM Syntax XML Parse
- Given a buffer, parse it a bit at a time
- xml parse buff processing procedure is routine
- goback.
- routine section. called n times, where n is
large - Handle xml-events
- exit section.
- Special registers for use in routine
- XML-EVENT name
- XML-TEXT
- XML-EXCEPTION
DT050 Barry Rosetti 01 March 04
21IBM Syntax Interesting example
- Parse an XML document for a sandwich
-
-
-
-
-
- Ham amp turkey
- Cheese,lettuce,tomato,etc.
- element!
- 4.99
- 0.10
-
- Note PIs, entity references, CDATA
DT050 Barry Rosetti 01 March 04
22IBM Syntax Interesting example
- Notes
- Every last bit of input data is reported
- Content for item arrives in drips and drabs,
- building entire data is a bit tough
- Manual conversion of data into numeric formats
- Hierarchy is difficult to handle
- You have to be very familiar with XML to use
- Hmmm thats a lot of code isnt it?
- What does it look like in MF Syntax?
DT050 Barry Rosetti 01 March 04
23General Things to know
- XML Documents are default encoded in ASCII
- You can modify this with PI
- Example with MF Syntax
- Shift-jis handling with READ/WRITE
- Example with XML PARSE
- Shift-jis handling with XML-PARSE
- NetX and ServerX have CBL2XML
- Automatic conversion from XSD to COBOL record
- Automatic conversion from COBOL record to XSD
- Validate XML streams
DT050 Barry Rosetti 01 March 04
24Resources
- Reading list and Web links
- Really good XML links
- The manual!
- http//www.xml.com/axml/axml.html
- http//www.rpbourret.com/xml/XMLURLs.htm
- IBM XML extensions
- IBM's Enterprise COBOL for z/OS and OS/390 -
Programming Guide - IBM's Enterprise COBOL for z/OS and OS/390 -
Language Reference - Contact information
- Barry.Rosetti_at_microfocus.com
25Questions?