XMLReader.g

XMLReader.g — reads an XML file from a URL.

Code

[Note]

The script allows you to process the data you receive in any way you like, and gives an example of printing the data with the Gamma princ function. If instead you would like to write the data to a point in the DataHub instance, you can replace this code:

with point in model._xml_children do
{
    princ (point.name, " = ", point.value, "\n");
}

with this

with point in model._xml_children do
{
    datahub_write (string("domain_name:"
                   point.name), point.value);
}

where domain is the name of the domain in which you want the point to be created. Be sure to include the colon ( : ) at the end of the domain name, and make sure there are no other colons in the point name. For more information, please refer to the Data Domains section of the DataHub manual, and the datahub_write function in this manual.

[Note]

The code for this and other example scripts can be found in the DataHub distribution archive, typically at this location:

C:\Program Files\Cogent\Cogent DataHub\scripts\

Please refer to Section 3.1, “How to Run a Script” for more information on using scripts.

/*
This script reads an XML file from a URL and parses it into a class
hierarchy that can be used to run further scripts or to populate a
DataHub instance.

The example in this case requires that DataPid is running and that
the DataHub Web Server is turned on, as the script is simply reading
some data point values from the DataHub Web Server in XML format.
*/

require ("Application");

/* Get the Gamma library functions and methods for XML parsing and
   wget command-line tool */
require ("XMLSupport");
require ("WgetSupport");

class XMLReader Application
{
  //Note: The following line is wrapped for documenation formatting.
  //      You will need to restore it to aingle line to run the
  //      program.
    DocumentUrl = "http://localhost/points?name=DataPid:PID1.Mv|
                   DataPid:PID1.Pv|DataPid:PID1.Sp";
    TickSeconds = 5;
}

method XMLReader.loadXml ()
{
    // Call wget asynchronously.  Protect against the possibility
    // that the script is stopped while a wget command is still
    // outstanding.
    Wget(.DocumentUrl, `progn {
        if (!destroyed_p(@self))
            (@self).processWgetResult();
    });
}

method XMLReader.processWgetResult()
{
    if (ExitCode == 0) // success
    {
        // Print the raw XML document
        princ ("------- Original Document -------\n");
        princ (ResultString);
        princ ("---------------------------------\n");

        // Parse the XML into a class hierarchy that we can easily
	// manipulate
        local reader =
	  scew_reader_buffer_create(string_to_buffer(ResultString));
        local parser = new scew_parser();
        local tree = parser.load(reader);
        local model = tree.create_model("myxml_");
		
        // Print the whole XML file model.
        // pretty_princ(model, "\n");
		
        // The XML model consists of a class instance for each XML
	// Element.  Each class has a member called _xml_children
	// that is an array of the sub-elements.
        // In addition, each class has member variables that are
	// named as the attributes of the XML element.  Replace this
	// with your own custom processing.
        with point in model._xml_children do
        {
            princ (point.name, " = ", point.value, "\n");
        }
    }
    else
    {
        princ ("XML read failed with exit code: ", ExitCode, "\n");
    }
}

/* Write the 'main line' of the program here. */
method XMLReader.constructor ()
{
    .TimerEvery(.TickSeconds, `(@self).loadXml());
}

/* Any code to be run when the program gets shut down. */
method XMLReader.destructor ()
{
}

/* Start the program by instantiating the class. */
ApplicationSingleton (XMLReader);