• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: DB2 For i XML Composition And The IFS

    June 11, 2018 Michael Sansoterra

    Hey, Mike! Regarding Composing An XML Document From Relational Data, Part 1, I have built an XML document using DB2 and i. When I run the query, I get a worthless result set. How do I use the SQL XML functions to get a usable XML file?

    This question comes from reader RA, and he doesn’t exaggerate. The result set from his XML-based query looks like this:

    ....+....1....+....2....+....3....+....4....+
    ************Beginning of data************** 
    
    XMLDATA 
    -------- 
    #CGULIB#
    
      1 RECORD(S) SELECTED.
    
     ************End of Data********************
    

    Notice that DB2 for i does nothing to make the XML attractive to human eyes as the entire XML document is stored on one line. Since only the XML data is desired, the header and row count information lines should be removed. Fortunately, the grep utility can be used to return just the XML data. The grep utility acts like a filter so it will be used to return line(s) in the file that have a lower-case “xml” value. With the addition of grep, the db2 results will be filtered to include only the XML data line:

    db2 'select * from qgpl.my_schemas' |grep xml >/tmp/schemas.xml
    

    To summarize the behavior of the above qshell statement, the db2 utility runs the query and hands the data over to the grep utility, which returns only lines that contain the value “xml”. Thereafter, the result of the query (after the grep filter) is placed in the IFS file /tmp/schemas.xml. Now when you review the IFS file using dspf ‘/tmp/schemas.xml’, only the desired XML data appears.

    By default, the IFS file on my system was tagged with CCSID 37 (US EBCDIC). If you have an XML declaration (<?xml version=”1.0″ encoding=”UTF-8″?>), such as the one I included in my XML file where UTF-8 is specified, then you will want to change your IFS file’s CCSID to match the declaration. On the IBM i, UTF-8 is specified using CCSID 1208. You can change the CCSID encoding of the file with this CL command:

    CHGATR OBJ('/tmp/schemas.xml') ATR(*CCSID) VALUE(1208)
    

    This CL command can also be run in a qshell script by using the system utility.

    Alternative Approaches

    I tried using CPYTOIMPF to see if I could simplify this mess but unfortunately it doesn’t like to play with tables containing large object data types. If your XML will always be a single-byte character set and less than 32KB, you could cast your XML to VARCHAR and then use CPYTOIMPF:

    CREATE OR REPLACE VIEW QGPL.MY_SCHEMAS AS
    SELECT XMLSERIALIZE(
           XMLELEMENT(NAME "Schemas",
           XMLAGG(XMLELEMENT(NAME "Schema", SCHEMA_NAME)))
           AS VARCHAR(32700) CCSID 1208 INCLUDING
           XMLDECLARATION)  AS XML
      FROM SYSSCHEMAS
    
    CPYTOIMPF FROMFILE(QGPL/MY_SCHEMAS)
              TOSTMF('/tmp/schemas2.xml')
              MBROPT(*REPLACE)
              STMFCCSID(1208)
              RCDDLM(*LF)
              DTAFMT(*DLM)
              STRDLM(*NONE)
    

    This option is a little easier to follow than qshell and even lets you set the CCSID on the IFS file. On IBM i 7.3, it looks like CPYTOIMPF issues a CPF2973 truncation warning even when the data has not reached 32KB.

    One other approach you can take that’s beyond the scope of this tip is to use the CLOB_FILE file reference SQL data type that is used in embedded SQL programming. Using CLOB_FILE will allow you to dump the content of a CLOB variable directly into an IFS file. This is a great feature which you can read about in the Embedded SQL Programming Guide (RPG data structure shown in the link).

    RELATED STORIES

    Composing An XML Document From Relational Data, Part 1

    Embedded SQL Programming Guide

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

    Tags: Tags: 400guru, CL, DB2 for i, FHG, Guru, IBM i, qshell, SQL, XML

    Sponsored by
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

    As I See It: The Hunt For Perfect Storage Profound Logic Shows Off Innovation At PowerUp

    Leave a ReplyCancel reply

TFH Volume: 28 Issue: 40

This Issue Sponsored By

  • Profound Logic Software
  • Software Concepts
  • T.L. Ashford

Table of Contents

  • Guru: DB2 For i XML Composition And The IFS
  • As I See It: The Hunt For Perfect Storage
  • IBM i PTF Guide, Volume 20, Number 22

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Bob Code Assistant Gets Better Understanding Of IBM i Applications
  • IBM i In The Cloud And The Case For Reversing Replication
  • Guru: From Hiking Boots To Long-Term Memory
  • Inside IBM’s New PowerHA Cloud Offering, Full System Manager
  • IBM i PTF Guide, Volume 28, Number 39
  • Power Systems Turns In Another Great Quarter
  • Big Blue Is Right On Schedule With Fall 2026 IBM i Tech Refreshes
  • Guru: Environment Variables – Global Variables Wearing Hiking Boots
  • A New IBM i Entity Takes Shape: Power By Fortra
  • IBM i PTF Guide, Volume 28, Number 38

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle