8 Replies Latest reply on Jul 1, 2016 12:00 PM by beverly

    Parse xml for one Element with script

    anchhor

      Hi,

      can I read an XML file (UTF8) and parse out the contents of one element. I only need the text in between <text> and </text>. And this element only comes up once in the xml file.

      Thanks, Sandro

        • 1. Re: Parse xml for one Element with script
          Magnus Fransson

          Hi anchhor,

           

          You have to somehow get the text mass in to a field (import maybe), to parse your part out.

           

          I frequently use a general text parsing custom function that I created many years ago to do such things.

           

          /*
          Syntax: ~ParseText ( Text ; Start ; End ; Number )
          */
          
          
          Let(
            [
                Count = PatternCount ( Text ; Start )
            ;
                XNumber = Case( GetAsNumber( Number )  ≤ 1 ; 1 ; Floor( GetAsNumber( Number ) ) )
            ;
                SLength = Length ( Start )
            ;
                SPos = Case( IsEmpty( Start ) ; 1 ; Position ( Text ; Start ; 0 ; XNumber ) + SLength )
            ;
                EPos = Case( IsEmpty( End) ; Length ( Text ) + 1 ; Position ( Text ; End ; SPos ; 1 ) )
            ;
                TLength = EPos - Spos
            ;
                Result = Middle ( Text ; SPos ; TLength )
            ;
                Error = GetAsBoolean( ( ( ( GetAsNumber( XNumber ) > GetAsNumber( Count ) ) or ( GetAsNumber( Count ) < 1 ) ) and ( not IsEmpty( Start ) ) ) or IsEmpty( Text ) )
            ]
          ;
            Case( not Error ; Result )
          )
          

           

          The syntax in your case would be something like:

          ~ParseText ( YourTable::YourField ; "<text>" ; "</text>" ; 1 )

           

          With best regards Magnus Fransson.

          1 of 1 people found this helpful
          • 2. Re: Parse xml for one Element with script
            beverly

            There are several custom functions (require FMPAdvanced to edit/add) that "parse XML". Here is one that would work for you question:

            ExtractData ( XML ; Attribute ; Instance )

            https://www.briandunning.com/cf/1

             

            There are other custom functions. If you do not have FMPAdvanced, I have been able to use the same logic in a CF to script (looping if necessary).

            If you need more complex, there are also several plug-ins to "parse XML"

             

            If you wish to import a lot of XML, XSLT can also be used to transform different schema/formats to the FMPXMLRESULT grammar required for import to FMP.

            beverly

            1 of 1 people found this helpful
            • 3. Re: Parse xml for one Element with script
              karina

              Hi,

               

              Can you show us the xml and give us the field you want to import?

              • 4. Re: Parse xml for one Element with script
                anchhor

                Thanks for the answers so far.

                 

                This would be the xml.

                 

                <?xml version="1.0" encoding="UTF-8"?>

                <?xml-stylesheet type="text/css" href="C:\Program Files\Oxygen XML Editor 17\frameworks\tei\xml\tei\css\tei_oxygen_ERC.css"?>

                <!DOCTYPE TEI SYSTEM "http://www.tei-c.org/Vault/P5/2.2.0/xml/tei/custom/schema/dtd/tei_all.dtd">

                <TEI xmlns="http://www.tei-c.org/ns/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                   xml:id="DATENSATZID">

                   <teiHeader>

                      <fileDesc>

                         <titleStmt>

                            <title/>

                         </titleStmt>

                         <publicationStmt>

                            <date/>

                         </publicationStmt>

                         <sourceDesc>

                            <ab/>

                         </sourceDesc>

                      </fileDesc>

                   </teiHeader>

                   <text>

                      <body>

                         <div type="edition">

                            <ab/>

                         </div>

                         <div type="translation" xml:lang="en">

                            <ab/>

                         </div>

                      </body>

                   </text>

                </TEI>

                • 5. Re: Parse xml for one Element with script
                  beverly

                  ok, your original post asked for between <text> & </text>

                  ======

                     <text>
                        <body>
                           <div type="edition">
                              <ab/>
                           </div>
                           <div type="translation" xml:lang="en">
                              <ab/>
                           </div>
                        </body>
                     </text>

                   

                  ======

                  you are going to have several things "to get". Likely, these two would be different values:

                  "ab", where the XPath => text/body/div[@type='edition']/ab

                  "ab", where the XPath => text/body/div[@type='translation']/ab

                  what do you really want and where do you want it?

                  beverly

                   

                  EDITED as text was munched in reply-by-email

                  • 6. Re: Parse xml for one Element with script
                    anchhor

                    Thanks, I am totally fine with getting all that is within the text element. So the first answer will probably do the trick.

                    • 7. Re: Parse xml for one Element with script
                      anchhor

                      Sorry, one more question. I noticed, that I do need FM Advanced, right?! We got Server and several FM Pros. If I then write the Function in Advanced, will our team members be able to use it in their FM Pro Version?

                      • 8. Re: Parse xml for one Element with script
                        beverly

                        if you "install" the Custom Function into your file(s) with FMPA, then any FMPro can use the function. You only need the Advanced to edit/create custom functions.

                        beverly

                        1 of 1 people found this helpful