Sorry there was a typo in my previous post:
The file reference type is XML_CLOB_FILE (and not XML_FILE)
DCL-S MyXMLFile SQLType(XML_CLOB_FILE);
Mit freundlichen Grüßen / Best regards
Birgitta Hauser
"Shoot for the moon, even if you miss, you'll land among the stars." (Les
Brown)
"If you think education is expensive, try ignorance." (Derek Bok)
"What is worse than training your staff and losing them? Not training them
and keeping them!"
-----Ursprüngliche Nachricht-----
Von: MIDRANGE-L [mailto:midrange-l-bounces@xxxxxxxxxxxx] Im Auftrag von
Birgitta Hauser
Gesendet: Saturday, 23.4 2016 12:39
An: 'Midrange Systems Technical Discussion'
Betreff: AW: Structured XML Generation using SQL
Hi Charles,
The most tricky thing is to generate the SELECT-Statement that produces the
XML document.
But with a couple of CTEs it's no magic (and remains still readable).
Once the SELECT-statement is generated, a simple SELECT INTO into an
XML-File-Reference variable is all what you need.
The XML document is converted per default into UTF-8.
Assumed we need to create the following XML-Document:
AllOrders>
<Order OrderNo="XXX" DeliveryDate="YYYY-MM-DD">
<Customer CustNo="1111">
<CustomerName>First CustomerName</CustomerName>
<CustomerName>Second CustomeName</CustomerName>
<City>CustomerCity</City>
</Customer>
<OrderType>XX </OrderType>
<DeliveryTerms>YY</DeliveryTerms>
</Order>
<Position Pos="PosNo">
<ItemNo>2222</ItemNo>
<ItemDescription>Item Description</ItemDescription>
<Price>1.222</Price>
</Position>
... More Positions
... More Orders
</AllOrders>
This SELECT-Statement generates the XML document:
With OrdSel as -- Select orders for the XML Document
(Select *
from OrderHdrX
Where Deldate between '2015-12-01' and '2015-12-31'),
OrdHdr as -- XML for Order Header and Address information
(Select Company, OrderNo,
XMLElement(Name "Order", XMLAttributes(Trim(OrderNo)
as "OrderNo", Char(DelDate, ISO) as "DeliveryDate"),
XMLElement(Name "Customer",
XMLAttributes(Trim(o.CustNo) as "CustNo"),
XMLForest(CustName1 as "CustomerName",
CustName2 as "CustomerName", City as "City")),
XMLForest(OrderType as "OrderType", DelTerms as
"DeliveryTerms")) OrdHdrXML
from OrdSel o join Addressx a on o.CustNo = a.CustNo),
OrdPos as -- XML for Order Position and Item information
(Select o.Company, o.OrderNo, d.OrderPos,
XMLElement(Name "Position", XMLAttributes(OrderPos
as "Pos"),
XMLForest(Trim(d.ItemNo) as "ItemNo",
Trim(Descript) as "ItemDescription", Price as "Price")) OrdPosXML
from OrdSel o join OrderDetX d on o.company = d.company
and o.OrderNo = d.OrderNo
Join ItemMastX i on d.company = i.Company
and i.ItemNo = d.ItemNo
Where d.Status = 'CP'),
OrdPosAgg as -- Aggregate the XML position information for each Order
(Select Company, OrderNo, XMLAgg(OrdPosXML) AggPosXML
From OrdPos
Group By Company, OrderNo),
OrdPosHdr as -- Join XML Order header and XML Order Position
information for each order
(Select XMLConcat(OrdHdrXML, AggPosXML) as OrdPosHdrXML
from OrdHdr h join OrdPosAgg d on h.Company =
d.Company and h.OrderNo = d.OrderNo
Order By h.Company, h.OrderNo)
-- Final Aggreagate all XML Order Information and add a Root Element Select
XMLDocument(XMLElement(Name "AllOrders", XMLAgg(OrdPosHdrXML))) from
ordPosHdr;;
RPG-Program:
DCL-S MyXMLFile SQLType(XML_File);
Clear MyXMLFile;
MyXMLFile_Name = '/home/Hauser/MyXMLFile.xml'; //IFS file name
MyXMLFile_NL = %Len(%Trim(MyXMLFile_Name)); //Length IFS file name
MyXMLFile_FO = SQFOVR; //Replace if exists
// SELECT-Into-Statement for producing the XML document
With OrdSel ...
Select XMLDocument(....) into :MyxmlFile
From ...;
//XML File is written
Mit freundlichen Grüßen / Best regards
Birgitta Hauser
"Shoot for the moon, even if you miss, you'll land among the stars." (Les
Brown)
"If you think education is expensive, try ignorance." (Derek Bok) "What is
worse than training your staff and losing them? Not training them and
keeping them!"
-----Ursprüngliche Nachricht-----
Von: MIDRANGE-L [mailto:midrange-l-bounces@xxxxxxxxxxxx] Im Auftrag von
Charles Wilt
Gesendet: Friday, 22.4 2016 20:03
An: Midrange Systems Technical Discussion
Betreff: Structured XML Generation using SQL
Ok, so I've never needed to use the new XML functions in DB2.
Until now. I've got a need for a data export that seems like it might be a
good fit for XML...
Basically, I need to dump invoice header & detail. But for each detail
line, there will be an additional sub query or two to provide some extra
info.
Example (manually created so pardon syntax errors)
<Invoice number="12345" date='2016-01-01'>
<line number="1">
<sku>456789</sku>
<upc>12345678901</upc>
<qty>12</qty>
<more_info>
<line>abcd</line>
<line>efgc</line>
</more_info>
</line>
<legal1>invoice legal verbiage 1</legal1>
<legal2>invoice legal verbiage 2</legal2> </Invoice>
I suppose I could just use a set of .CSV files with "foreign keys" but
using XML would allow me to keep everything together.
So my questions to the experts:
Does this seem like a reasonable use of XML? Or am I asking for trouble?
Do you have links to good references for learning to use the XML
functionality in DB2 for i? I've looked at the SQL reference, and while
useful, I'd like something a bit more together and with more advanced
examples of putting it all together.
Lastly, once I build the XML...how do I get it saved into a text file the
IFS?
Thanks!
Charles
--
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing list
To post a message email: MIDRANGE-L@xxxxxxxxxxxx To subscribe, unsubscribe,
or change list options,
visit:
http://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxx Before posting, please take a
moment to review the archives at
http://archive.midrange.com/midrange-l.
Please contact support@xxxxxxxxxxxx for any subscription related questions.
--
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing list
To post a message email: MIDRANGE-L@xxxxxxxxxxxx To subscribe, unsubscribe,
or change list options,
visit:
http://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxx Before posting, please take a
moment to review the archives at
http://archive.midrange.com/midrange-l.
Please contact support@xxxxxxxxxxxx for any subscription related questions.
As an Amazon Associate we earn from qualifying purchases.