XML Utility SL Library Documentation

Company:

CODESYS

Title:

XML Utility SL

Version:

1.11.0.0

Categories:

Application|Utils

Namespace:

XML

Author:

CODESYS Development GmbH

Placeholder:

XML_Utility_SL

Description [1]

Library to read and write XML files. The library can read and write UTF-8 and UTF-16 coded well-formed xml files.

Supported xml elements:

  • Standard element <xxx>aaa</xxx> and <xxx />

  • Attributes yyy="zzz"

  • Comments <!-- Comment -->

  • CDATA elements

Each element of the file is stored in a structure of the type XMLElement. A complete xml file or parts of it will be stored in an array of structures.

Function block to read xml files

Function block to read xml elements with following options:
  • Read one element only

  • Read all children of an element

  • Read all underlying elements

The reading is done file based and buffered, so it is possible to load an element without loading the complete file.

Asynchronous version of the function block XMLGetElement.

Function blocks to find xml elements

Function block to find xml elements with following search options:
  • Search by element name

  • Search by attribute name and value

  • Read all children of the search result

  • Read all underlying elements of the search result

  • Start position in the file of the search

Asynchronous version of the function block XMLFindElementAsync.
Functions like function block XMLFindElementAsync with STRING input fields.

Function blocks to write xml files

Function block to write xml files. Partial writing of elements is not supported, i.e. the complete file structure must be loaded.
Asynchronous version of the function block XMLWrite.

Parameters

Reading files is buffered. The size of the buffer must greater than the size of the largest xml element. The size of the buffer and the maximum WSTRING size of the structure XMLElement can be changed in the parameter list (param) of the library manager. The size of the data array can be declared individually outside the function blocks.

Example project XMLUtilityExample.project

The example project XMLUtilityExample contains two example applications for the xml library. The application XMLFindExample (see Figure 1) shows how to search for elements and how to load child elements. The application has three functions. The button ‘Find first XML element’ returns the first element with the specified element name and attribute value. If no element name is set than the root element will be returned. The button ‘Find next element’ will continue the search from the current position and will return the next element. The function ‘Read children of the selected element’ reads all child elements of the selected element. You can use the example XML file ‘c:\temp\SimpleXMLExample.xml’ to test the application.

_images/XMLExample2.png

Figure 1: Visualization of application ‘XMLFindExample’

Example XML file ‘SimpleXMLExample.xml’

<?xml version="1.0" encoding="utf-8"?>
<Elements>
  <!--This is a simple xml example -->
  <Element attribute1="1">
    This is the value of Element 1
  </Element>
  <Element attribute1="2">
    This is the value of Element 2
  </Element>
  <Element attribute1="3" attribute2="33789" attribute3="99.98">
    This is the value of Element 2
  </Element>
  <Element attribute1="4">
    <Subelement id="1">
      Value of Subelement 1
    </Subelement>
    <Subelement id="2">
      Value of Subelement 2
    </Subelement>
    <Subelement id="3">
      Value of Subelement 3
      <!--3. level -->
      <Child attribute1="1">
        Element4/Subelement3/Child1
      </Child>
      <Child attribute1="2">
        Element4/Subelement3/Child2
      </Child>
      <Child attribute1="3">
        Element4/Subelement3/Child3
      </Child>
    </Subelement>
  </Element>
  <!--CDATA and escaping -->
  <Text id="1">
    <![CDATA[This is a CDATA section.]]>
  </Text>
  <Text id="2">
    Lesser than: &lt; Greater than: &gt; And: &amp; Apostroph: &apos; Quote: &quot;
  </Text>
</Elements>

The application XMLReadWriteExampleAsync (see Figure 2 and Figure 3) reads and writes xml files. The function ‘Read XML file’ loads every element of the file ‘SimpleXMLExample.xml’ in an array of XMLElements. The function ‘Write XML file’ writes the loaded elements in a new file called ‘SimpleXMLExample2.xml’. Each element of the file is loaded to the memory. It is important to ensure that the size of data array is large enough to hold the data.

_images/XMLExample1.png

Figure 2: Visualization of application ‘XMLReadWriteExampleAsync’

_images/XMLExample3.png

Figure 3: CFC of application ‘XMLReadWriteExampleAsync’

Contents:

Indices and tables