Grokking XML - CSDAinc.com
Transcription
Grokking XML - CSDAinc.com
Grokking XML Lab David Andruchuk Sr. Architect Computer Systems Design Associates, Inc. November 14, 2008 What can i do…..i can do XML Grokking XML Grok To understand so thoroughly that the observer becomes a part of the observed—to merge, blend, intermarry, lose identity in group experience. It means almost everything that we mean by religion, philosophy, and science—and it means as little to us (because we are from Earth) as color means to a blind man. Grokking XML Table of Contents • Introduction Introduction to XML • Programmatic Approach Using IBM RAD & XML Using Excel & XML Using SQL for XML compose Using RPG for XML decompose Grokking XML • XML has become a standard for cross-platform communications • XML has or will impact your business if you continue to be in business • XML has gained acceptance as a preferred data exchange technology in spite of the limitations because of the flexibility and human readable formatting Grokking XML Introduction to XML Grokking XML What you need to get started: XML Editing tool(s) WDSC, Rational Application Developer (RAD) (IBM) XMLSpy (Altova) Stylus Studio (Progress Software Corporation) Microsoft Office 2003 or higher XML Reference(s) WDSC Step by Step (MC Press) XML for eServer i5 and iSeries (MC Press) Qshell for iSeries (MC Press) XSLT 2nd Edition (WROX) W3Schools Online Web Tutorials (www.w3schools.org) Grokking XML What is XML? – XML is short for Extensible Markup Language Pros of XML? – XML is flexible – XML is self documenting – XML can replace or extend legacy systems Cons of XML? – XML is verbose – XML can significantly increases the size of your data – XML works best with hierarchal data in a relational data base Grokking XML There are three main components of XML • XML Document – Document containing data • XML Document Type Definition (DTD) – Document that defines the document structure of the XML Document • XML Schema Definition (XSD) – Document that defines the document structure of the XML Document in an XML-based alternative to DTD Grokking XML Structure of XML documents • An XML document has a tree-like structure that is hierarchical • The document must contain one and only one root element • An element is the parent of all the elements it contains • The elements that are inside a parent element are called its children • Similarly, the elements that have the same parent element are called siblings Grokking XML Grokking XML Grokking XML Structure of DTD documents Contains the list of tags which are allowed within the XML document and their types and attributes Defines how elements relate to one another within the document's tree structure and specifies which attributes may be used with which elements Grokking XML Grokking XML Structure of XSD documents • Contains the legal building blocks of an XML document similar to DTD • Defines how elements relate to one another within the document's tree structure and specifies which attributes may be used with which elements • Schema supports all data types used in most programming languages such as string, decimal, integer, Boolean, date and time • Supports complexType elements that allows you to define an element type that can consist of sub-elements Grokking XML Grokking XML Programmatic Approach Grokking XML RAD & XML This product provides a comprehensive visual Extensible Markup Language (XML) development environment. Tools help you create, import, edit, validate, and generate XML, XSL, DTDs, and XML schemas (XSD) files. Mapping tools help you establish relationships between XML files and between XML files and relational database tables. (C) Copyright IBM Corporation 1992, 2005. All Rights Reserved. Grokking XML Opening an XML document with RAD XML document Right click > open with > rationalsdp (eclipse on RAD 7.0) Grokking XML Create workspace Enter C:\Grokking_XML\RAD_workspace and leave Use this as the default….unchecked! Grokking XML Open a Perspective Window>Open Perspective>Other>Resource Grokking XML Create new project In Navigator pane Right Click>New>Project>Simple (General on RAD7.0)>Next Grokking XML Create new project (continued) Enter Project Name Grokking_XML and click Finish Grokking XML Import employee XML document to project Again in Navigator pane Right click Grokking_XML project>Import>(General on RAD7.0) File System>Next>Browse>Select XML file>Finish Grokking XML Create generic schema (XSD) to enable XML Development Again in Navigator pane Right click employee.XML document>New>Other>Show All Wizards.>XML>XML Schema>Next> Check Always enable capabilities…>OK Grokking XML Create generic schema (XSD) (continued) Enter or select the parent folder Grokking_XML>Leave file name NewXMLSchema.xsd>Finish Grokking XML Our new generic schema in project The Navigator pane is redisplayed showing the new XSD Grokking XML Create employee schema (XSD) With XML Development now enabled for our project, we can generate the Employee XSD from our existing employee XML document. In the Navigator pane Right click the employee.xml document>Generate (NOT in RAD7.0) >XML Schema… Grokking XML Create employee schema (XSD) (continued) Accept the File name as is and click Finish Grokking XML Create employee schema (XSD) (continued) The XML Schema generated window is now displayed, click OK Grokking XML Delete generic schema (XSD) The generic XML Schema generated to enable XML can now be deleted In the Navigator pane Right click the NewXMLSchema.xsd document>Delete>Click Yes… Grokking XML Create employee document type definition (DTD) With the Employee XSD generated, we can generate the Employee DTD from our existing XSD In the Navigator pane Right click the employee.xsd document>Generate (NOT in RAD7.0) >DTD… Grokking XML Create employee document type definition (DTD) (continued) Accept the File name as is and click Finish Grokking XML Create employee document type definition (DTD) (continued) The DTD generated window is now displayed, click OK Grokking XML Exit RAD Click File>Exit and if any resources need to be saved prior to exit, Click OK Grokking XML RAD & XML recap What we accomplished: Created a project to Grok XML Imported our sample XML document Generated an XSD & DTD from the XML document What we could still accomplish: Assign our XSD & DTD to our XML document for validation Edit our XML, XSD & DTD documents in RAD to verify that the XSD & DTD generated are accurate in regards to Repeating elements are correctly defined Validations required for elements/attributes Required elements are defined Save our document(s) to a mapped IFS folder on our System i Grokking XML Excel & XML New tools in Microsoft Office Excel 2003 make it easier to work with Extensible Markup Language (XML). Learn how to use these tools to create XML lists and XML maps. XML lists have the same look and feel as regular Excel lists, but they take their data from XML files. XML maps are sets of links between the cells in a worksheet and the elements in an XML schema. You can import data into the map, modify the data, and then save the changed data or export it for others to use. Grokking XML Create an Excel document In our Grokking_XML\RAD_workspace\Grokking_XML folder Right click>New>Microsoft Excel Worksheet Grokking XML Create an Excel document (continued) Now name it employee.xls Grokking XML Create an Excel document (continued) Now double click to open it Grokking XML Importing XML into Excel is easy There are two ways we can import our XML data into our spreadsheet: with or without an existing Schema (XSD). In this example, we will simply import the XML data and let Excel generate a schema template for us. Click Data>Import External Data>Import Data Grokking XML Importing XML w/o a schema Now select the source of our import, our employee.xml document and click Open Grokking XML Importing XML w/o a schema (continued) Click OK when the Error in XML window is display to instruct Excel to create a schema for us and apply to our worksheet Grokking XML Importing XML w/o a schema (continued) Except the defaults on the Import Data window and click OK Grokking XML Importing XML w/o a schema (continued) We have now successfully imported an XML document into an Excel worksheet Grokking XML Importing XML w/o a schema (continued) Notice the bold column names. This indicates that the schema mappings apply to the columns so that an XML document could be generated from this worksheet. Let’s display the underlying XML of this worksheet. Click Data>XML>XML Source Grokking XML Importing XML w/o a schema (continued) In the XML Source window displayed to the right of our worksheet, the Excel generated Employees_Map schema(XSD) is displayed Grokking XML Importing XML w/o a schema (continued) If you click the Tips for mapping XML link, a help panel is displayed defining more about the workings of Excel & XML Grokking XML Adding an XML Map using a schema (XSD) In this example, we will use an existing schema (XSD) as the template. Click Data>XML>XML Source… Grokking XML Adding an XML Map using a schema (XSD) (continued) Click XML Maps>Add…>employee.xsd>Open Grokking XML Adding an XML Map using a schema (XSD) (continued) Click Employees>OK in the Multiple Roots windows since Employees, not Employee is our root Grokking XML Adding an XML Map using a schema (XSD) (continued) Click OK in the XML Maps windows Grokking XML Adding an XML Map using a schema (XSD) (continued) In XML Source window, right click the Employees root>Map element…>Click OK in Map XML Element window Grokking XML Our worksheet with the Employee Map Notice the markings on the Employee_Map showing that each element is now mapped to a worksheet column and that the columns are now in bold and the XML containment box Grokking XML Importing the Employee XML document Click Data>XML>Import>employee.xml>Import Grokking XML Our worksheet with the Employee XML data Notice the markings on the worksheet that surrounds the column data. The blue box designates the containment of the XML Source template. Grokking XML Adding data to our worksheet Right click a row>Insert>Row>key data to the row to populate each column as you would in any worksheet Grokking XML Saving/Exporting our worksheet To save the worksheet as XML data you can either: Click File>Save As…>Save as type: XML Data(*.xml)>name file>Save Click Data>XML>Export…>name file>Export Grokking XML Excel & XML recap What we accomplished: Imported our employee XML document and generated an XML Map Applied an existing XSD schema as an XML Map to our worksheet Imported our employee XML data Added a row to our worksheet Saved/Exported our XML data What we could still accomplish: Perform editing of column(s) in Excel to validate input Save our document(s) to a mapped IFS folder on our System i Grokking XML SQL & XML composition Using the System i SQL engine we can create an SQL Procedure that will generate the Employee XML document used in our lab so that we can understand the simplicity of XML composition without any special coding requirements or tools on our existing System i. Grokking XML Coding the SQL stored procedure Grokking XML Coding the SQL stored procedure (continued) Grokking XML Coding the SQL stored procedure (continued) Grokking XML Coding the SQL stored procedure (continued) Grokking XML Coding the SQL stored procedure (continued) Grokking XML Creating the SQL stored procedure Grokking XML Executing the SQL stored procedure Grokking XML Some considerations for SQL Composition SQL Stored Procedures contain two parts: a catalog entry and a program object. These two items can exist separate of each other but only work when both parts are there at run time and are not as easy to implement as using a SAVOBJ & RSTOBJ. Work around for this limitation: - Run the SQL Create Procedure statement on each box they are to be used on to create the catalog entry and program object Grokking XML SQL & XML recap What we accomplished: Created a source member that contained our SQL source Created the SQL procedure using RUNSQLSTM Executed the SQL procedure to generate the employee XML data What we could still accomplish: Add variables to the SQL source for our procedure for flexibility Add maintenance coding to DROP the procedure before create and the Outfile table once complete Grokking XML RPG & XML decomposition The ILE RPG Compiler as of the V5R4 release of i5/OS has new native operations codes and built in functions that facilitate decomposition of an XML document into data structures defined in the program using a non-validating parser. For more information on the XML parser used by ILE RPG, please refer to Chapter 11 in the WebSphere Development Studio ILE RPG Programmer’s Guide, SC09-2507. The advantage of using the new operation codes and built-in functions added to RPG is that once the hierarchy of the XML document is replicated using data structures in our RPG program code, the decomposition of the XML data and population of data structure fields is done automatically and efficiently without the developer having to manipulate storage to get the XML data in to workable form. The complexity has been removed from the XML decomposition process and the developer now can just concentrate on business logic required to implement the XML data. No pointers, API interfaces, buffer manipulation, just straight forward RPG coding. Grokking XML Coding a CMD interface Grokking XML Coding a PNLGRP for the CMD interface Grokking XML Coding a PNLGRP for the CMD interface (continued) Grokking XML Coding a PNLGRP for the CMD interface (continued) Grokking XML Coding a CLLE validation module Grokking XML Coding a CLLE validation module (continued) Grokking XML Coding a CLLE validation module (continued) Grokking XML Coding a CLLE validation module (continued) Grokking XML Coding the SQLRPGLE module for decomposition Grokking XML Coding the SQLRPGLE module (continued) Grokking XML Coding the SQLRPGLE module (continued) Grokking XML Coding the SQLRPGLE module (continued) Grokking XML Coding the SQLRPGLE module (continued) Grokking XML Coding the SQLRPGLE module (continued) Grokking XML Coding the SQLRPGLE module (continued) Grokking XML Coding the SQLRPGLE module (continued) Grokking XML Coding the SQLRPGLE module (continued) Grokking XML Executing the XML decomposition Grokking XML Some considerations for RPG Decomposition Use of the Java Virtual Machine (JVM) on a System i requires some simple tuning to optimize performance. Since we use the JVM to run our Transform class it is important to note the memory pool that your JVM executes in be set appropriately to improve performance. XML-INTO operation cannot handle empty elements or attributes that contain zero length numeric, date, time or timestamp fields during decomposition. Work around for this limitation: - Use a stylesheet (XSL) as we did to remove the empty limitations noted above Grokking XML Some considerations for RPG Decomposition (continued) The RPG compiler limits character variables to 65535 characters in length. Data Structures, being considered character fields, are subject to this length limitation as well. You must be aware when replicating the XML tree using data structures, especially data structures that are dimensional, that the total size of the data structure tree does not exceed this limit as well. Work around for this limitation: - break apart the data structures without nesting them in side each other and use multiple XML-INTO operations to decompose specifying the specific path of the XML document that matches the data structure names. Grokking XML Questions?