Crystal Reports in the Real WorldLeveraging XML as a Data Source


With the emergence of XML as a data interchange format, many customers wanted to create reports on XML documents. So in Crystal Reports 8.5, a new driver was released that allowed just this scenario. This ODBC driver reads certain types of XML documents. Version XI of Crystal Reports provides the capability to read multiple XML files, most commonly a folder of XML files that have the same schema. When using this driver, you specify either a folder name or a file path to an XML file as described in detail earlier in this chapter. Once connected, XML elements at the first level are represented as fields that you can place on a report.

If you require more flexibility around reading XML files, a good approach to take is to write a COM or Java Data Provider to read the XML. This Data Provider can use one of the many readily available XML parsers to read in the XML and choose exactly what fields to return to Crystal Reports. Listing 15.3 is a sample Visual Basic COM Data Provider that reads in a simple XML file. This method is still used by developers who want to exert very strict control over the XML data that is provided to the report.

Listing 15.3. A COM Data Provider That Reads XML Data

' Loads an XML document with the following structure:     ' <employees>     '   <employee>     '     <name>X</name>     '     <dept>X</dept>     '     <salary>X</salary>     '   </employee>     ' </employees>     Public Function SimpleXML(ByVal fileName As String) As ADODB.Recordset         Dim rs As New ADODB.Recordset     Dim xmlDoc As New MSXML2.DOMDocument     xmlDoc.Load(fileName)     rs.Fields.Append("Name", ADODB.DataTypeEnum.adBSTR)     rs.Fields.Append("Dept", ADODB.DataTypeEnum.adBSTR)     rs.Fields.Append("Salary", ADODB.DataTypeEnum.adCurrency)     rs.Open()     ' Loop through each employee element     Dim employeeNode As MSXML2.IXMLDOMElement     Dim childNode As MSXML2.IXMLDOMElement     For Each employeeNode In xmlDoc.documentElement.childNodes         rs.AddNew()         For Each childNode In employeeNode.childNodes             rs(childNode.nodeName).Value = childNode.Text         Next         rs.Update()     Next     SimpleXML = rs End Function

 




Crystal Reports XI(c) Official Guide
Crystal Reports XI Official Guide
ISBN: 0672329174
EAN: 2147483647
Year: N/A
Pages: 365

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net