Skip to main content

Example: Using the XML Utility SL Library

Product: CODESYS IIOT Libraries SL

The XML Utility SL Example.project sample project shows how to use of the XML Utility SL library.

The sample project contains applications with visualizations for reading and writing XML files. The individual elements are stored in an array of structures. In addition, search queries for XML elements can also be performed.

Description

The XML Utility SL Example.project sample project contains the two sample applications XMLFindExample and XMLReadWriteExampleAsync.

Application: XMLFindExample

The XMLFindExample application shows how to search for XML elements in a file and how to load child elements of XML elements (see the following listing).

<?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>

Three functions are available in the application:

  • Find first XML element

    This button is used to search for the first element with the specified name and its attribute.

  • Find next element

    This button can be used to search for additional elements.

  • Read children of selected element

    This button reads all child elements of the currently selected element.

The SimpleXMLExample.xml file can be used to test the application.

Tip

To test the examples on a CODESYS Control Win, you can copy the SimpleXMLExample.xml and SimpleXMLExample_UTF16.xml files from the target directory of the installation to the c:\temp directory.

Application: XMLReadWriteExampleAsync

The XMLReadWriteExampleAsync application reads and writes an XML file.

  • Read XML file

    Reads the entire XML file and stores the values in structures of type XMLElement

  • Write XML file

    Saves the structures in the XML file SimpleXMLExample2.xml

In the example, the entire XML file is loaded into memory. Make sure to choose a data array which is large enough.

System requirements and restrictions

Programming system

CODESYS Development System (version 3.5.18.0 or higher)

Runtime system

CODESYS Control Win (version 3.5.18.0)

Add-on components

-

Note

_example_icon.png DOWNLOAD projects