Absent Member.
Absent Member.

XML Syntax in Visual Cobol

I have a managed .NET class named Customer and  i need to write a method in order to create an XML file. How can i enable XML syntax in my Class? is there any sample code?

1 Reply
Micro Focus Expert
Micro Focus Expert

RE: XML Syntax in Visual Cobol

 There are a number of different methods that can be used to enable XML file creation in a COBOL program. The methods directly offered by Visual COBOL are documented here:

There are also a set of .NET classes available for XML manipulation.

The COBOL XML Syntax extensions should be used only within a COBOL procedural program that can then be called from within your method. This is because it uses a preprocessor which does not support the class syntax.

A simple example of a program using the COBOL XML syntax is:

      $set preprocess(prexml) endp
       id division.
       program-id.   writexml.
       environment division.
       input-output section.
          select customers assign to "customers.xml"
                      organization is xml
                      document-type is omitted
                      file status is xml-status.

       data division.
       file section.
       xd customers.
       01 customerdb identified by "customerdb"
          count in customerdb-count.
          05 customer-record identified by "customer-record"
             count in customer-record-count.
             10 customer-id pic X(5) identified by "customer-id"
                count in customer-id-count.
             10 customer-name pic X(20) identified by "customer-name"
                count in customer-name-count.
       working-storage section.
       01 xml-status   pic s9(9) value 0.
       procedure division.

           open output customers
           display xml-status
           move "12345" to customer-id
           move "john smith" to customer-name
           write customerdb
           display xml-status
           close customers
           stop run.

If you wish to use the .NET classes directly within your method then an example would be:

      $set ilusing "System.Xml"
       class-id WindowsFormsApplication1.Form1 is partial
                 inherits type System.Windows.Forms.Form.
       working-storage section.

       method-id NEW.
       procedure division.
           invoke self::InitializeComponent
       end method.

       method-id button1_Click final private.
       procedure division using by value sender as object e as type System.EventArgs.
            declare writer as type XmlTextWriter = new XmlTextWriter("product.xml", type System.Text.Encoding::UTF8)
            invoke writer::WriteStartDocument(true)
            set writer::Formatting to type Formatting::Indented
            set writer::Indentation to 2
            invoke writer::WriteStartElement("Table")
            invoke createNode("1", "Product 1", "1000", writer)
            invoke createNode("2", "Product 2", "2000", writer)
            invoke createNode("3", "Product 3", "3000", writer)
            invoke createNode("4", "Product 4", "4000", writer)
            invoke writer::WriteEndElement
            invoke writer::WriteEndDocument
            invoke writer::Close
            invoke type MessageBox::Show("XML File created ! ")
       end method.
       method-id createNode public.
       procedure division using pID as string, pName as string, pPrice as string, writer as type XmlTextWriter.
            invoke writer::WriteStartElement("Product")
            invoke writer::WriteStartElement("Product_id")
            invoke writer::WriteString(pID)
            invoke writer::WriteEndElement
            invoke writer::WriteStartElement("Product_name")
            invoke writer::WriteString(pName)
            invoke writer::WriteEndElement
            invoke writer::WriteStartElement("Product_price")
            invoke writer::WriteString(pPrice)
            invoke writer::WriteEndElement
            invoke writer::WriteEndElement
       end method.
       end class.
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.