Skip to main content

A quick way of dumping records of a table in an xml-file.

A quick way of of dumping table records into a xml-file is to use the kernel method xml, which is present on all instances of a tablebuffer. However, the xml produced by this method is NOT well-formed.

Three problems exists:
  • No header info for the xml-document is written
  • No root node is written
  • The data within tags are not html escaped
When the xml-method is called a call to the GLOBAL class method XMLString method is made.
With a little adjustment to this method we can make the XML data output wellformed.

But first you must add this method to the GLOBAL class:

public static str escapeHTMLChars(str _in)
{
    int x;
    str out;
    for(x=1;x<=strlen(_in);x++)
    {
        if ((char2num(_in,x) < 32) && (char2num(_in,x) > 126))
        {
            out += '&#'+num2str(char2num(_in,x),0,0,0,0)+';';
        }
        else
        {
            out += substr(_in,x,1);
        }
    }
    return out;
}

And now back to the global::XMLString method.
The last code block in the method is a big switch case construct handling all data types in x++. In the string handler section:

case Types::RSTRING:
case Types::VARSTRING:
case Types::STRING:     r += legalXMLString(value);
                        break;


Insert this code just before the break-statement:

r = global::escapeHTMLChars(r);

To call the method mentioned above.
So the code will look like this:

case Types::RSTRING:
case Types::VARSTRING:
case Types::STRING:
                    r += legalXMLString(value);
                    r = global::escapeHTMLChars(r);
                    break;

Now it is possible to write code that:
  1. Writes an xml-header.
  2. Writes a root node tag.
  3. Iterates a table and calls the xml-method on the tablenbuffer. 
  4. Write the end for the root node tag.
static void Job45(Args _args)
{
    query q;
    QueryRun qr;
    AsciiIo f;

    f = new AsciiIo('\\\\axdev2\\e$\\CIT Files\\jas\\mcfly.xml','w');
    if (!f)
        throw error("Argh ! File can not be created.");
    f.writeRaw('');
    // Write root node
    f.writeRaw('');
    q = new Query();
    // Get customer no. 0215

    q.addDataSource(tablenum(CustTable)).addRange(fieldnum(CustTable,AccountNum)).value('0215');
    qr = new QueryRun(q);
    while (qr.next()) {
        f.write(qr.get(tablenum(CustTable)).xml());
    }
    f.writeRaw('');
    f = null;
    // Show xml-file in browser
    winapi::shellExecute('\\\\axdev2\\e$\\CIT Files\\jas\\mcfly.xml');
}

Comments

  1. Minor syntax error it looks like:
    if ((char2num(_in,x) < 32) (char2num(_in,x) > 126))

    should be, I think:
    if ((char2num(_in,x) < 32) && (char2num(_in,x) > 126))

    ReplyDelete
  2. Darn html-encoding. :)
    Corrected. Thanx for the input.

    ReplyDelete

Post a Comment

Popular posts from this blog

Suppressing the infolog

Supressing the infolog is often useful in D365FO when augmenting code. When augmenting code using COC (Chain Of Command), you can have new code run either before or after the code you are augmenting. This means that any infolog-messages that the standard application code does, will be shown to the user, even if your augmentation supports a scenario where there must be no infolog-messages. How do you avoid the standard application infolog-messages ? To the rescue comes temporary supression of the infolog. The suppression consists of: 1) Saving the current infologLevel 2) Setting the infologLevel to SysInfologLevel::None 3) Run your code 4) Restoring the saved infologLevel to the infolog For example a table could have a validatewrite-method that validates that you are only allowed to use 3 out of 6 options in an enum-field, and you need to allow for a fourth one. Table a - validateWrite method: boolean validateWrite() {     Switch (this.enumField)     {     ...

Dynamics AX 2012 ValidTimeState tables and form changing view from current to all

Valid Time State tables are new i AX 2012 a gives the developer the possibility to easily create tables that hold e.g. current setup data for various purposes, and at the same time keeping a "history" of the changes of the data in the table. For more reading: http://msdn.microsoft.com/en-us/library/gg861781.aspx I was tasked with doing a setup table with rates for calculating Vendor Bonus and I chose to base this a valid time state table. The customer asked for a button on the form, where you maintain the vendor bonus calculation setup data, so you could toggle viewing "Current setup" or "All setup" records (changing the view from actual to all records and vice versa in the form). I found that you can not change the ValidTimeStateAutoQuery property on the form data source in a form at run-time. It simply does not change anything, so I came up with the following solution: A boolean class member in the classdeclation method of the form: boolean ...

Subtle but important difference between _ds.executeQuery() and ds.Research()

This is actually an old entry. Been tumbling with a problem for the last few days. A form in our Dynamics AX module for Preventive Maintenance Control was not behaving. The form has "explicit" filter fields that the user can see without having to activate the form filter (CTRL+F3), for setting up filters most commonly used in an easy way. And this is working ok. However at this customer site, the form has been adjusted so that the user can have the form refreshed automatically periodically, and when the users at the customer site were making use of the "explicit" filter combined with the AX's normal filtering (CTRL+F3), the form simply threw away the normal form filtering. I discovered a subtle but very important difference between writing _ds.executeQuery(); (which was the way our code was doing it) and _ds.Research(); The difference is that _ds.Research() will retain the filter ranges in the forms query as they are right now. _ds.executeQuery() will NOT. It ...