Jump to content

DOM: Cannot read XML file


kristo5747

Recommended Posts

Hello!

 

I am new to PHP and I am trying to read this XML file (`myfile.xml`) using DOM:

 

<?xml version="1.0" encoding="ISO-8859-1"?>

<note>

<to>Tove</to>

<from>Jani</from>

<heading>Reminder</heading>

<body>Don't forget me this weekend!</body>

</note>

I created a very simple PHP document I called `xml_01.php`:

<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("myfile.xml");
print $xmlDoc->saveXML();
?>

When I point my browser to http://localhost/xml_01.php, nothing happens. No errors, nothing. I also specified the full path to my XML data file: no change.

 

I even tried re-install the RPM for PHP&XML : no change. I know I am doing something wrong: is it the code or my configuration?

 

Can someone please help?

 

My sandbox config is:

RedHat 2.6.9 el

Apache 2.2.3

PHP 5.16

MySQL 5.0

 

Thanks.

 

Al.

Link to comment
https://forums.phpfreaks.com/topic/150217-dom-cannot-read-xml-file/
Share on other sites

I just went through something very similar and found the following code to work:

 

class XMLParser  {
    
    // raw xml
    private $rawXML;
    // xml parser
    private $parser = null;
    // array returned by the xml parser
    private $valueArray = array();
    private $keyArray = array();
    
    // arrays for dealing with duplicate keys
    private $duplicateKeys = array();
    
    // return data
    private $output = array();
    private $status;

    public function XMLParser($xml){
        $this->rawXML = $xml;
        $this->parser = xml_parser_create();
        return $this->parse();
    }

    private function parse(){
        
        $parser = $this->parser;
        
        xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); // Dont mess with my cAsE sEtTings
        xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);     // Dont bother with empty info
        if(!xml_parse_into_struct($parser, $this->rawXML, $this->valueArray, $this->keyArray)){
            $this->status = 'error: '.xml_error_string(xml_get_error_code($parser)).' at line '.xml_get_current_line_number($parser);
            return false;
        }
        xml_parser_free($parser);

        $this->findDuplicateKeys();

        // tmp array used for stacking
        $stack = array();         
        $increment = 0;
        
        foreach($this->valueArray as $val) {
            if($val['type'] == "open") {
                //if array key is duplicate then send in increment
                if(array_key_exists($val['tag'], $this->duplicateKeys)){
                    array_push($stack, $this->duplicateKeys[$val['tag']]);
                    $this->duplicateKeys[$val['tag']]++;
                }
                else{
                    // else send in tag
                    array_push($stack, $val['tag']);
                }
            } elseif($val['type'] == "close") {
                array_pop($stack);
                // reset the increment if they tag does not exists in the stack
                if(array_key_exists($val['tag'], $stack)){
                    $this->duplicateKeys[$val['tag']] = 0;
                }
            } elseif($val['type'] == "complete") {
                //if array key is duplicate then send in increment
                if(array_key_exists($val['tag'], $this->duplicateKeys)){
                    array_push($stack, $this->duplicateKeys[$val['tag']]);
                    $this->duplicateKeys[$val['tag']]++;
                }
                else{                
                    // else send in tag
                    array_push($stack,  $val['tag']);
                }
                $this->setArrayValue($this->output, $stack, $val['value']);
                array_pop($stack);
            }
            $increment++;
        }

        $this->status = 'success: xml was parsed';
        return true;

    }
    
    private function findDuplicateKeys(){
        
        for($i=0;$i < count($this->valueArray); $i++) {
            // duplicate keys are when two complete tags are side by side
            if($this->valueArray[$i]['type'] == "complete"){
                if( $i+1 < count($this->valueArray) ){
                    if($this->valueArray[$i+1]['tag'] == $this->valueArray[$i]['tag'] && $this->valueArray[$i+1]['type'] == "complete"){
                        $this->duplicateKeys[$this->valueArray[$i]['tag']] = 0;
                    }
                }
            }
            // also when a close tag is before an open tag and the tags are the same
            if($this->valueArray[$i]['type'] == "close"){
                if( $i+1 < count($this->valueArray) ){
                    if(    $this->valueArray[$i+1]['type'] == "open" && $this->valueArray[$i+1]['tag'] == $this->valueArray[$i]['tag'])
                        $this->duplicateKeys[$this->valueArray[$i]['tag']] = 0; 
                }
            }
            
        }
        
    }
    
    private function setArrayValue(&$array, $stack, $value){
        if ($stack) {
            $key = array_shift($stack);
            $this->setArrayValue($array[$key], $stack, $value);
            return $array;
        } else {
            $array = $value;
        }
    }
    
    public function getOutput(){
        return $this->output;
    }
    
    public function getStatus(){
        return $this->status;    
    }
       
}

 

It loads your XML data into an array which I pulled out and echoed back using:

 

foreach($_POST as $k => $v){
$value = $k . $v;
$value = $value . "</payload>
					</data>
					</NuanceAlarm>" ;
$value = str_replace('<?xml_version\"1.0\" encoding=\"UTF-8\"?>','',$value);
$times = date('Y-m-d H:i:s');
$p = new XMLParser($value);
$y = $p->getOutput(); 	

foreach($y as $a=> $b){
	echo $a . "<br>";
	foreach($b as $c => $d){
		$i = 0;
		foreach($d as $e=> $f) {
			$field[$i] = $e;
			$fvalue[$i] = $f;
	echo $e . " : " . $f . "<br>";
	$i++;
		}
	}
}

 

Note the str_replace() pulls the XML data from the $_POST variables that I receive from the HTTP POST application that I get the XML data from. Their application did not provide me with a well formatted doc type declaration so i stripped it and got the code to work great!

 

Cheers!

Interesting.  You are not using the XML DOM parser, you simply built your own.

 

I guess I could do the same thing as you (certainly not as fancy) by echo-ing each line to HTML.

 

It would work but it is not solving my problem...... :-\

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.