Tech Support Corner: Sharing XML File Data With Cogent DataHub

6 min read

Nov 5, 2020 2:00:00 PM

If you've read our other posts on sharing XML data, you'll remember that our tech support team regularly gets asked how to integrate XML (eXtensible Markup Language) data with other systems such as OPC clients or servers, databases, devices or even the cloud.

Continuing our discussion on Integrating XML Data (part of our continuing Tech Support Corner blog series), this post covers how to integrate XML data with a variety of other systems including OPC clients and servers, databases and more using the built-in scripting engine and interfaces of Cogent DataHub. 

Xml_logo.svgSo just to reiterate from the previous posts, there are a variety of reasons you might need to access data in an XML format and share it with other systems. For instance, XML is commonly used to quickly initialize data points in a PLC or other device. Some other common applications include reading and integrating stored configuration information for devices and software or integrating process data that some devices provide in an XML format.

It's also common for web-based applications to exchange data using an XML payload format. Regardless of what data and where it's coming from, what you want to do with the data once you have accessed it is the most important question.

This post specifically addresses the need to take XML data and share it with different systems that Cogent DataHub can connect to using its array of different interfaces.

Integrating XML data with many other IT and OT systems

Cogent DataHub scripting is a powerful tool that can be used to configure custom implementations for many situations and enable functionality beyond connectivity to many other systems . DataHub provides a comprehensive gateway that unifies data from all sources and any protocol into organized datasets.

The DataHub scripting engine builds upon that without having to manage any of the low level communication or connection stacks. Scripting allows for the XML file or data stream to be processed with values assigned to DataHub points which then can be then be integrated using any of the other supported DataHub interfaces, including OPC UA client and server, OPC DA client and server, ODBC databases, DDE client and server, MQTT clients and brokers and more.

How to use DataHub for XML data integration

1. Defining Classes to match your XML file data format

The first step in integrating XML data using the DataHub scripting engine is creating a few classes with matching the attributes and sub-elements from the format in your XML file or data stream.  For example, the following XML file has a main Element called "MachineSetPoints" with a defined format of attributes and sub-elements for each machine. Any number of machines following the same standard format could be accessed using this method.

Example - XML Data File

So crafting a Class must be created in the DataHub scripting engine for the root MachineSetPoints object in the XML file. Additionally, a class is needed for the inner Machine object with Elements for each parameter. An example of that script would look like the following:

Example - Defining a Class with DataHub Scripting

This will allow for the XML file to be processed and deserialized to the model - with element values being assigned to each member of the Machine class.

2. Processing and Deserializing XML data with your DataHub script

The next step is using the "Machine" class for processing and deserializing the model. The LoadXML file method opens the file specified by the ‘filename’ input variable, and loads the XML file content into a local variable. The read_n_chars() method is used to then read the content of the XML file itself and assigns it to the totalString local variable where it can be processed. This allows for the XMLSupport methods to parse the total string of the XML file contents.

Example - DataHub XML File Load script

This method also creates a result variable which will contain all the Machine classes within it as well as the domainName which will provide necessary information for associating the XML variables to the DataHub points.

3. Parsing the XML data contents with DataHub scripting

The last step is parsing the actual data from the XML file into DataHub points.  This requires creating a local reader in the script which will read the XML file content string and using a parser to load the reader into a tree, which will be used to create a model.

Example - XML file parsing with DataHub scripting
The model will have a structure where each “child” in the model will be a machine ‘object’ in the XML file. The model will then be looped through for each child assigning each parameter of the Machine to the MachineSetPoint class. Then we need to write the value for each variable to the corresponding DataHub point name using the DataHub_Write command. If the points do not already exist, the datahub_write command will create them prior to pushing the data value to the desired system.

Example - Writing XML data with DataHub scripting

4. Verifying XML data values are correct in DataHub points

Now that your DataHub script has integrated the XML data, you can verify everything is working correctly using the DataHub's View Data window.

Screenshot - Viewing XML data points in DataHub View Data window

5. Integrate XML data with any other system DataHub supports

Now that your successfully parsing the XML data into DataHub points, those points are available for sharing your XML data with any other system that DataHub has an interface for.  For tutorials on setting up most of DataHub's other supported interfaces, you can visit our DataHub how-to videos page.

Diagram - DataHub supports a wide variety of connectivity options

So, to close, flexible scripting functionality coupled with DataHub's many supported interfaces makes it easy to extract XML data and share it with virtually any other system you might need to access that data. If you haven't already tried the Cogent DataHub, the free trial version is available here.

And don't forget to subscribe to our blog to not only be notified of our other XML Integration posts as they are available but to find out about more relevant technical topics applying to industrial process automation and solving related challenges.  Want to try integrating your own XML data using the Cogent DataHub?

New call-to-action

Win Worrall
Written by Win Worrall

Software Toolbox Technical Blog

We're engineers like you, so this blog focuses on "How to" appnotes, videos, tech team tips, product update announcements, user case studies, and other technical updates.  Subscribe to updates below. Your feedback and questions on posts are always welcomed - just use the area at the bottom of any post.

Subscribe to our Blog

Recent Posts

Posts by Topic

See all