Hi,

I have the following problem. I want to import data into my site via PHP
XML Parser, but I also want to include php string variables into
the .xml file and when the xml file is parsed the variables are replaced
whit the corresponding string.

So far all my attempts have been unsuccessful.

Here is the parser i have used

<?php


class XMLParser {
   var $filename;
   var $xml;
   var $data;
  
   function XMLParser($xml_file)
   {
       $this->filename = $xml_file;
       $this->xml = xml_parser_create();
       xml_set_object($this->xml, $this);
       xml_set_element_handler($this->xml, 'startHandler',
'endHandler');
       xml_set_character_data_handler($this->xml, 'dataHandler');
       $this->parse($xml_file);
   }
  
   function parse($xml_file)
   {
       if (!($fp = fopen($xml_file, 'r'))) {
             die('Cannot open XML data file: '.$xml_file);
               return false;
       }

       $bytes_to_parse = 512;

       while ($data = fread($fp, $bytes_to_parse)) {
           $parse = xml_parse($this->xml, $data, feof($fp));
          
           if (!$parse) {
               die(sprintf("XML error: %s at line %d",
                   xml_error_string(xml_get_error_code($this->xml)),
                       xml_get_current_line_number($this->xml)));
                       xml_parser_free($this->xml
                     );
           }
       }

       return true;
   }
  
   function startHandler($parser, $name, $attributes)
   {
       $data['name'] = $name;
       if ($attributes) { $data['attributes'] = $attributes; }
       $this->data[] = $data;
   }

   function dataHandler($parser, $data)
   {
       if ($data = trim($data)) {
           $index = count($this->data) - 1;
           // begin multi-line bug fix (use the .= operator)
           $this->data[$index]['content'] .= $data;
           // end multi-line bug fix
       }
   }

   function endHandler($parser, $name)
   {
       if (count($this->data) > 1) {
           $data = array_pop($this->data);
           $index = count($this->data) - 1;
           $this->data[$index]['child'][] = $data;
       }
   }
}



$url = 'data.xml';
$myFile = new XMLParser($url);

echo "<PRE>";
print_r($myFile->data);
echo "</PRE>";

$foo3 = "foo3";
?>

here is a sample XML file

<?xml version='1.0'?>
<!DOCTYPE chapter SYSTEM "/just/a/test.dtd" [
<!ENTITY plainEntity "FOO entity">
<!ENTITY systemEntity SYSTEM "xmltest2.xml">
]>
<document>

        <data>
                foo1
        </data>
        <data>
                foo2
        </data>
        <data>
                <?php echo $foo3; ?>
        </data>
        <data>
                foo4
        </data>
        <data>
                foo5
        </data>
</document>

and what I get is:

Array
(
    [0] => Array
        (
            [name] => DOCUMENT
            [child] => Array
                (
                    [0] => Array
                        (
                            [name] => DATA
                            [content] => foo1
                        )

                    [1] => Array
                        (
                            [name] => DATA
                            [content] => foo2
                        )

                    [2] => Array
                        (
                            [name] => DATA
                        )

                    [3] => Array
                        (
                            [name] => DATA
                            [content] => foo4
                        )

                    [4] => Array
                        (
                            [name] => DATA
                            [content] => foo5
                        )

                )

        )

)

So I want $myFile->data[0][child][2][content] to be equal to $foo3



   

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to