Esempio FB_XmlWrite

Panoramica

L'esempio seguente mostra come elementi e attributi memorizzati nel buffer fornito dall'applicazione vengono scritti nel file XML. Oltre agli elementi e attributi, è fornito un esempio per contenuto aggiuntivo. Per ulteriori informazioni, vedere anche la EcoStruxure Machine Expert - Guida di esempio per la gestione dei file XML.

Buffer

Il buffer fornito dall'applicazione di tipo XmlItems contiene elementi da scrivere nel file XML.

Indice array

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

Esempio di programma

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 => ) ;

Esempio di file XML

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