<?
           error_reporting(E_ALL & ~E_NOTICE);
            if (@is_file('functions.inc.php')) require_once('functions.inc.php');
            $xml = preg_replace('/(>)[\n\r\\s\t]+(<)/', '$1$2', @file_get_contents('php://stdin'));
            $parser = @xml_parser_create();
            @xml_parse_into_struct($parser, $xml, $xmlArray, $tags);
            @xml_parser_free($parser);
            for ($i = 1; $i < @sizeof($xmlArray) - 1; $i++) {
             foreach ($xmlArray[$i]['attributes'] as $attr => $val) {
              foreach (array(&$attr, &$val) as $field) {
               $field = str_replace('{', '&#123;', str_replace('}', '&#125;', $field));
               $tclList .= (preg_match('/[\s\t]+/', $field)) ? '{' . $field . '} ' : "$field ";
              }
             }
            }
            echo trim($tclList);
?>

This converts XML that is inputted from stdin (a TCL script will input the XML-read contents into the PHP script) into a TCL list.

However, the PHP script in question dies if stdin is too long. I don't exactly know the "maximum length" it could be, if there is one, but I do know that this scirpt will work with this:

php -q /home/ppowell/web/blah.php
<?xml version="1.0" encoding="utf-8" ?><usa><state id="1" abbrev="AL" name="Alabama"></state></usa>

But will fail with this:

php -q /home/ppowell/web/blah.php
<?xml version="1.0" encoding="utf-8" ?><usa><state id="1" abbrev="AL" name="Alabama"></state><state id="2" abbrev="AK" name="Alaska"></state><state id="3" abbrev="AZ" name="Arizona"></state><state id="4" abbrev="AR" name="Arkansas"></state><state id="5" abbrev="CA" name="California"></state><state id="6" abbrev="CO" name="Colorado"></state><state id="7" abbrev="CT" name="Connecticut"></state><state id="8" abbrev="DE" name="Delaware"></state><state id="9" abbrev="DC" name="District of Columbia"></state></usa>

[nothing is returned, and verifying: $xml = ">"]

Is there an actual size limit or is there something else wrong? Feel free to play with this script as much as you like.

Thanx
Phil

    There isn't a size limit on stdin (the "standard input" file is the conventional way of piping data into a program at runtime; if it's not taking in the output from some other program, it's redirected from the keyboard device). Passing multimegabyte files through stdin is not uncommon at all (building the PHP source tarball would involve passing a >43MB .tar file to gzip via stdin).

    What I get when running your code (starting it at the command line and then pasting the sample XML and hitting EOF) is

    Notice: Undefined variable: tclList in C:\test.php on line 10
    ID 1 ABBREV AL NAME Alabama ID 2 ABBREV AK NAME Alaska ID 3 ABBREV AZ NAME Arizo
    na ID 4 ABBREV AR NAME Arkansas ID 5 ABBREV CA NAME California ID 6 ABBREV CO NA
    ME Colorado ID 7 ABBREV CT NAME Connecticut ID 8 ABBREV DE NAME Delaware ID 9 AB
    BREV DC NAME {District of Columbia}
    

    The problem may be that your shell is limiting line length (all that XML is on a single line; if it's truncated by the shell the result will no longer be well-formed and hence won't parse). Put some linebreaks in there so that it spans more than one line.

    And/or try putting that XML in a file and then piping it into the script, see if that makes a difference

    > cat sample.xml | php -q /home/ppowell/web/blah.php
      Write a Reply...