Sample PeopleCode to Create XML file – BI Publisher Report

 In Bi Publisher there are three methods to create XML files as data source.

  1. PS Query Based
  2. Row-set Based
  3. Manually Creating XML file

To create XML files through PS Query and Row-set methods, need to import delivered App package ”PSXP_XMLGEN

PeopleCode to create XML file through Rowset:

Local Rowset &rsRowset;

Local File &fXMLFile, &fXSDFile;

Local string &strXSDFileName, &strXMLData, &strXMLSchema, &strPSHome;

Local PSXP_XMLGEN:RowSetDS &objRowsetDS = create PSXP_XMLGEN:RowSetDS();

&strFileName = &filename;

&rsRowset = &rs;

&strXMLFileName = %This.XMLFilePath | &strFileName | “.xml”;

&strXSDFileName = %This.XMLFilePath | &strFileName | “.xsd”;

/*******************Create XML Schema File *****************/

&strXMLSchema = &objRowsetDS.getXSDSchema(&rsRowset);

&fXSDFile = GetFile(&strXSDFileName, “w”, %FilePath_Absolute);

&fXSDFile.WriteLine(&strXMLSchema);

&fXSDFile.Close();

/*******************Create XML File *****************/

&strXMLData = &objRowsetDS.getXMLData(&rsRowset, &strXSDFileName);

&fXMLFile = GetFile(&strXMLFileName, “w”, %FilePath_Absolute);

&fXMLFile.WriteLine(&strXMLData);

&fXMLFile.Close();

PeopleCode to create XML file manually:

Local XmlDoc &inXMLDoc;

Local XmlNode &xRootNode, &xMonthNode, &xAmountNode, &xSummaryNode;

&inXMLDoc = CreateXmlDoc(“<?xml version=’1.0′, encoding=’UTF-8’STANDALONE=’yes’?><ROOT/>”);

&inXMLDoc.DocumentElement.AddAttribute(“xmlns:xsi”,

“http://www.w3.org/2001/XMLSchema-instance&#8221;);

&xRootNode = &inXMLDoc.DocumentElement.AddElement(“MAIN”);

&xSummaryNode = &xRootNode.AddElement(“SUMMARY”);

&xMonthNode1 = &xSummaryNode.AddElement(“JAN”);

&xMonthNode1.NodeValue = String(&nbrAmount);

&xMonthNode2 = &xSummaryNode.AddElement(“DEC”);

&xMonthNode2.NodeValue = String(&nbrAmount);

/*To add Child Nodes dynamically either through For loop or While Loop*/

&sqlSummary=CreateSQL(“SELECT MONTH,AMOUNT FROM PS_TEST_TBL”);

While &sqlSummary.Fetch(&nbrMonth, &nbrAmount)

&xDataNode = &xRootNode.AddElement(“SUMMARYDATA”);

&xMonthNode = &xDataNode.AddElement(“MONTH”);

&xMonthNode.NodeValue = &strMonth;

&xAmountNode = &xDataNode.AddElement(“AMOUNT”);

&xAmountNode.NodeValue = String(&nbrAmount);

End-While;

&strXMLFileName = %This.FilePath | &strFileName | “.xml”;

&fXMLFile = GetFile(&strXMLFileName, “w”, %FilePath_Absolute);

If &fXMLFile.IsOpen Then

&strXMLData = &inXMLDoc.GenFormattedXmlString();

&fXMLFile.WriteLine(&strXMLData);

&fXMLFile.Close();

Else

Error (“Temporary XML file creation failed. Please contact your System Administrator”);

End-If;

PeopleCode to display a report in PDF format:

Import PSXP_RPTDEFNMANAGER:*;

Local string &strRptDefnId, &strTemplateId, &strLanguageCd, &strOutputfmt, &strOutDest;

Local date &dtAsofdate;

Local Rowset &rsOutput;

Local PSXP_RPTDEFNMANAGER:ReportDefn &objReportdefn;

&strFileName = “HSampleExample”;

&strRptDefnId =” HSAMPLE”;;

&strTemplateId = “HMAHANTA_1”;

&strOutputfmt = “PDF”;

&strLanguageCd = “ENG”;

&dtAsofdate = %Date;

&strXMLFileName = %This.XMLFilePath | &strFileName | “.xml”;

&strOutDest = %This.XMLFilePath;

&objReportdefn = create PSXP_RPTDEFNMANAGER:ReportDefn(&strRptDefnId);

&objReportdefn.Get();

&objReportdefn.SetRuntimeDataXMLFile(&strXMLFileName);

/*To store the PDF report with the given name, provide the following properties to the report definition object*/

&objReportdefn.OutDestination = &strOutDest;

&objReportdefn.ReportFileName = &strFileName;

/*Generate and Display PDF Report*/

&objReportdefn.ProcessReport(&strTemplateId, &strLanguageCd, &dtAsofdate, &strOutputfmt); CommitWork();

/*To display the report on line*/

&objReportdefn.DisplayOutput();

/*To print the report*/

&objReportdefn.PrintReport();

/*PeopleCode to delete the XML file created after displaying the report */

try

Local File &fTempFile = GetFile(&strXMLFileName, “W”, %FilePath_Absolute);

&fTempFile.Delete();

catch Exception &ExCleanup

Warning (“Clean-Up of temporary XML File failed with Exception:” | &ExCleanup.ToString());

end-try;

Note: For printing .XLS/.XLSX format output reports in process monitor logs/report repository in BI Publisher reports , We need to add below piece of code in your App Engine.

SELECT PRCSOUTPUTDIR FROM PSPRCSPARMS WHERE PRCSINSTANCE = %ProcessInstance;

Next, Assign the value of PRCSOUTPUTDIR to Report definition object.

&oRptDefn = create PSXP_RPTDEFNMANAGER:ReportDefn(“Report_ID”);

&oRptDefn.Get();

&oRptDefn.OutDestination = “Value from PRCSOUTPUTDIR”;

No comments:

Post a Comment

PeopleCode to retrieve Google map between two addresses

  PeopleCode Example: /* Define constants for the API request */ Local string &origin = "123 Main St, Anytown, USA";   /* ...