php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #32558 formatOutput Problems
Submitted: 2005-04-03 18:24 UTC Modified: 2005-04-04 00:00 UTC
From: thomas dot werner at mac dot com Assigned:
Status: Not a bug Package: DOM XML related
PHP Version: 5.0.4 OS: Linux/OSX
Private report: No CVE-ID: None
 [2005-04-03 18:24 UTC] thomas dot werner at mac dot com
Description:
------------
i wrote for my old php4 application a dom/xml wrapper 
class, which does not extends the domdocument class, 
only created the object inside of my class.

but after adding of an element node, the cr/lf ist 
missing.

a tried everything to get it work :((

cheers tom

Reproduce code:
---------------
<?php

	class CMS_XML {
	
		var $convert = false;
		var $formed = true;
		var $encoding = "ISO-8859-1";
		
		var $filename;
		
		var $wddx = false;
	
		var $_xml;
		var $_debug;
	
		var $_xpath;
		var $_path;
	
		function __construct( $debug = 0 ) 
		{/*{{{*/
	
			$this->_debug = $debug;
			$this->convert  = $convert;
	
		}/*}}}*/
		
		function _returnXML( $path, $value = array() )
		{/*{{{*/
			if ( is_array( $path ) && sizeof( $path ) ) {
				$returnxml = array();
				for( $i = 0; $i < sizeof( $path ); $i++ ) {
					$returnxml[$i] = new ESMT_CORE_XML_ITEM( $this );
					$returnxml[$i]->xpath = $path[$i];
					if( isset( $value[$i] ) )
						$returnxml[$i]->text = $value[$i];
				}
			}
			else {
				$returnxml = new ESMT_CORE_XML_ITEM( $this );
				$returnxml->xpath = $path;
				if( isset( $value ) ) 
					$returnxml->value = $value;
			}
			return $returnxml;		
		}/*}}}*/
		
		function _prepareXpath( $path )
		{/*{{{*/
			
			if( !is_string( $path ) ) {
				die( 'XML FATAL ERROR: path is not a string ('.print_r( $path ).')!');
			}
			if ( preg_match_all("|(\/[^\[]+\[)([^\]'\"\s]+)(\])|", $path, $out ) ) {
				for( $i = 0; $i < sizeof( $out[0] ); $i++ ) {
					$path = str_replace( $out[1][$i].$out[2][$i].$out[3][$i], $out[1][$i].preg_replace( '/([^=]+)=([^"\']+)/', "\\1='\\2'", $out[2][$i] ).$out[3][$i] ,$path );
				}
			}
			if ( preg_match_all("|\/([a-zA-Z_:]+)\(([0-9]+)\)|", $path, $out ) ) {
				for( $i = 0; $i < sizeof( $out[0] ); $i++ ) {
					$path = str_replace( '/'.$out[1][$i].'('.$out[2][$i].')', '/'.$out[1][$i].'['.( $out[2][$i] + 1 ).']', $path );
				}
			}
			#echo "<br>$path";
			
			return $path;
		}/*}}}*/
	
		function _getXpath( $node, $path = '' )
		{/*{{{*/
	
			$index = 0;  

			if ( $node->parentNode ) {
			
				$temp = $node;
				
				while( $temp )
				{
					if ( $temp->nodeType == XML_ELEMENT_NODE )
					{
						if( $temp->nodeName == $node->nodeName )
						{
				 			$index++;  
						}
					}
					$temp = $temp->previousSibling;
				}
				$path = ( "/" . $node->nodeName . "[" . $index . "]" . $path );
	
				if( $node->parentNode ) {
					$path = $this->_getXpath( $node->parentNode, $path );
				}
				
				unset( $temp );
			}
			
			return $path;
			
		}/*}}}*/
	
		function parseString( $string ) 
		{/*{{{*/
	
			$this->_xml = new domDocument();
			
			$this->_xml->resolveExternals = true;
	
			if ( !$this->_xml->loadXML( trim( $string ) ) ) {
				
				unset( $this->_xml );
				
				return false;
			}
	
			return $this;
		}/*}}}*/
		
		function parseFile( $filename, $trusted = false ) 
		{/*{{{*/
	
			$this->filename = ( !ereg( "/", $filename ) && is_file( ereg_replace( "/$", "", $this->path )."/".$filename ) ) ? ereg_replace( "/$", "", $this->path )."/".$filename : $filename;

	
			if ( $trusted && !$this->trustedFile() ) {
				return false;
			}
	
			if ( ! ( $filepointer = fopen( $this->filename, "r" ) ) ) {
				return false;
			}
	
			$data = "";
			while ( !feof( $filepointer) ) {
				$data .= fgets( $filepointer, 4096 );
			}
			fclose( $filepointer );
			
			return $this->parseString( $data );
		}/*}}}*/
	
		function getDocument( $header = true, $formatted = true ) 
		{/*{{{*/
	
			if ( !is_object( $this->_xml ) ) 
				return false;
	
			if ( $formatted === true || $this->_debug ) {
				$this->_xml->formatOutput = true;
			}
			if ( $header ) {
				$doc = $this->_xml->saveXML();
			}
			else {
				if ( $this->_xml->lastChild  ) {
					$doc = $this->_xml->saveXML( $this->_xml->lastChild );
				}
			}
			
			if ( $this->_debug ) {
				echo "XML Debug: getDocument [<pre>".htmlentities( $doc )."</pre>]\n";
			}
	
			return $doc;
	
		}/*}}}*/
	
		function createNode( $path, $name ) 
		{/*{{{*/
	
			if ( $path == "" ) $path = $this->_path;
			
			$path = $this->_prepareXpath( $path );
		
			if ( !is_object( $this->_xml ) ) 
				return false;
	
			if ( !is_object( $this->_xpath ) ) 
				$this->_xpath = new domXPath( $this->_xml );
				
			if ( $nodes = $this->_xpath->query( $path ) ) {
				if ( $nodes->length ) {
					$xpath = array();
					foreach( $nodes as $node ) {
						if( $node->nodeType == XML_ELEMENT_NODE ) {
							$foo = $node->appendChild( new domElement( $name ) );			
							if ( $nodes->length == 1 ) 
								return $this->_returnXML( $this->_getXPath( $foo ) );
							else
								$xpath[] = $this->_getXPath( $foo );
						}
					}
					if( $xpath ) return $this->_returnXML( $xpath );
				}
			}
	
			return null;
		}/*}}}*/	
	}
	
	class CMS_XSL extends CMS_XML {/*{{{*/

	}/*}}}*/
	
	
	class ESMT_CORE_XML_ITEM {/*{{{*/
	
		var $xpath;
		var $text;
		var $value;
		
		var $parent = false;
		
		function __construct( $parent = false ) 
		{/*{{{*/
			if ( is_object( $parent ) ) {
				$this->parent = $parent;
			}	
		}/*}}}*/
		
	}/*}}}*/

<?php

        include( 'xml2.php' );

        $xml = new CMS_XML();

        $xml->parseFile( 'test.xml' ); 

        $xml->createNode( '//struct', 'foo' );

        echo "<pre>".htmlentities( $xml->getDocument() )."</pre>";

        echo "FormatOuptut: ".( $xml->_xml->formatOutput ? 'true' : 'false' );
?>

Expected result:
----------------
<?xml version="1.0"?>
<wddxpacket version="1.0">
  <header>
    <comment>idea::CMS Editor Configuration File</
comment>
  </header>
  <data>
    <struct>
      <var name="editor_versioning">
        <number>25</number>
      </var>
      <var name="editor_console">
        <number>25</number>
      </var>
      <var name="editor_caching">
        <number>3</number>
      </var>
      <var name="editor_paste">
        <string>false</string>
      </var>
      <var name="editor_styles">
        <null/>
      </var>
      <foo/>
    </struct>
  </data>
</wddxpacket>

FormatOuptut: true

Actual result:
--------------
<?xml version="1.0"?>
<wddxpacket version="1.0">
  <header>
    <comment>idea::CMS Editor Configuration File</
comment>
  </header>
  <data>
    <struct>
      <var name="editor_versioning">
        <number>25</number>
      </var>
      <var name="editor_console">
        <number>25</number>
      </var>
      <var name="editor_caching">
        <number>3</number>
      </var>
      <var name="editor_paste">
        <string>false</string>
      </var>
      <var name="editor_styles">
        <null/>
      </var>
    <foo/></struct>
  </data>
</wddxpacket>

FormatOuptut: true

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-04-03 18:26 UTC] tony2001@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc.

If possible, make the script source available online and provide
an URL to it here. Try to avoid embedding huge scripts into the report.


 [2005-04-03 20:21 UTC] thomas dot werner at mac dot com
sorry for the long code, but a have only this example:

working example with php5.0.3: 
http://213.61.134.141/32558.php
source code:
http://213.61.134.141/32558.txt

http://213.61.134.141/phpinfo.php

i think the parseString and the getDocument methods in 
my class is the important point. i set it there without 
any success.

cheers tom
 [2005-04-03 22:52 UTC] sniper@php.net
Try putting some effort into this yourself, we're not here to fix your code.

 [2005-04-04 00:00 UTC] thomas dot werner at mac dot com
sorry! it will never happen again, it could be my 
fault... and thank your for your 'help'.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 02 11:01:31 2024 UTC