|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2005-04-03 18:26 UTC] tony2001@php.net
[2005-04-03 20:21 UTC] thomas dot werner at mac dot com
[2005-04-03 22:52 UTC] sniper@php.net
[2005-04-04 00:00 UTC] thomas dot werner at mac dot com
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 22 00:00:01 2025 UTC |
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