Sample code for 30+ languages & platforms
DataFlex

XMP API Helps Build the XML

The Chilkat XMP API is helpful in making it easier to build the XML that comprises XMP. If an XMP API feature does not exist to create the desired XML structure, then the Chilkat XML API can be used. The important thing to notice is that all XMP methods that modify XML pass the XML to be modified in the 1st argument. Thus, the XMP methods are really just helper methods to do common tasks in creating/editing the XML.

This example shows how to create a bag with a more complicated structure -- something that is not automatically provided by the XMP API, but can easily be done using the XML API.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoXmp
    Variant vXml
    Handle hoXml
    Variant vSa
    Handle hoSa
    Variant vX
    Handle hoX
    String sTemp1

    Move False To iSuccess

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    Get Create (RefClass(cComChilkatXmp)) To hoXmp
    If (Not(IsComObjectCreated(hoXmp))) Begin
        Send CreateComObject of hoXmp
    End

    // The first step is to create a new XMP document, which is nothing
    // more than XML.  The NewXmp method returns an XML document with
    // the standard XMP boilerplate.  
    Get ComNewXmp Of hoXmp To vXml
    If (IsComObject(vXml)) Begin
        Get Create (RefClass(cComChilkatXml)) To hoXml
        Set pvComObject Of hoXml To vXml
    End

    // Add some properties...
    Get ComAddSimpleStr Of hoXmp vXml "Iptc4xmpCore:Chilkat" "Blah blah" To iSuccess

    // Now create a Bag.
    Get Create (RefClass(cComCkStringArray)) To hoSa
    If (Not(IsComObjectCreated(hoSa))) Begin
        Send CreateComObject of hoSa
    End
    Get ComAppend Of hoSa "value_1" To iSuccess
    Get ComAppend Of hoSa "value_2" To iSuccess
    // ...

    // Notice the XML object is passed in the 1st argument.
    // The XMP method is really just a higher-level means to create XML
    // commonly needed for XMP.
    Get pvComObject of hoSa to vSa
    Get ComAddArray Of hoXmp vXml "bag" "Iptc4xmpExt:LocationCreated" vSa To iSuccess

    // Let's examine the XML..
    Get ComGetXml Of hoXml To sTemp1
    Showln sTemp1

    // the bag looks like this:

    //     <Iptc4xmpExt:LocationCreated>
    //         <rdf:Bag>
    //             <rdf:li>value_1</rdf:li>
    //             <rdf:li>value_2</rdf:li>
    //         </rdf:Bag>
    //     </Iptc4xmpExt:LocationCreated>

    // Let's say we really want this:

    //  <Iptc4xmpExt:LocationCreated>
    //    <rdf:Bag>
    //     <rdf:li rdf:parseType='Resource'>
    //      <Iptc4xmpExt:City>Ville création photo</Iptc4xmpExt:City>
    //      <Iptc4xmpExt:CountryName>Nom diu pays de la photo</Iptc4xmpExt:CountryName>
    //      <Iptc4xmpExt:ProvinceState>Département; Photo</Iptc4xmpExt:ProvinceState>
    //      <Iptc4xmpExt:Sublocation>Lieu création photo</Iptc4xmpExt:Sublocation>
    //     </rdf:li>
    //    </rdf:Bag>
    //   </Iptc4xmpExt:LocationCreated>

    // No problem...  Let's just fix it.

    Get ComSearchForTag Of hoXml vXml "Iptc4xmpExt:LocationCreated" To vX
    If (IsComObject(vX)) Begin
        Get Create (RefClass(cComChilkatXml)) To hoX
        Set pvComObject Of hoX To vX
    End

    // Assume it was found..
    // Move to the rdf:Bag node
    Get ComFirstChild2 Of x To iSuccess

    // Discard the existing <rdf:il> values..
    Send ComRemoveAllChildren To x

    // Build the new XML in its place..
    Send ComNewChild2 To x "rdf:li" ""
    Get ComAddAttribute Of x "rdf:parseType" "Resource" To iSuccess

    // Move to the "rdf:li" node.
    Get ComFirstChild2 Of x To iSuccess
    Send ComNewChild2 To x "Iptc4xmpExt:City" "Ville création photo"
    Send ComNewChild2 To x "Iptc4xmpExt:CountryName" "Nom diu pays de la photo"
    Send ComNewChild2 To x "Iptc4xmpExt:ProvinceState" "Département; Photo"
    Send ComNewChild2 To x "Iptc4xmpExt:Sublocation" "Lieu création photo"

    // Let's create another bag item..
    // Move back up to the "rdf:Bag" node.
    Get ComGetParent2 Of x To iSuccess

    Send ComNewChild2 To x "rdf:li" ""
    Get ComAddAttribute Of x "rdf:parseType" "Resource" To iSuccess

    // Move to the newly added "rdf:li" node, which is the 2nd child (index = 1)
    Get ComGetChild2 Of x 1 To iSuccess
    Send ComNewChild2 To x "Iptc4xmpExt:City" "2 Ville création photo"
    Send ComNewChild2 To x "Iptc4xmpExt:CountryName" "2 Nom diu pays de la photo"
    Send ComNewChild2 To x "Iptc4xmpExt:ProvinceState" "2 Département; Photo"
    Send ComNewChild2 To x "Iptc4xmpExt:Sublocation" "2 Lieu création photo"

    Send Destroy of x

    // Let's examine the XML now...
    Get ComGetXml Of hoXml To sTemp1
    Showln sTemp1

    Send Destroy of hoXml


End_Procedure