----------------------------------------------------------------------
--- Knud van Eeden --- 21 October 2007 - 06:00 pm --------------------

Computer: Editor: TSE: XML: How to read your .xml files in TSE: Method: Create Perl program using XML::Simple, redirect XML file to it, insert output file in TSE

---

Steps: Overview:

 1. -Download and install Perl

     1. -Install (free) ActivePerl v5.8.8 or higher

          http://www.activestate.com/store/activeperl/download/

         (optionally fill in your contact details,
          otherwise skip this and press button 'continue',
          choose 'Windows (x86)' MSI).
          Then run this .msi file by running it, otherwise downloading
          and double clicking on it, or using
          'start <your .msi file>'
         in MSDOS)

     2. -This includes out of the box the package
         (it is thus not necessary to separately install it)

          XML::Simple

 2. -Make sure your example XML file exists on disk

     1. -E.g.

--- cut here: begin --------------------------------------------------

          <MYBOOKS>

           <BOOK>

            <AUTHOR>
              John Doe
            </AUTHOR>

            <TITLE>
              How to use XML in TSE?
            </TITLE>

            <PICTURE>
              picture1.jpg
            </PICTURE>

           </BOOK>

           <BOOK>

            <AUTHOR>
              Isaac Newton
            </AUTHOR>

            <TITLE>
              Principia
            </TITLE>

            <PICTURE>
              picture2.jpg
            </PICTURE>

           </BOOK>

           <BOOK>

            <AUTHOR remark="this is a remark">
              Sammy Mitchell
            </AUTHOR>

            <TITLE>
              TSE
            </TITLE>

            <PICTURE>
              picture3.jpg
            </PICTURE>

           </BOOK>

           <BOOK>

            <AUTHOR remark="this is another remark" year="2008">
              Sammy Mitchell
            </AUTHOR>

            <TITLE>
              Semware
            </TITLE>

            <PICTURE>
              picture4.jpg
            </PICTURE>

           </BOOK>

          </MYBOOKS>

--- cut here: end ----------------------------------------------------

 3. -Then this TSE macro creates a temporary Perl program:

--- cut here: begin --------------------------------------------------

FORWARD PROC Main()
FORWARD PROC PROCFileInsertXmlSimplePerl( STRING s1, STRING s2, STRING s3 )


// --- MAIN --- //

INTEGER filt_history1I = 0
INTEGER filt_history2I = 0
INTEGER filt_history3I = 0
PROC Main()
 STRING s1[255] = "{BOOK}->[1]->{AUTHOR}" // change this
 STRING s2[255] = "c:\temp\book.xml" // change this
 STRING s3[255] = "c:\perl\bin\perl.exe" // change this
 filt_history1I = GetFreeHistory( "FILT:filt_history1I" )
 filt_history2I = GetFreeHistory( "FILT:filt_history2I" )
 filt_history3I = GetFreeHistory( "FILT:filt_history3I" )
 IF ( Ask( "file: insert: xml: simple: perl: s = ", s1, filt_history1I ) == 0 ) RETURN() ENDIF
 IF ( Ask( "file: insert: xml: simple: perl: fileNameXmlS = ", s2, filt_history2I ) == 0 ) RETURN() ENDIF
 IF ( Ask( "file: insert: xml: simple: perl: fileNamePerlExecutableS = ", s3, filt_history3I ) == 0 ) RETURN() ENDIF
 PROCFileInsertXmlSimplePerl( s1, s2, s3 )
END

<F12> Main()

// --- LIBRARY --- //

// library: file: insert: xml: simple: perl (filenamemacro=insefisp.s) [kn, ri, su, 21-10-2007 03:28:03]
PROC PROCFileInsertXmlSimplePerl( STRING s, STRING fileNameXmlS, STRING fileNamePerlExecutableS )
 // e.g. PROC Main()
 // e.g.  PROCFileInsertXmlSimplePerl( "{BOOK}->[1]->{AUTHOR}", "c:\temp\book.xml", "c:\perl\bin\perl.exe" )
 // e.g. END
 // e.g.
 // e.g. <F12> Main()
 // create temporary Perl program file
 STRING fileNamePerlRunS[255] = MakeTempName( ".", ".pl" )
 // create temporary Perl output file containing the XML result
 STRING fileNamePerlOutputS[255] = MakeTempName( ".", ".txt" )
 //
 PushPosition()
 //
 // Make sure you get the full path
 EditFile( fileNamePerlRunS )
 fileNamePerlRunS = CurrFileName()
 SaveFile()
 //
 // Make sure you get the full path
 EditFile( fileNamePerlOutputS )
 fileNamePerlOutputS = CurrFileName()
 SaveFile()
 //
 // Create a Perl program
 //
 EditFile( fileNamePerlRunS )
 AddLine( "use XML::Simple;" )
 AddLine( "my $config = XMLin( '-' );" )
 AddLine( "print" + " " + "$config->" + s + ";" )
 SaveFile()
 //
 // ExecMacro( "capture.mac" + " " + QuotePath( fileNamePerlExecutableS ) + " " + QuotePath( fileNamePerlRunS ) + " " + "<" + QuotePath( fileNameXmlS ) + " " + ">" + QuotePath( fileNamePerlOutputS ) )
 //
 // Run the Perl executable with your Perl program
 // and as first parameter your XML file
 // and as second parameter the file into which STDOUT is stored
 Dos( QuotePath( fileNamePerlExecutableS ) + " " + QuotePath( fileNamePerlRunS ) + " " + "<" + QuotePath( fileNameXmlS ) + " " + ">" + QuotePath( fileNamePerlOutputS ), _DONT_CLEAR_ | _START_HIDDEN_ | _RUN_DETACHED_ )
 //
 PopPosition()
 //
 // Now insert into your TSE program the file in which the STDOUT is stored
 // (if empty then e.g. your syntax is wrong,
 //  or you did point to the wrong position in the XML file)
 InsertFile( QuotePath( fileNamePerlOutputS ) )
 //
 EditFile( fileNamePerlRunS )
 AbandonFile()
 EraseDiskFile( fileNamePerlRunS ) // delete the temporary file
 //
 EditFile( fileNamePerlOutputS )
 AbandonFile()
 EraseDiskFile( fileNamePerlOutputS ) // delete the temporary file
END

--- cut here: end ----------------------------------------------------

 4. -This Perl program

     1. -Uses a standard
         Perl package called

          XML::Simple

     2. -This loads your XML file
         by using the method XMLin()
         (also XMLout() exists, to write to your XML files)

     3. -It reads your XML file from STDIN
         (that is represented here by '-')

     4. -Finally it extracts from your XML file
         the text, at the indicated by you positionstring
         (you build that by walking the tree, while
          concatenating {}, -> and [0], [1], [2], ...)

 5. -Then TSE runs

      perl.exe yourPerlProgram.pl <yourXmlFile.xml >yourOutputFile.txt

     (via Dos(), redirecting input and output as usual using '<' and '>'
      (as applied in other TSE macros like 'capture.s', 'filter.s', ...))

 6. -To extract your XML values out of your XML file
     (typically a .INI like configuration file, ...):

     1. -Build that {} path, starting from the root,
         so that it points to that (text) value.

     2. -In general you skip the
         outer tags in your XML file,
         or you get empty result back
         (e.g. do not include <MYBOOKS>)

     3. -The counting starts from 0.
         Thus 0, 1, 2, ...
         Thus [0], [1], [2], ...
         You work from top to bottom in your XML file.
         The first tag (e.g. <BOOK>) that occurs from the top is 0.
         The next tag with the same name is 1.
         The next tag with the same name is 2.
         And so on.
         When you arrive at that tag, you can get its attribute (e.g.
         remark="...") by just supplying its name between {}, like
         {remark}. You do not add thus something like [1] to the find
         the attribute, only the name is enough to uniquely identify
         it.

     4. -The tag names are case sensitive
         (thus e.g. 'BOOKS' is not the same as 'books').
         So use the tag names verbatim as they occur
         in your XML file.

     5. -You start at the next outer tags,
         e.g. indicate which of the book tags you want: 0, 1, 2, 3, ...
         and use -> to point to further inner tags from there

          E.g. {BOOK}->[0]->{AUTHOR}
               means
               goto the 0th tag <BOOK>
               goto its inner tag {AUTHOR}
               and get the text there

               e.g.

                John Doe

          E.g. {BOOK}->[0]->{TITLE}
               means
               goto the 0th tag <BOOK>
               goto its inner tag {TITLE}
               and get the text there

               e.g.

                How to use XML in TSE?

          E.g. {BOOK}->[0]->{PICTURE}
               means
               goto the 0th tag <BOOK>
               goto its inner tag {TITLE}
               and get the text there

               e.g.

                picture1.jpg

          E.g. {BOOK}->[1]->{AUTHOR}
               means
               goto the 1st tag <BOOK>
               goto its inner tag {AUTHOR}
               and get the text there

               e.g.

                Isaac Newton

          E.g. {BOOK}->[1]->{TITLE}
               means
               goto the 1st tag <BOOK>
               goto its inner tag {TITLE}
               and get the text there

               e.g.

                Principia

          E.g. {BOOK}->[2]->{AUTHOR}->{remark}
               means
               goto the 2nd tag <BOOK>
               goto its tag <AUTHOR>
               goto its attribute 'remark'
               and get the text there

               e.g.

                this is a remark

          E.g. {BOOK}->[3]->{AUTHOR}->{year}
               means
               goto the 3th tag <BOOK>
               goto its tag <AUTHOR>
               goto its attribute 'year'
               and get the text there

               e.g.

                2008

 7. -Troubleshooting

     1. -If you get no result back in TSE (thus nothing or empty line),
         it means usually that

         1. -you did not build the path {} correctly.

             E.g.

              {BOOK}->{AUTHOR}

             instead of

              {BOOK}->[1]->{AUTHOR}

            Check this by looking at the default examples,
            or looking in the XML::Simple manual

         2. -Some error in your XML file

             (e.g. tags <> wrong, or tags <> forgotten (e.g. closing
                   tag))

             You can e.g. run the (free) program 'tidy.exe' to pretty print
             or do validation of your XML.

         3. -If you use the example XML file, and use the example
             {} path, it should work.
             Actually the 'e.g.' examples (like 'this is a remark')
             above where directly inserted into
             this file by using this TSE macro.

         4. -If all fails, if possible, try the program directly in Perl.
             Then work forward to getting it working in TSE.

 8. -Remark

     1. -It works OK in TSE, but rather slow.

===

Tested successfully on
Microsoft Windows XP Professional (service pack 2),
running
ActivePerl Perl v5.8.8
TSE v4.4.x

===

Book: see also:

[book: Christiansen, Tom / Torkington, Nathan - Perl Cookbook (2nd Edition) - O'Reilly - 'Recipe 22.1. Parsing XML into Data Structures']

===

Internet: see also:

XML::Simple
http://search.cpan.org/dist/XML-Simple-2.17

---

Some other 'simple' Perl packages
http://search.cpan.org/dist/XML-Simple-2.16

---

XML::Simple
http://www.ibm.com/developerworks/web/library/x-xmlperl1.html

---

XML::Simple: manual
C:\Perl\html\lib\XML\Simple.html

---

Computer: Editor: TSE: XML: Link: Overview: Can you give an overview of links?
http://www.knudvaneeden.com/tinyurl.php?urlKey=url000008

----------------------------------------------------------------------