1 Reply Latest reply on Nov 30, 2008 4:22 AM by (Herb_Albert)

    LrXML sample

      Hello,
      I am trying to begin employing the SDK to improve my workflow and need to generate and write an XML file out to a specified path from metadata of each of the images selected for export.
      For example, export.xml could be:


        
             London
             capital,town,uk
        

        
             Alice
             girl,actress,movie,cat
        



      I could not find a simple example on how to use LrXML to achieve such result, does anybody know of any?

      Thank you
        • 1. Re: LrXML sample
          Level 1
          Hi.

          Pretty straightforward, you won't get indentation though.

          local LrApplication = import 'LrApplication'
          
          local LrXml = import 'LrXml'
          local LrLogger = import 'LrLogger'

          function xmlFromPhotos(catalog, photos)
            
          local xmlBuilder = LrXml.createXmlBuilder(true) -- omit XML declaration
          xmlBuilder:beginBlock('images')

          for i, photo in ipairs(photos) do

            local title = nil
            local keywords = nil

            catalog:withReadAccessDo( function()
             title = photo:getFormattedMetadata('title')
             -- keywords = photo:getFormattedMetadata('keywordTagsForExport') -- LR2 only
             keywords = photo:getFormattedMetadata('keywordTags')
            end )

            local blockname = 'image' .. i

            xmlBuilder:beginBlock(blockname)
            xmlBuilder:tag('titl', title)
            xmlBuilder:tag('keywords', keywords)
            xmlBuilder:endBlock(blockname)
          end

          xmlBuilder:endBlock('images')
          return xmlBuilder:serialize()
          end

          local catalog = LrApplication.activeCatalog()
          local photos = nil
          catalog:withReadAccessDo( function()
          photos = catalog.targetPhotos
          end )

          local xmlString = xmlFromPhotos(catalog, photos)

          local myLogger = LrLogger('libraryLogger')
          myLogger:enable('logfile')
          myLogger:info(xmlString)


          PS: I don't think it makes sense to introduce numbering within the tag name, so it better should be "image" than "image1", "image2" etc.

          Herb