Wednesday, November 27, 2013

XML FIle output from PeopleCode

Below is sample PeopleCode to export a Record View – PS_BCP1 to a xml file. Highlighted is the Target XML file name and Source table name.

Local XmlDoc &inXMLDoc;
Local XmlNode &rootNode, &childNode1, &childNode2, &textNode2;
Local string &xmlStr;
Local File &xmlFile;
Local number &i;

&xmlFile = GetFile("c:\temp\export_sample1.xml", "W", %FilePath_Absolute);

&inXMLDoc = CreateXmlDoc("");
&rootNode = &inXMLDoc.CreateDocumentElement("root");

Local Record &rec = CreateRecord(Record.BCP1);
Local SQL &sql = CreateSQL("SELECT * FROM PS_BCP1");

While &sql.Fetch(&rec)
   &childNode1 = &rootNode.AddElement(Lower(&rec.Name));
   For &i = 1 To &rec.FieldCount
      &childNode2 = &childNode1.AddElement(Lower(&rec.GetField(&i).Name));
      &textNode2 = &childNode2.AddText(&rec.GetField(&i).Value);
   End-For;
End-While;

&xmlStr = &inXMLDoc.GenFormattedXmlString();

&xmlFile.WriteLine(&xmlStr);


&xmlFile.Close();