FB_XmlWrite Example

Overview

The following example shows how the elements and attributes stored in the buffer provided by the application are written to the XML file. In addition to the elements and attributes, an example for the additional content is provided. For further information, also refer to the EcoStruxure Machine Expert XML File Handling Example Guide.

Buffer

The buffer provided by the application of type XmlItems contains elements to be written to the XML file.

Array Index

diParentIndex

sName

sValue

etType

diNumOfAttributes

0

-1

AAA

1

0

1

0

BBB

1st bbb

1

0

2

0

BBB

1

0

3

2

CCC

1

1

4

3

id

1

2

1

5

3

DDD

ddd

1

2

6

5

id

1

2

1

7

5

activate

TRUE

2

2

Example Program

PROGRAM SR_Example
VAR
    fbWrite : FFU . FB_XmlWrite ;
    fbXmlUtil : FFU . FB_XmlItemsUtility ;
    astXmlItems : FFU . XmlItems ;
    xCmdWrite : BOOL ;
    sComment : STRING ( 255 ) := 'This is the user comment.' ;
    sExternalDTD : STRING := '<!DOCTYPE AAA SYSTEM "example.dtd">' ;
    stHeader : FFU . ST_XmlUserDefinedHeader ;
    xInitDone : BOOL ;
END_VAR

IF NOT xInitDone THEN
    xInitDone := TRUE ;
    SysMem . SysMemSet ( ADR ( astXmlItems ) , 0 , SIZEOF ( astXmlItems ) ) ;
    fbXmlUtil . InitializeXmlItems ( 'AAA' , astXmlItems ) ;
    fbXmlUtil . AddSubElement ( 'BBB' ) ;
    fbXmlUtil . ValueOfSelectedElement := '1st bbb' ;
    fbXmlUtil . AppendElement ( 'BBB' ) ;
    fbXmlUtil . AddSubElement ( 'CCC' ) ;
    fbXmlUtil . AddAttribute ( 'id' , '1' ) ;
    fbXmlUtil . AddSubElement ( 'DDD' ) ;
    fbXmlUtil . ValueOfSelectedElement := 'ddd' ;
    fbXmlUtil . AddAttribute ( 'id' , '1' ) ;
    fbXmlUtil . AddAttribute ( 'activate' , 'TRUE' ) ;
END_IF

stHeader . sUserComment := sComment ;
stHeader . pbyAdditionalContent := ADR ( sExternalDTD ) ;
stHeader . udiNumBytesToWrite := INT_TO_UDINT ( Standard . LEN ( sExternalDTD ) ) ;

fbWrite (
        i_xExecute := xCmdWrite ,
        i_sFilePath := './myfiles/Example.xml' ,
        i_xOverwriteFile := ,
        i_sRootElement := ,
        i_refXmlItems := astXmlItems ,
        i_stUserDefinedHeader := stHeader ,
        i_timTimeout := ,
        q_xDone => ,
        q_xBusy => ,
        q_xError => ,
        q_etResult => ,
        q_sResultMsg => ) ;

Example XML File

<?xml version="1.0" encoding="ASCII"?>
<!--This is the user comment.-->
<!DOCTYPE AAA SYSTEM "example.dtd">

<AAA>
  <BBB>1st bbb</BBB>
    <CCC id="1">
      <DDD id="1" activate="TRUE">ddd</DDD>
    </CCC>
  </BBB>
</AAA>