TCL/PHP/XML problem: I need to convert an XML file into a TCL list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • comp.lang.tcl

    TCL/PHP/XML problem: I need to convert an XML file into a TCL list

    My TCL proc, XML_GET_ALL_ELE MENT_ATTRS, is supposed to convert an XML
    file into a TCL list as follows:

    attr1 {val1} attr2 {val2} ... attrN {valN}

    This is the TCL code that does this:

    [TCL]
    set contents [read $fileID [file size ${fileName}.xml]]; close $fileID


    if {![string equal $switch -body]} {
    # ONLY DO THIS IF THE XML CONTENTS CONTAIN NO BODY - WILL UPGRADE AT
    A LATER DATE 11/24/2006 - PHIL
    global serverName
    if {![info exists serverName]} {
    global env
    source ./cgi_globals.tcl
    global serverName
    }

    if {[string length [info procs {URL_ENCODE}]] == 0} { source
    ../url_procs.tcl }; # INCLUDE NEW url_procs.tcl URL TCL LIBRARY
    if {[string length [info procs {IS_LIST}]] == 0} { source
    {./tcl_string_tool s.tcl} }; # INCLUDE THE TCL STRING LIBRARY CONTAINING
    PROC is_list IF !FOUND

    # BLOCK TO CREATE THE PHP SCRIPT TO PIPE INTO php.exe
    regsub -all {'} [XML_CLEANUP_ATT RIBUTE $contents] {\"}
    phpEscapedConte nts
    regsub -all {"} $phpEscapedCont ents {\"} phpEscapedConte nts
    set php {<? }
    append php [subst { if (@is_file("./functions.inc.p hp")) [format %c
    123] }]
    append php { require_once("./functions.inc.p hp"); }
    append php [subst { echo xml_to_tcl_list ("$phpEscapedCo ntents"); }]
    append php [subst {[format %c 125] }]
    append php { ?>}


    set cannotRunPHP [catch {exec "echo '$php' | php" >@stdout
    2>&stderr} errorMsg]
    if {$cannotRunPHP} {
    puts $errorMsg
    return {}
    } else {
    if {![IS_LIST $contentsList]} { set contentsList [split
    $contentsList] }
    if {[llength $contentsList] == 0} { lappend contentsList {?} }
    return [lrange $contentsList [expr {[lsearch -exact $contentsList
    "?"] + 1}] end]
    }

    }
    [/TCL]

    The command-line PHP should look like this:

    [PHP]
    <? if (@is_file("./functions.inc.p hp")) {
    require_once("./functions.inc.p hp"); echo
    xml_to_tcl_list ("$phpEscapedCo ntents"); } ?>
    [/PHP]

    Note that $phpEscapedCont ents is a TCL variable, not a PHP variable

    Ok, so this is what should happen:

    1) PHP function xml_to_tcl_list will convert the inputted escaped XML
    string into a TCL list
    2) I need to return that TCL list into $contentsList and return it so
    the proc returns a TCL list

    However, it never gets that far, here is the error I get:

    couldn't execute "echo '<? if (@is_file("./functions.inc.p hp")) {
    require_once("./functions.inc.p hp"); echo xml_to_tcl_list ("&lt;?xml
    version=&quot;1 .0&quot; encod": file name too long
    I am sure I am going at this the wrong way, but I can't understand the
    right way (TCL DOM parsing is far FAR beyond my ability to understand,
    I've read manuals, tutorials, books, to no avail: I simply don't
    understand how TCL can ever parse XML, I only understand simple PHP
    parsing XML)

    So at this point, all I want to do is convert an XML string into a TCL
    list, and the only way I know how to do it is via PHP, but I can't get
    all three languages to cooperate.

    Help appreciated.

    Thanx
    Phil

  • Bryan Oakley

    #2
    Re: TCL/PHP/XML problem: I need to convert an XML file into a TCLlist

    comp.lang.tcl wrote:
    set cannotRunPHP [catch {exec "echo '$php' | php" >@stdout
    2>&stderr} errorMsg]
    You need to re-read the man page on exec. The first argument to exec is
    a filename that will be exec'd; it is not a command line. If you want to
    run a command line you should exec a command line processor (/bin/sh,
    command.com, whatever).

    Since "php" is what you want to exec, and $php is what you want to send
    to its stdin, use "<<" (documented on the exec man page):

    if {[catch {exec php << $php} result]} {
    puts "error exec'ing php: $resulut"
    } else {
    puts "php result: $result"
    }

    You can read a wiki page on the subject here: http://mini.net/tcl/exec

    Comment

    • comp.lang.tcl

      #3
      Re: TCL/PHP/XML problem: I need to convert an XML file into a TCL list


      Bryan Oakley wrote:
      comp.lang.tcl wrote:
      set cannotRunPHP [catch {exec "echo '$php' | php" >@stdout
      2>&stderr} errorMsg]
      >
      You need to re-read the man page on exec. The first argument to exec is
      a filename that will be exec'd; it is not a command line. If you want to
      run a command line you should exec a command line processor (/bin/sh,
      command.com, whatever).
      >
      Since "php" is what you want to exec, and $php is what you want to send
      to its stdin, use "<<" (documented on the exec man page):
      >
      if {[catch {exec php << $php} result]} {
      puts "error exec'ing php: $resulut"
      } else {
      puts "php result: $result"
      }

      Now I get this error:

      couldn't execute "php": no such file or directory

      When I try this:

      [TCL]
      set cannotRunPHP [catch {exec exec php << $php >@stdout 2>&stderr}
      errorMsg]
      if {$cannotRunPHP} {
      puts $errorMsg
      return {}
      } else {
      if {![IS_LIST $contentsList]} { set contentsList [split
      $contentsList] }
      if {[llength $contentsList] == 0} { lappend contentsList {?} }
      return [lrange $contentsList [expr {[lsearch -exact $contentsList
      "?"] + 1}] end]
      }

      [/TCL]

      Wow this should only take an hour, it's taken 3 days so far!

      Phil
      >
      You can read a wiki page on the subject here: http://mini.net/tcl/exec

      Comment

      • Michael Schlenker

        #4
        Re: TCL/PHP/XML problem: I need to convert an XML file into a TCLlist

        comp.lang.tcl schrieb:
        My TCL proc, XML_GET_ALL_ELE MENT_ATTRS, is supposed to convert an XML
        file into a TCL list as follows:
        >
        attr1 {val1} attr2 {val2} ... attrN {valN}
        >
        This is the TCL code that does this:
        >
        [TCL]
        set contents [read $fileID [file size ${fileName}.xml]]; close $fileID
        >
        >
        if {![string equal $switch -body]} {
        # ONLY DO THIS IF THE XML CONTENTS CONTAIN NO BODY - WILL UPGRADE AT
        A LATER DATE 11/24/2006 - PHIL
        global serverName
        if {![info exists serverName]} {
        global env
        source ./cgi_globals.tcl
        global serverName
        }
        >
        if {[string length [info procs {URL_ENCODE}]] == 0} { source
        ./url_procs.tcl }; # INCLUDE NEW url_procs.tcl URL TCL LIBRARY
        if {[string length [info procs {IS_LIST}]] == 0} { source
        {./tcl_string_tool s.tcl} }; # INCLUDE THE TCL STRING LIBRARY CONTAINING
        PROC is_list IF !FOUND
        >
        # BLOCK TO CREATE THE PHP SCRIPT TO PIPE INTO php.exe
        regsub -all {'} [XML_CLEANUP_ATT RIBUTE $contents] {\&#034;}
        phpEscapedConte nts
        regsub -all {"} $phpEscapedCont ents {\&quot;} phpEscapedConte nts
        set php {<? }
        append php [subst { if (@is_file("./functions.inc.p hp")) [format %c
        123] }]
        append php { require_once("./functions.inc.p hp"); }
        append php [subst { echo xml_to_tcl_list ("$phpEscapedCo ntents"); }]
        append php [subst {[format %c 125] }]
        append php { ?>}
        >
        >
        set cannotRunPHP [catch {exec "echo '$php' | php" >@stdout
        2>&stderr} errorMsg]
        if {$cannotRunPHP} {
        puts $errorMsg
        return {}
        } else {
        if {![IS_LIST $contentsList]} { set contentsList [split
        $contentsList] }
        if {[llength $contentsList] == 0} { lappend contentsList {?} }
        return [lrange $contentsList [expr {[lsearch -exact $contentsList
        "?"] + 1}] end]
        }
        >
        }
        [/TCL]
        >
        The command-line PHP should look like this:
        >
        [PHP]
        <? if (@is_file("./functions.inc.p hp")) {
        require_once("./functions.inc.p hp"); echo
        xml_to_tcl_list ("$phpEscapedCo ntents"); } ?>
        [/PHP]
        >
        Note that $phpEscapedCont ents is a TCL variable, not a PHP variable
        >
        Ok, so this is what should happen:
        >
        1) PHP function xml_to_tcl_list will convert the inputted escaped XML
        string into a TCL list
        2) I need to return that TCL list into $contentsList and return it so
        the proc returns a TCL list
        >
        However, it never gets that far, here is the error I get:
        >
        couldn't execute "echo '<? if (@is_file("./functions.inc.p hp")) {
        require_once("./functions.inc.p hp"); echo xml_to_tcl_list ("&lt;?xml
        version=&quot;1 .0&quot; encod": file name too long
        >
        I am sure I am going at this the wrong way, but I can't understand the
        right way (TCL DOM parsing is far FAR beyond my ability to understand,
        I've read manuals, tutorials, books, to no avail: I simply don't
        understand how TCL can ever parse XML, I only understand simple PHP
        parsing XML)
        >
        So at this point, all I want to do is convert an XML string into a TCL
        list, and the only way I know how to do it is via PHP, but I can't get
        all three languages to cooperate.
        >
        If you showed us part of the XML there surely is a shorter way with one
        of the XML parsers for Tcl like tdom, but maybe your just confused by
        the difference between DOM and SAX style xml parsing.

        Michael

        Comment

        • comp.lang.tcl

          #5
          Re: TCL/PHP/XML problem: I need to convert an XML file into a TCL list


          Michael Schlenker wrote:
          comp.lang.tcl schrieb:
          My TCL proc, XML_GET_ALL_ELE MENT_ATTRS, is supposed to convert an XML
          file into a TCL list as follows:

          attr1 {val1} attr2 {val2} ... attrN {valN}

          This is the TCL code that does this:

          [TCL]
          set contents [read $fileID [file size ${fileName}.xml]]; close $fileID


          if {![string equal $switch -body]} {
          # ONLY DO THIS IF THE XML CONTENTS CONTAIN NO BODY - WILL UPGRADE AT
          A LATER DATE 11/24/2006 - PHIL
          global serverName
          if {![info exists serverName]} {
          global env
          source ./cgi_globals.tcl
          global serverName
          }

          if {[string length [info procs {URL_ENCODE}]] == 0} { source
          ./url_procs.tcl }; # INCLUDE NEW url_procs.tcl URL TCL LIBRARY
          if {[string length [info procs {IS_LIST}]] == 0} { source
          {./tcl_string_tool s.tcl} }; # INCLUDE THE TCL STRING LIBRARY CONTAINING
          PROC is_list IF !FOUND

          # BLOCK TO CREATE THE PHP SCRIPT TO PIPE INTO php.exe
          regsub -all {'} [XML_CLEANUP_ATT RIBUTE $contents] {\&#034;}
          phpEscapedConte nts
          regsub -all {"} $phpEscapedCont ents {\&quot;} phpEscapedConte nts
          set php {<? }
          append php [subst { if (@is_file("./functions.inc.p hp")) [format %c
          123] }]
          append php { require_once("./functions.inc.p hp"); }
          append php [subst { echo xml_to_tcl_list ("$phpEscapedCo ntents"); }]
          append php [subst {[format %c 125] }]
          append php { ?>}


          set cannotRunPHP [catch {exec "echo '$php' | php" >@stdout
          2>&stderr} errorMsg]
          if {$cannotRunPHP} {
          puts $errorMsg
          return {}
          } else {
          if {![IS_LIST $contentsList]} { set contentsList [split
          $contentsList] }
          if {[llength $contentsList] == 0} { lappend contentsList {?} }
          return [lrange $contentsList [expr {[lsearch -exact $contentsList
          "?"] + 1}] end]
          }

          }
          [/TCL]

          The command-line PHP should look like this:

          [PHP]
          <? if (@is_file("./functions.inc.p hp")) {
          require_once("./functions.inc.p hp"); echo
          xml_to_tcl_list ("$phpEscapedCo ntents"); } ?>
          [/PHP]

          Note that $phpEscapedCont ents is a TCL variable, not a PHP variable

          Ok, so this is what should happen:

          1) PHP function xml_to_tcl_list will convert the inputted escaped XML
          string into a TCL list
          2) I need to return that TCL list into $contentsList and return it so
          the proc returns a TCL list

          However, it never gets that far, here is the error I get:

          couldn't execute "echo '<? if (@is_file("./functions.inc.p hp")) {
          require_once("./functions.inc.p hp"); echo xml_to_tcl_list ("&lt;?xml
          version=&quot;1 .0&quot; encod": file name too long
          I am sure I am going at this the wrong way, but I can't understand the
          right way (TCL DOM parsing is far FAR beyond my ability to understand,
          I've read manuals, tutorials, books, to no avail: I simply don't
          understand how TCL can ever parse XML, I only understand simple PHP
          parsing XML)

          So at this point, all I want to do is convert an XML string into a TCL
          list, and the only way I know how to do it is via PHP, but I can't get
          all three languages to cooperate.
          If you showed us part of the XML there surely is a shorter way with one
          of the XML parsers for Tcl like tdom, but maybe your just confused by
          the difference between DOM and SAX style xml parsing.
          There is, but I've said this to nearly ever TCL and PHP developer on
          earth: I do not understand XML parsing at all, DOM, SAX, xQuery,
          X[whatever else], none of it makes sense to me and I cannot understand
          the books, tutorials and online guides (not to mention the tips from
          far smarter people than I) that has barraged me; all of it makes
          absolutely no sense to me.

          At this point the only way I can parse XML is using PHP, because that's
          literally the ONLY way I can do it, period!

          But if you want to see a sample of the XML I'm working with, here it
          is:

          <?xml version="1.0" encoding="utf-8" ?><trivia><entr y id="1101"
          triviaID="233" question="Who wrote &quot;Trilog y of Knowledge&quot; ?"
          answerID="1" correctAnswerID ="1" answer="Believe r"
          expDate="113963 4000"></entry><entry id="1102" triviaID="233"
          question="Who wrote &quot;Trilog y of Knowledge&quot; ?" answerID="2"
          correctAnswerID ="1" answer="Saviour Machine"
          expDate="113963 4000"></entry><entry id="1103" triviaID="233"
          question="Who wrote &quot;Trilog y of Knowledge&quot; ?" answerID="3"
          correctAnswerID ="1" answer="Seventh Avenue"
          expDate="113963 4000"></entry><entry id="1104" triviaID="233"
          question="Who wrote &quot;Trilog y of Knowledge&quot; ?" answerID="4"
          correctAnswerID ="1" answer="Inevita ble End"
          expDate="113963 4000"></entry><entry id="1105" triviaID="233"
          question="Who wrote &quot;Trilog y of Knowledge&quot; ?" answerID="5"
          correctAnswerID ="1" answer="No such song existed"
          expDate="113963 4000"></entry>

          Phil

          >
          Michael

          Comment

          • Bryan Oakley

            #6
            Re: TCL/PHP/XML problem: I need to convert an XML file into a TCLlist

            comp.lang.tcl wrote:
            Bryan Oakley wrote:
            >
            >>comp.lang.t cl wrote:
            >>
            >> set cannotRunPHP [catch {exec "echo '$php' | php" >@stdout
            >>>2>&stderr} errorMsg]
            >>
            >>You need to re-read the man page on exec. The first argument to exec is
            >>a filename that will be exec'd; it is not a command line. If you want to
            > run a command line you should exec a command line processor (/bin/sh,
            >>command.com , whatever).
            >>
            >>Since "php" is what you want to exec, and $php is what you want to send
            >>to its stdin, use "<<" (documented on the exec man page):
            >>
            > if {[catch {exec php << $php} result]} {
            > puts "error exec'ing php: $resulut"
            > } else {
            > puts "php result: $result"
            > }
            >
            >
            >
            Now I get this error:
            >
            couldn't execute "php": no such file or directory
            >
            When I try this:
            >
            [TCL]
            set cannotRunPHP [catch {exec exec php << $php >@stdout 2>&stderr}
            errorMsg]
            if {$cannotRunPHP} {
            puts $errorMsg
            return {}
            } else {
            if {![IS_LIST $contentsList]} { set contentsList [split
            $contentsList] }
            if {[llength $contentsList] == 0} { lappend contentsList {?} }
            return [lrange $contentsList [expr {[lsearch -exact $contentsList
            "?"] + 1}] end]
            }
            >
            [/TCL]
            >
            Wow this should only take an hour, it's taken 3 days so far!
            When you are learning a new language, it always takes much longer to
            solve problems. And in your case I'm afraid, you aren't taking much time
            to actually learn the language which is compounding the problem.

            The answer to your current problem is well documented in the exec man
            page, and on the exec pages on the tcler's wiki. The problem you are
            encountering is very simple: you are telling it to execute "php" but tcl
            can't find "php". The short answer is, you either have to explicitly
            tell the exec command which *file* to execute (eg: exec
            /usr/local/bin/php), or you have to make sure that the file you tell it
            exists somewhere within a directory defined in your PATH environment
            variable.

            Phil, I'm sorry you're having so many problems. You are correct that
            this probably should have taken only an hour or two for a seasoned
            programmer, but you are not a seasoned programmer and properly parsing
            XML is, generally speaking, a Hard Problem. At least, it's hard when you
            choose to not use proper XML parsing solutions and instead rely on a
            series of text transformations .

            Tcl is a very powerful language, but it doesn't have built-in facilities
            to parse XML. This is not a weakness of Tcl, it's just the way it is.
            The beauty of Tcl is, people can write extensions to the language to do
            just about anything, including parsing XML. Unfortunately, you are
            choosing not to take the time to learn how to use them.

            I'll again recommend trying the xml2list function documented here:
            http://mini.net/tcl/3919. I'm guessing that will work good enough for
            you. Fortunately, it appears you're working on a simply hobby
            application rather than a commercial application so we don't have to
            worry so much about robustness.

            Comment

            • comp.lang.tcl

              #7
              Re: TCL/PHP/XML problem: I need to convert an XML file into a TCL list


              Bryan Oakley wrote:
              comp.lang.tcl wrote:
              Bryan Oakley wrote:
              >comp.lang.tc l wrote:
              >
              > set cannotRunPHP [catch {exec "echo '$php' | php" >@stdout
              >>2>&stderr} errorMsg]
              >
              >You need to re-read the man page on exec. The first argument to exec is
              >a filename that will be exec'd; it is not a command line. If you want to
              run a command line you should exec a command line processor (/bin/sh,
              >command.com, whatever).
              >
              >Since "php" is what you want to exec, and $php is what you want to send
              >to its stdin, use "<<" (documented on the exec man page):
              >
              if {[catch {exec php << $php} result]} {
              puts "error exec'ing php: $resulut"
              } else {
              puts "php result: $result"
              }


              Now I get this error:

              couldn't execute "php": no such file or directory

              When I try this:

              [TCL]
              set cannotRunPHP [catch {exec exec php << $php >@stdout 2>&stderr}
              errorMsg]
              if {$cannotRunPHP} {
              puts $errorMsg
              return {}
              } else {
              if {![IS_LIST $contentsList]} { set contentsList [split
              $contentsList] }
              if {[llength $contentsList] == 0} { lappend contentsList {?} }
              return [lrange $contentsList [expr {[lsearch -exact $contentsList
              "?"] + 1}] end]
              }

              [/TCL]

              Wow this should only take an hour, it's taken 3 days so far!
              >
              When you are learning a new language, it always takes much longer to
              solve problems. And in your case I'm afraid, you aren't taking much time
              to actually learn the language which is compounding the problem.
              >
              The answer to your current problem is well documented in the exec man
              page, and on the exec pages on the tcler's wiki. The problem you are
              encountering is very simple: you are telling it to execute "php" but tcl
              can't find "php". The short answer is, you either have to explicitly
              tell the exec command which *file* to execute (eg: exec
              /usr/local/bin/php), or you have to make sure that the file you tell it
              exists somewhere within a directory defined in your PATH environment
              variable.
              >
              Phil, I'm sorry you're having so many problems. You are correct that
              this probably should have taken only an hour or two for a seasoned
              programmer, but you are not a seasoned programmer and properly parsing
              XML is, generally speaking, a Hard Problem. At least, it's hard when you
              choose to not use proper XML parsing solutions and instead rely on a
              series of text transformations .
              >
              Bryan, you're going to die when you hear this: I have 9+ years web
              development experience, been doing TCL for about 7 years (mostly via
              Vignette), have 5 certifications including a master PHP certification..
              and I have no clue how to do any of this. Thus I am a seasoned
              programmer (though honestly nobody here will ever recognize or believe
              that)

              Tcl is a very powerful language, but it doesn't have built-in facilities
              to parse XML. This is not a weakness of Tcl, it's just the way it is.
              The beauty of Tcl is, people can write extensions to the language to do
              just about anything, including parsing XML. Unfortunately, you are
              choosing not to take the time to learn how to use them.
              I've said this over and over again: it's not that I won't take the
              time, it's that I CANNOT do it because I have a learning disability
              called Attention Deficit Disorder which psychologically does not permit
              me to take the time to learn it. I am unable to understand man pages
              like you and Cameron and other can do in your sleep. I read the Tcl
              extensions online but don't understand them. The pages are in
              Hungarian to me. I can't fathom them, I don't know how to install
              them, manipulate them, use them, anything. If people could show me a
              very very simple way of parsing this:

              <?xml version="1.0" encoding="utf-8" ?><trivia><entr y id="1101"
              triviaID="233" question="Who wrote &quot;Trilog y of Knowledge&quot; ?"
              answerID="1" correctAnswerID ="1" answer="Believe r"
              expDate="113963 4000"></entry><entry id="1102" triviaID="233"
              question="Who wrote &quot;Trilog y of Knowledge&quot; ?" answerID="2"
              correctAnswerID ="1" answer="Saviour Machine"
              expDate="113963 4000"></entry><entry id="1103" triviaID="233"
              question="Who wrote &quot;Trilog y of Knowledge&quot; ?" answerID="3"
              correctAnswerID ="1" answer="Seventh Avenue"
              expDate="113963 4000"></entry><entry id="1104" triviaID="233"
              question="Who wrote &quot;Trilog y of Knowledge&quot; ?" answerID="4"
              correctAnswerID ="1" answer="Inevita ble End"
              expDate="113963 4000"></entry><entry id="1105" triviaID="233"
              question="Who wrote &quot;Trilog y of Knowledge&quot; ?" answerID="5"
              correctAnswerID ="1" answer="No such song existed"
              expDate="113963 4000"></entry></trivia>

              Into this:

              id 1101 triviaID 233 question {Who wrote &quot;Trilog y of
              Knowledge&quot; ?} answerID 1 correctAnswerID 1 answer Believer expDate
              113963500

              [etc]

              Then that would do it, but I guess the problem is that I can't explain
              my problem, nor my disabilities, well enough for the programming
              community to fathom my dilemma.

              Phil
              >
              I'll again recommend trying the xml2list function documented here:
              http://mini.net/tcl/3919. I'm guessing that will work good enough for
              you. Fortunately, it appears you're working on a simply hobby
              application rather than a commercial application so we don't have to
              worry so much about robustness.

              Comment

              • Bryan Oakley

                #8
                Re: TCL/PHP/XML problem: I need to convert an XML file into a TCLlist

                comp.lang.tcl wrote:
                ... I do not understand XML parsing at all, DOM, SAX, xQuery,
                X[whatever else], none of it makes sense to me and I cannot understand
                the books, tutorials and online guides (not to mention the tips from
                far smarter people than I) that has barraged me; all of it makes
                absolutely no sense to me.
                >
                At this point the only way I can parse XML is using PHP, because that's
                literally the ONLY way I can do it, period!
                >
                But if you want to see a sample of the XML I'm working with, here it
                is:
                >
                <?xml version="1.0" encoding="utf-8" ?><trivia><entr y id="1101"
                triviaID="233" question="Who wrote &quot;Trilog y of Knowledge&quot; ?"
                answerID="1" correctAnswerID ="1" answer="Believe r"
                expDate="113963 4000"></entry><entry id="1102" triviaID="233"
                question="Who wrote &quot;Trilog y of Knowledge&quot; ?" answerID="2"
                correctAnswerID ="1" answer="Saviour Machine"
                expDate="113963 4000"></entry><entry id="1103" triviaID="233"
                question="Who wrote &quot;Trilog y of Knowledge&quot; ?" answerID="3"
                correctAnswerID ="1" answer="Seventh Avenue"
                expDate="113963 4000"></entry><entry id="1104" triviaID="233"
                question="Who wrote &quot;Trilog y of Knowledge&quot; ?" answerID="4"
                correctAnswerID ="1" answer="Inevita ble End"
                expDate="113963 4000"></entry><entry id="1105" triviaID="233"
                question="Who wrote &quot;Trilog y of Knowledge&quot; ?" answerID="5"
                correctAnswerID ="1" answer="No such song existed"
                expDate="113963 4000"></entry>
                That data is mal-formed XML. For example, you are missing the closing
                </triviatag.

                Here's a solution that works with the above data. I've mentioned the
                "xml2list" proc a couple of times, but with the sample data I see your
                data will need a little extra pre-processing.

                Step 1: copy the proc "xml2list" from this page: http://mini.net/tcl/3919

                Second, enter the following, which is taking the above data verbatim and
                storing it in a variable:

                set data {<?xml version="1.0" encoding="utf-8" ?><trivia><entr y
                id="1101"
                triviaID="233" question="Who wrote &quot;Trilog y of Knowledge&quot; ?"
                answerID="1" correctAnswerID ="1" answer="Believe r"
                expDate="113963 4000"></entry><entry id="1102" triviaID="233"
                question="Who wrote &quot;Trilog y of Knowledge&quot; ?" answerID="2"
                correctAnswerID ="1" answer="Saviour Machine"
                expDate="113963 4000"></entry><entry id="1103" triviaID="233"
                question="Who wrote &quot;Trilog y of Knowledge&quot; ?" answerID="3"
                correctAnswerID ="1" answer="Seventh Avenue"
                expDate="113963 4000"></entry><entry id="1104" triviaID="233"
                question="Who wrote &quot;Trilog y of Knowledge&quot; ?" answerID="4"
                correctAnswerID ="1" answer="Inevita ble End"
                expDate="113963 4000"></entry><entry id="1105" triviaID="233"
                question="Who wrote &quot;Trilog y of Knowledge&quot; ?" answerID="5"
                correctAnswerID ="1" answer="No such song existed"
                expDate="113963 4000"></entry>}

                Your data is missing an ending </triviatag, so we have to add it for
                this specific example. I don't know if this is a problem you'll have to
                solve with your full dataset. Also, the xml2list proc doesn't like the
                leading <?xml...stuff . So, let's modify your data:

                # remove the leading <?xml...?data
                regexp {<\?.*?\?>(.*$) } $data -- data

                # add a trailing </triviawhich is missing from
                # the sample data
                set data "$data</trivia>"

                And now, convert it to a list and print it out:

                set result [xml2list $data]
                puts $result

                If you didn't introduce any typos, you'll get the following output:

                trivia {} {{entry {id 1101 triviaID 233 question {Who wrote
                &quot;Trilog y of Knowledge&quot; ?} answerID 1 correctAnswerID 1 answer
                Believer expDate 1139634000} {}} {entry {id 1102 triviaID 233 question
                {Who wrote &quot;Trilog y of Knowledge&quot; ?} answerID 2 correctAnswerID
                1 answer {Saviour Machine} expDate 1139634000} {}} {entry {id 1103
                triviaID 233 question {Who wrote &quot;Trilog y of Knowledge&quot; ?}
                answerID 3 correctAnswerID 1 answer {Seventh Avenue} expDate 1139634000}
                {}} {entry {id 1104 triviaID 233 question {Who wrote &quot;Trilog y of
                Knowledge&quot; ?} answerID 4 correctAnswerID 1 answer {Inevitable End}
                expDate 1139634000} {}} {entry {id 1105 triviaID 233 question {Who wrote
                &quot;Trilog y of Knowledge&quot; ?} answerID 5 correctAnswerID 1 answer
                {No such song existed} expDate 1139634000} {}}}

                The above is a valid tcl list that you can now process with normal tcl
                list-handling commands. Do *not* process this list with string
                transformations (such as converting &quot; to a quote). If you do, you
                run the risk of breaking it's list-ness. Instead, loop over the data and
                do the conversion as a final step on a element-by-element basis.

                Does this help? It's not robust; the xml2list assumes you have proper
                xml with a balanced set of tags (or in the specific case in this
                message, with a missing </triviatag). Hopefully, though, it will at
                least get you started.

                Comment

                • comp.lang.tcl

                  #9
                  Re: TCL/PHP/XML problem: I need to convert an XML file into a TCL list


                  Bryan Oakley wrote:
                  comp.lang.tcl wrote:
                  ... I do not understand XML parsing at all, DOM, SAX, xQuery,
                  X[whatever else], none of it makes sense to me and I cannot understand
                  the books, tutorials and online guides (not to mention the tips from
                  far smarter people than I) that has barraged me; all of it makes
                  absolutely no sense to me.

                  At this point the only way I can parse XML is using PHP, because that's
                  literally the ONLY way I can do it, period!

                  But if you want to see a sample of the XML I'm working with, here it
                  is:

                  <?xml version="1.0" encoding="utf-8" ?><trivia><entr y id="1101"
                  triviaID="233" question="Who wrote &quot;Trilog y of Knowledge&quot; ?"
                  answerID="1" correctAnswerID ="1" answer="Believe r"
                  expDate="113963 4000"></entry><entry id="1102" triviaID="233"
                  question="Who wrote &quot;Trilog y of Knowledge&quot; ?" answerID="2"
                  correctAnswerID ="1" answer="Saviour Machine"
                  expDate="113963 4000"></entry><entry id="1103" triviaID="233"
                  question="Who wrote &quot;Trilog y of Knowledge&quot; ?" answerID="3"
                  correctAnswerID ="1" answer="Seventh Avenue"
                  expDate="113963 4000"></entry><entry id="1104" triviaID="233"
                  question="Who wrote &quot;Trilog y of Knowledge&quot; ?" answerID="4"
                  correctAnswerID ="1" answer="Inevita ble End"
                  expDate="113963 4000"></entry><entry id="1105" triviaID="233"
                  question="Who wrote &quot;Trilog y of Knowledge&quot; ?" answerID="5"
                  correctAnswerID ="1" answer="No such song existed"
                  expDate="113963 4000"></entry>
                  >
                  That data is mal-formed XML. For example, you are missing the closing
                  </triviatag.
                  >
                  Here's a solution that works with the above data. I've mentioned the
                  "xml2list" proc a couple of times, but with the sample data I see your
                  data will need a little extra pre-processing.
                  >
                  Step 1: copy the proc "xml2list" from this page: http://mini.net/tcl/3919
                  Ok done
                  >
                  Second, enter the following, which is taking the above data verbatim and
                  storing it in a variable:
                  >
                  set data {<?xml version="1.0" encoding="utf-8" ?><trivia><entr y
                  id="1101"
                  triviaID="233" question="Who wrote &quot;Trilog y of Knowledge&quot; ?"
                  answerID="1" correctAnswerID ="1" answer="Believe r"
                  expDate="113963 4000"></entry><entry id="1102" triviaID="233"
                  question="Who wrote &quot;Trilog y of Knowledge&quot; ?" answerID="2"
                  correctAnswerID ="1" answer="Saviour Machine"
                  expDate="113963 4000"></entry><entry id="1103" triviaID="233"
                  question="Who wrote &quot;Trilog y of Knowledge&quot; ?" answerID="3"
                  correctAnswerID ="1" answer="Seventh Avenue"
                  expDate="113963 4000"></entry><entry id="1104" triviaID="233"
                  question="Who wrote &quot;Trilog y of Knowledge&quot; ?" answerID="4"
                  correctAnswerID ="1" answer="Inevita ble End"
                  expDate="113963 4000"></entry><entry id="1105" triviaID="233"
                  question="Who wrote &quot;Trilog y of Knowledge&quot; ?" answerID="5"
                  correctAnswerID ="1" answer="No such song existed"
                  expDate="113963 4000"></entry>}
                  >
                  Your data is missing an ending </triviatag, so we have to add it for
                  this specific example. I don't know if this is a problem you'll have to
                  solve with your full dataset. Also, the xml2list proc doesn't like the
                  leading <?xml...stuff . So, let's modify your data:

                  Sorry that was my fault, I left off the </triviatag when I copied and
                  pasted onto here. The closing tags do exist in all of my XML files
                  >
                  # remove the leading <?xml...?data
                  regexp {<\?.*?\?>(.*$) } $data -- data
                  >
                  # add a trailing </triviawhich is missing from
                  # the sample data
                  set data "$data</trivia>"
                  >
                  And now, convert it to a list and print it out:
                  >
                  set result [xml2list $data]
                  puts $result
                  >
                  If you didn't introduce any typos, you'll get the following output:

                  No I don't, I get the following error, spawned from within xml2list:

                  unmatched open quote in list while executing "lindex $item 0"
                  ("default" arm line 2) invoked from within "switch -regexp -- $item {
                  ^# {append res "{[lrange $item 0 end]} " ; #text item} ^/ { regexp
                  {/(.+)} $item -..." (procedure "xml2list" line 9)

                  This is what I did:

                  [TCL]
                  # USE xml2list PROC WITHIN THIS LIBRARY AS YOUR DEFAULT MEANS OF
                  PARSING XML INTO TCL LIST
                  if {![string equal $switch -body] && [string length [info procs
                  {xml2list}]] 0} {
                  regexp {<\?.*?\?>(.*$) } $contents -- contents
                  return [xml2list $contents]
                  }
                  [/TCL]
                  >
                  trivia {} {{entry {id 1101 triviaID 233 question {Who wrote
                  &quot;Trilog y of Knowledge&quot; ?} answerID 1 correctAnswerID 1 answer
                  Believer expDate 1139634000} {}} {entry {id 1102 triviaID 233 question
                  {Who wrote &quot;Trilog y of Knowledge&quot; ?} answerID 2 correctAnswerID
                  1 answer {Saviour Machine} expDate 1139634000} {}} {entry {id 1103
                  triviaID 233 question {Who wrote &quot;Trilog y of Knowledge&quot; ?}
                  answerID 3 correctAnswerID 1 answer {Seventh Avenue} expDate 1139634000}
                  {}} {entry {id 1104 triviaID 233 question {Who wrote &quot;Trilog y of
                  Knowledge&quot; ?} answerID 4 correctAnswerID 1 answer {Inevitable End}
                  expDate 1139634000} {}} {entry {id 1105 triviaID 233 question {Who wrote
                  &quot;Trilog y of Knowledge&quot; ?} answerID 5 correctAnswerID 1 answer
                  {No such song existed} expDate 1139634000} {}}}
                  >
                  The above is a valid tcl list that you can now process with normal tcl
                  list-handling commands. Do *not* process this list with string
                  transformations (such as converting &quot; to a quote). If you do, you
                  run the risk of breaking it's list-ness. Instead, loop over the data and
                  do the conversion as a final step on a element-by-element basis.
                  >
                  Does this help? It's not robust; the xml2list assumes you have proper
                  xml with a balanced set of tags (or in the specific case in this
                  message, with a missing </triviatag). Hopefully, though, it will at
                  least get you started.
                  Like I said, I do have balanced XML (just didn't produce it here tis
                  all), but xml2list produces errors when I try to read it

                  Phil

                  Comment

                  • Darren New

                    #10
                    Re: TCL/PHP/XML problem: I need to convert an XML file into a TCLlist

                    comp.lang.tcl wrote:
                    id 1101 triviaID 233 question {Who wrote &quot;Trilog y of
                    Knowledge&quot; ?} answerID 1 correctAnswerID 1 answer Believer expDate
                    113963500
                    Take the zip file I posted. Install it. Call

                    set x [::xsxp::parse $myXml]
                    return [lindex [lindex $x 2] 1]

                    (I think that's the right path thru the list to the attributes of the
                    first child element. Otherwise, print out the list and see which
                    elements you have to grab.)

                    --
                    Darren New / San Diego, CA, USA (PST)
                    Scruffitarianis m - Where T-shirt, jeans,
                    and a three-day beard are "Sunday Best."

                    Comment

                    • comp.lang.tcl

                      #11
                      Re: TCL/PHP/XML problem: I need to convert an XML file into a TCL list


                      Darren New wrote:
                      comp.lang.tcl wrote:
                      id 1101 triviaID 233 question {Who wrote &quot;Trilog y of
                      Knowledge&quot; ?} answerID 1 correctAnswerID 1 answer Believer expDate
                      113963500
                      >
                      Take the zip file I posted. Install it. Call
                      Sorry there is no attachment, no zip file of any kind.

                      Phil

                      >
                      set x [::xsxp::parse $myXml]
                      return [lindex [lindex $x 2] 1]
                      >
                      (I think that's the right path thru the list to the attributes of the
                      first child element. Otherwise, print out the list and see which
                      elements you have to grab.)
                      >
                      --
                      Darren New / San Diego, CA, USA (PST)
                      Scruffitarianis m - Where T-shirt, jeans,
                      and a three-day beard are "Sunday Best."

                      Comment

                      • Cameron Laird

                        #12
                        Re: TCL/PHP/XML problem: I need to convert an XML file into a TCLlist

                        In article <tt0ah.6413$yf7 .3870@newssvr21 .news.prodigy.n et>,
                        Bryan Oakley <oakley@bardo.c learlight.comwr ote:

                        Comment

                        • comp.lang.tcl

                          #13
                          Re: TCL/PHP/XML problem: I need to convert an XML file into a TCL list


                          Cameron Laird wrote:
                          In article <tt0ah.6413$yf7 .3870@newssvr21 .news.prodigy.n et>,
                          Bryan Oakley <oakley@bardo.c learlight.comwr ote:
                          .
                          .
                          .
                          # add a trailing </triviawhich is missing from
                          # the sample data
                          set data "$data</trivia>"
                          .
                          .
                          .
                          Alternatively, as Bryan knows, but doubtless eschewed for
                          pedagogic reasons,
                          >
                          append data </trivia>
                          Why? The XML files are not being auto-spawned by TCL or anything else!

                          Comment

                          • Gerald W. Lester

                            #14
                            Re: TCL/PHP/XML problem: I need to convert an XML file into a TCLlist

                            comp.lang.tcl wrote:
                            My TCL proc, XML_GET_ALL_ELE MENT_ATTRS, is supposed to convert an XML
                            file into a TCL list as follows:
                            >
                            attr1 {val1} attr2 {val2} ... attrN {valN}
                            First off, sorry to everyone else for the long post, but I got tired reading
                            these threads.

                            Second off, the following does not return *EXACTLY* what you asked for it
                            returns more, but you should be able to convert it to what you ask for.

                            ##
                            ## A node will be the following list of name value pairs:
                            ## NAME nodeName
                            ## TEXT text
                            ## ATTRIBUTES attributeNameVa lueList
                            ## CHILDREN childNodeList
                            ##
                            package require tdom

                            set xml {<?xml version="1.0" encoding="utf-8" ?>
                            <trivia>
                            <entry id="1101" triviaID="233" question="Who wrote &quot;Trilog y
                            of Knowledge&quot; ?" answerID="1" correctAnswerID ="1" answer="Believe r"
                            expDate="113963 4000"></entry>
                            <entry id="1102" triviaID="233" question="Who wrote &quot;Trilog y
                            of Knowledge&quot; ?" answerID="2" correctAnswerID ="1" answer="Saviour
                            Machine" expDate="113963 4000"</entry>
                            <entry id="1103" triviaID="233" question="Who wrote &quot;Trilog y
                            of Knowledge&quot; ?" answerID="3" correctAnswerID ="1" answer="Seventh
                            Avenue" expDate="113963 4000"></entry>
                            <entry id="1104" triviaID="233" question="Who wrote &quot;Trilog y
                            of Knowledge&quot; ?" answerID="4" correctAnswerID ="1" answer="Inevita ble
                            End" expDate="113963 4000"></entry>
                            <entry id="1105" triviaID="233" question="Who wrote &quot;Trilog y
                            of Knowledge&quot; ?" answerID="5" correctAnswerID ="1" answer="No such song
                            existed" expDate="113963 4000"></entry>
                            </trivia>
                            }

                            ##
                            ## Convert a node to a list
                            ##
                            proc NodeToList {node} {
                            ##
                            ## Get the name and text value of the node
                            ##
                            set name [$node nodeName]
                            set text [$node text]

                            ##
                            ## Get the attributes of the node
                            ##
                            set attrList {}
                            foreach attribute [$node attributes] {
                            lappend attrList $attribute [$node getAttribute $attribute]
                            }

                            ##
                            ## Get the children of the node
                            ##
                            set childrenList {}
                            foreach child [$node childNodes] {
                            if {![string equal [$child nodeType] TEXT_NODE]} then {
                            lappend childrenList [NodeToList $child]
                            }
                            }

                            ##
                            ## All done so return the list representing this subtree
                            ##
                            return[list NAME $name TEXT $text ATTRIBUTES $attrList CHILDREN
                            $childrenList]
                            }

                            ##
                            ## Convert the XML to a DOM tree
                            ##
                            dom parse $xml doc

                            ##
                            ## No get the root element
                            ##
                            $doc documentElement root

                            ##
                            ## Convert the tree to a list
                            ##
                            set results [NodeToList $root]
                            $doc delete


                            --
                            +--------------------------------+---------------------------------------+
                            | Gerald W. Lester |
                            |"The man who fights for his ideals is the man who is alive." - Cervantes|
                            +------------------------------------------------------------------------+

                            Comment

                            • comp.lang.tcl

                              #15
                              Re: TCL/PHP/XML problem: I need to convert an XML file into a TCL list


                              Gerald W. Lester wrote:
                              comp.lang.tcl wrote:
                              My TCL proc, XML_GET_ALL_ELE MENT_ATTRS, is supposed to convert an XML
                              file into a TCL list as follows:

                              attr1 {val1} attr2 {val2} ... attrN {valN}
                              >
                              First off, sorry to everyone else for the long post, but I got tired reading
                              these threads.
                              >
                              Second off, the following does not return *EXACTLY* what you asked for it
                              returns more, but you should be able to convert it to what you ask for.
                              >
                              ##
                              ## A node will be the following list of name value pairs:
                              ## NAME nodeName
                              ## TEXT text
                              ## ATTRIBUTES attributeNameVa lueList
                              ## CHILDREN childNodeList
                              ##
                              package require tdom
                              Sorry I am unable to figure out how to install and use tdom. I tried
                              this

                              lappend auto_path /home/ppowell/web/cgi-bin
                              package require tdom

                              at the top of xml_procs.tcl, to no avail, I get the following error:

                              can't find package tdom while executing "package require tdom" (file
                              "xml_procs. tcl" line 2)

                              I was able to download and install tdom, or I guess install it:

                              1) I downloaded it

                              2) I unzipped it

                              3) everything's in /home/ppowell/web/cgi-bin/tDom-0.8.0

                              4) I was not allowed to do ../configure, much less "make" nor "make
                              install", "../configure" not found

                              Phil
                              >
                              set xml {<?xml version="1.0" encoding="utf-8" ?>
                              <trivia>
                              <entry id="1101" triviaID="233" question="Who wrote &quot;Trilog y
                              of Knowledge&quot; ?" answerID="1" correctAnswerID ="1" answer="Believe r"
                              expDate="113963 4000"></entry>
                              <entry id="1102" triviaID="233" question="Who wrote &quot;Trilog y
                              of Knowledge&quot; ?" answerID="2" correctAnswerID ="1" answer="Saviour
                              Machine" expDate="113963 4000"</entry>
                              <entry id="1103" triviaID="233" question="Who wrote &quot;Trilog y
                              of Knowledge&quot; ?" answerID="3" correctAnswerID ="1" answer="Seventh
                              Avenue" expDate="113963 4000"></entry>
                              <entry id="1104" triviaID="233" question="Who wrote &quot;Trilog y
                              of Knowledge&quot; ?" answerID="4" correctAnswerID ="1" answer="Inevita ble
                              End" expDate="113963 4000"></entry>
                              <entry id="1105" triviaID="233" question="Who wrote &quot;Trilog y
                              of Knowledge&quot; ?" answerID="5" correctAnswerID ="1" answer="No such song
                              existed" expDate="113963 4000"></entry>
                              </trivia>
                              }
                              >
                              ##
                              ## Convert a node to a list
                              ##
                              proc NodeToList {node} {
                              ##
                              ## Get the name and text value of the node
                              ##
                              set name [$node nodeName]
                              set text [$node text]
                              >
                              ##
                              ## Get the attributes of the node
                              ##
                              set attrList {}
                              foreach attribute [$node attributes] {
                              lappend attrList $attribute [$node getAttribute $attribute]
                              }
                              >
                              ##
                              ## Get the children of the node
                              ##
                              set childrenList {}
                              foreach child [$node childNodes] {
                              if {![string equal [$child nodeType] TEXT_NODE]} then {
                              lappend childrenList [NodeToList $child]
                              }
                              }
                              >
                              ##
                              ## All done so return the list representing this subtree
                              ##
                              return[list NAME $name TEXT $text ATTRIBUTES $attrList CHILDREN
                              $childrenList]
                              }
                              >
                              ##
                              ## Convert the XML to a DOM tree
                              ##
                              dom parse $xml doc
                              >
                              ##
                              ## No get the root element
                              ##
                              $doc documentElement root
                              >
                              ##
                              ## Convert the tree to a list
                              ##
                              set results [NodeToList $root]
                              $doc delete
                              >
                              >
                              --
                              +--------------------------------+---------------------------------------+
                              | Gerald W. Lester |
                              |"The man who fights for his ideals is the man who is alive." - Cervantes|
                              +------------------------------------------------------------------------+

                              Comment

                              Working...