#32558 [NEW]: formatOutput Problems
| From: | thomas dot werner at mac dot com | Date: | Sun, 03 Apr 2005 16:24:17 +0000 |
| Subject: | #32558 [NEW]: formatOutput Problems | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-76254@lists.php.net to get a copy of this message | ||
From: thomas dot werner at mac dot com
Operating system: Linux/OSX
PHP version: 5.0.4
PHP Bug Type: DOM XML related
Bug description: formatOutput Problems
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
--
Edit bug report at http://bugs.php.net/?id=32558&edit=1
--
Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=32558&r=trysnapshot4
Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=32558&r=trysnapshot50
Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=32558&r=trysnapshot51
Fixed in CVS: http://bugs.php.net/fix.php?id=32558&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=32558&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=32558&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=32558&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=32558&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=32558&r=support
Expected behavior: http://bugs.php.net/fix.php?id=32558&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=32558&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=32558&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=32558&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=32558&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=32558&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=32558&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=32558&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=32558&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=32558&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=32558&r=mysqlcfg