Page 1 of 1

unable to parse XML with xml_parse_into_struct

Posted: Tue Aug 16, 2005 10:05 am
by pospi
Hi there all!

I'm having big troubles parsing XML the PHP4.x way with xml_parser_create() and xml_parse_into_struct(). Everything was working perfectly fine until one day we had some server problems and now all my XML dependant pages are simply left blank.

I've harassed my server admins a number of times but they seem unable to find the problem, so I was wondering if anyone here had any ideas what might be causing it.

xml_parser_create() still seems like it might be working, because when debugged it is still returning a valid resource ID. xml_parse_into_struct(), however, is always returning a 0 for failure.

any help very much appreciated

cheers
pospi

Posted: Tue Aug 16, 2005 10:08 am
by timvw
What do you find in the error logs?

Prepending the following to your script can be useful:

Code: Select all

ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);
You might want to check if the XML is still valid..

Posted: Tue Aug 16, 2005 10:57 am
by pospi
Thanks for the prompt reply (:

Double checked my XML again and it's still definitely valid.
I also tried adding your ini_set's to the top of my code, however only error_reporting was actually doing something. Shouldnt matter though since display_errors is true by default. In any case, nothing extra showed up at all.

The only errors in my error log are the server whining about missing favicons, so I guess there's nothing indicative of the problem there either.

Any other ideas? (:

edit:
If there's any alternative methods of getting XML data into an array structure then i suppose rewriting a few pages might be the easiest way. Does anyone know of other simple ways to accomplish this with php4?

Posted: Wed Aug 17, 2005 4:36 am
by pospi
I tried rewriting my code to use xml_parse() rather than xml_parse_into_struct() so that I could step through the data and find out specifically what's wrong.

Using this method was also a failure, however on my local pc the data was parsed correctly whilst on my server it would always return a syntax error at line 1.

Could there be any reason why the same XML document would be able to be parsed correctly on one machine and not on another?

cheers

Posted: Wed Aug 17, 2005 5:44 am
by patrikG
This is a script I'm using to parse XML-files. It uses http://uk.php.net/xml_set_element_handler which makes things a bit easier. Returns a nicely formatted array. Modify it to your needs.

Code: Select all

/**
* XML parser package
*/
/**
* XML parser class
* @access  public
* @author   PatrikG (modified version of a php.net usernote)
* @version  v 0.1 17.03.2003
*/
class parser{
var $att;
var $id;
var $title;
var $content;
var $index=-1;
var $xml_parser;
var $tagname;
var $node_number;

/**
* creates an XML-parser & XML-parser event handler, reads the XML-file $file and parses it
*
* @access      public
* @param       object $db reference to database object
* @param        file $file XML-file
* @param        string $base_element The element from which parsing will start (tag $base_element itself will be ignored)
* @param        string $id_element The element which will be looped through
* @return      void
*/
function parser($file,$base_element,$id_element){
    $this->base_element =   $base_element;
    $this->id_element   =   $id_element;
    $this->xml_parser = xml_parser_create();
    xml_set_object($this->xml_parser,$this);
    xml_set_element_handler($this->xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($this->xml_parser, 'elementContent');
    if (!$fp = fopen($file, "r")) {
        $this->error[date("dMYHis")]=   "Could not open XML-file ".$file;
        return false;
    }

    while ($data = fread($fp, 4096)) {
        $data=eregi_replace(">"."[[]]+"."<","><",$data);
        if (!xml_parse($this->xml_parser, $data, feof($fp))) {
            die(sprintf("XML error: %s at line %d",
                xml_error_string(xml_get_error_code($this->xml_parser)),
                xml_get_current_line_number($this->xml_parser)));
        }
    }
    xml_parser_free($this->xml_parser);
}

/**
* set start element
*
* @access      public
* @param       $parser parser
* @param       stirng $name name of tag
* @param       string $attrs attributes
* @return      void
*/
function startElement($parser, $name, $attrs) {
    if ($name== $this->base_element){
         $this->index++;
    }
    else{
        $this->att[$name]=0;
    }
    $this->tagname=$name;
}

/**
* element content
*
* @access      public
* @param       array $parser parser
* @param       array $data data to be parsed
* @return      void
*/
function elementContent($parser, $data) {
    $this->result[$this->index][$this->tagname]=$data;
    if ($this->tagname== $this->id_element){
         $this->id[$this->index]=$data;
    }
}

/**
* end element 
*
* @access      public
* @param       array $parser parser
* @param       sting $name name
* @return      void
*/
function endElement($parser, $name){
    $this->tagname=="";
}
}
Usage

Code: Select all

require_once('class_xml_parser.inc.php');
$xml		=   new parser("myXMLfile.xml","XMLbasetag","XMLelement");

Posted: Wed Aug 17, 2005 10:33 am
by pospi
:(

Thanks for your code, that was very generous.
But, no dice! It keeps doing the same thing to me. Working fine on my local server, but my webhost keeps just saying 'XML error: syntax error at line 1'.

Why oh why is it working perfectly in one place and not at all in another place!?

Posted: Wed Aug 17, 2005 10:34 am
by patrikG
does your webhost have PHP's dom-xml extension enabled (check in php.ini or do a phpinfo())?

Posted: Thu Aug 18, 2005 8:15 am
by pospi
they say they do, but for some very stupid reason theyve disallowed me checking via phpinfo() so I've no idea whether theyre telling the truth or not.

Posted: Thu Aug 18, 2005 8:32 am
by feyd
maybe you can figure out if the extension is loaded through: extension_loaded() and/or get_loaded_extensions()

Posted: Thu Aug 18, 2005 9:44 pm
by pospi
Thanks for that!

Array
(
[0] => zip
[1] => xml
[2] => tokenizer
[3] => standard
[4] => sockets
[5] => session
[6] => posix
[7] => pgsql
[8] => pcre
[9] => overload
[10] => mysql
[11] => ming
[12] => mhash
[13] => mcrypt
[14] => gettext
[15] => gd
[16] => ftp
[17] => exif
[18] => curl
[19] => ctype
[20] => calendar
[21] => bcmath
[22] => zlib
[23] => openssl
[24] => apache
[25] => Zend Optimizer
)

So it looks like the XML extensions are loaded. I'm stumped!