XML Header - Special Characters

Hello. I’m trying to product the following output: (the values aren’t important…the format is)

<ORIG_AUTH_GUID>TESTBRIC2</ORIG_AUTH_GUID>

165.00

<CARD_ENT_METH>Z</CARD_ENT_METH>

<TRAN_TYPE>CCM1</TRAN_TYPE>

<USER_DATA_1>10008930</USER_DATA_1>

<USER_DATA_2>360 VT Tech Ltd</USER_DATA_2>

<INVOICE_NBR>0102IN000007561</INVOICE_NBR>

<CURRENCY_CODE>978</CURRENCY_CODE>

<TRAN_NBR>1</TRAN_NBR>

The problem is in the and tag part...I cannot get it to allow special characters such as the = or quotes between the < and >... I get:

ERROR: The ‘=’ character, hexadecimal value 0x3D, cannot be included in a name.

Here’s my code:

//concatenate line in XML

fileStr =

“file_id=’”+BatchLogTable.fileid+"’ file_total_amount=’"+num2str(myView.AmountCur,0,2,1,0)+"’ file_tran_count=’"+num2str(myView.RecId,0,0,1,0)+"’";

//concatenate line in XML

batchStr =

“batch_id=’”+currDateStr+"‘cust_nbr=’"+dialogCustNum.value()+"‘merch_nbr=’"+dialogMerchNum.value()+"‘dba_nbr=’"+dialogDBANum.value()+"‘terminal_nbr=’"+dialogTermNum.value()+"‘batch_total_amount=’"+num2str(myView.AmountCur,0,2,1,0)+"‘batch_tran_count=’"+num2str(myView.RecId,0,0,1,0)+"’";

//start creation of XML

doc = XmlDocument::newBlank();

nodeFile = doc.createElement(fileStr

);

doc.appendChild(nodeFile);

nodeBatch = doc.createElement(batchStr);

nodeFile.appendChild(nodeBatch);

Everything after this works, so I didn’t include it. If I remove the single quotes and equal signs from the fileStr and batchStr values, I don’t get the error, but I need those so they can be read on the other end.

Any ideas?

thanks!

It’s because createElement() expects a name of an element, therefore the whole string you provided is interpreted as element’s name and characters as ‘=’ are not allowed there. But the name is wrong. The name is “FILE”, not *"*FILE file_id=“20140318A” file_total_amount=“1945.00” file_tran_count=“10” (for example). Use setAttribute() to add attributes to an element.

Thanks for your help…I actually came to same conclusion after posting this. This was the first time I dealt with trying to create an XML, so I had trouble with my syntax when trying to use the setAttribute(), but I finally got it figured out. I was over-thinking it.

Here’s a simplified sample of my code for anyone else looking for same solution, since there’s not a lot of examples I found out there…

Create a new class and create this as the main method:

public static void main(Args _args)

{

//declare variables

XmlDocument doc;

XmlElement nodeFile;

XmlElement nodeBatch;

XmlElement nodeTable;

XmlElement nodeAccount;

XmlElement nodeAmount;

str myFileName;

CustTransView myView;

date currDate;

//define file path and name

#define.filename(@’\serverName\folder\FileName’)

//data used for my example

currDate = systemDateGet();

select sum(AmountCur),count(recid) from myView where myView.TransDate == currDate;

myFileName = ‘Sample.xml’;

//start creation of XML

doc = XmlDocument::newBlank();

//FILE Node

nodeFile = doc.createElement(‘File’);

nodeFile.setAttribute(‘file_id’,myFileName);

//will display Total AmountCur and Total # of records from myView in node (from previous sql select)

nodeFile.setAttribute(‘file_total_amount’,num2str(myView.AmountCur,0,2,1,0));

nodeFile.setAttribute(‘file_tran_count’,num2str(myView.RecId,0,0,1,0));

doc.appendChild(nodeFile);

//BATCH Node

nodeBatch = doc.createElement(‘Batch’);

//uses date as the batch id to display in node

nodeBatch.setAttribute(‘batch_id’,date2str(currDate,321,2,0,2,0,4,DateFlags::None));

nodeFile.appendChild(nodeBatch);

//DETAIL Node

While select myView where myView.TransDate == currDate

{

//display Account and Amount for each record in the node

nodeTable = doc.createElement(“DETAIL”);

nodeBatch.appendChild(nodeTable);

nodeAccount = doc.createElement(“Account_Num”);

nodeAccount.appendChild(doc.createTextNode(myView.accountNum));

nodeTable.appendChild(nodeAccount);

nodeAmount = doc.createElement(“AMOUNT”);

nodeAmount.appendChild(doc.createTextNode(num2str(myView.AmountCur,0,2,1,0)));

nodeTable.appendChild(nodeAmount);

}

//save XML to path

doc.save(#filename);

//display completion message

info(strFmt(“File %1 created.”, #filename));

}