ChrisMartino Posted January 14, 2011 Share Posted January 14, 2011 Hey there, I'm having a issue when I edit the XML file from a string here. Now the SimpleXML functions edit the nodes of the XML file perfectly and everything is done the way I want but upon returning the file to a string it keeps putting a XML content type at the top of the file, E.G: Before XML File Is Edited: <settings> <!-- Port the server will listen on --> <port>7781</port> <!-- Maximum number of players the server will support (Max 32) --> <maxplayers>10</maxplayers> <!-- Maximum number of vehicles the server will support (Max 140) --> <maxvehicles>100</maxvehicles> <!-- Password clients will have to enter to connect --> <!-- password>None</password --> <!-- Add the server to the master list --> <listed>true</listed> <!-- The hostname players will see --> <hostname>IV:MP Server</hostname> <!-- The address the server will bind to --> <!-- hostaddress>127.0.0.1</hostaddress --> <!-- Toggles frequently called events which has impact on CPU usage --> <frequentevents>false</frequentevents> <!-- The scripts the server will load and run --> <script>cp.nut</script> <script>whisper.nut</script> <script>namecheck.nut</script> <script>runcode.nut</script> <!-- The modules the server will load and run --> <!-- For windows: --> <!-- module>Sample.dll</module --> <!-- For linux: --> <!-- module>Sample.so</module --> </settings> After XML File Is Edited <?xml version=1.0?> <settings> <!-- Port the server will listen on --> <port>7781</port> <!-- Maximum number of players the server will support (Max 32) --> <maxplayers>10</maxplayers> <!-- Maximum number of vehicles the server will support (Max 140) --> <maxvehicles>100</maxvehicles> <!-- Password clients will have to enter to connect --> <!-- password>None</password --> <!-- Add the server to the master list --> <listed>true</listed> <!-- The hostname players will see --> <hostname>IV:MP Server</hostname> <!-- The address the server will bind to --> <!-- hostaddress>127.0.0.1</hostaddress --> <!-- Toggles frequently called events which has impact on CPU usage --> <frequentevents>false</frequentevents> <!-- The scripts the server will load and run --> <script>cp.nut</script> <script>whisper.nut</script> <script>namecheck.nut</script> <script>runcode.nut</script> <!-- The modules the server will load and run --> <!-- For windows: --> <!-- module>Sample.dll</module --> <!-- For linux: --> <!-- module>Sample.so</module --> </settings> Notice the addition to the header of the file "<?xml version=1.0?>". This is causing issues when the XML file is read by the server. Could anybody help me stop the SimpleXML editor from placing this in the header of the file? Thanks. Link to comment https://forums.phpfreaks.com/topic/224384-simplexml-support/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 14, 2011 Share Posted January 14, 2011 A) What exact problem does this cause when the file is read? B) You would need to get the xml into a string and simply remove the <?xml version=1.0?> from the start of the string, then save the string to a file. Link to comment https://forums.phpfreaks.com/topic/224384-simplexml-support/#findComment-1159162 Share on other sites More sharing options...
ChrisMartino Posted January 14, 2011 Author Share Posted January 14, 2011 Quote A) What exact problem does this cause when the file is read? B) You would need to get the xml into a string and simply remove the <?xml version=1.0?> from the start of the string, then save the string to a file. A) When I try to edit the file again upon that being added to the file I'm prompted with the following errors from PHP: Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : String not started expecting ' or " in /home/atomnetw/public_html/Atom-Host.com/Modules/Module-Config.php on line 38 Warning: simplexml_load_string() [function.simplexml-load-string]: <?xml version=1.0?> in /home/atomnetw/public_html/Atom-Host.com/Modules/Module-Config.php on line 38 Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in /home/atomnetw/public_html/Atom-Host.com/Modules/Module-Config.php on line 38 Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : Malformed declaration expecting version in /home/atomnetw/public_html/Atom-Host.com/Modules/Module-Config.php on line 38 Warning: simplexml_load_string() [function.simplexml-load-string]: <?xml version=1.0?> in /home/atomnetw/public_html/Atom-Host.com/Modules/Module-Config.php on line 38 Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in /home/atomnetw/public_html/Atom-Host.com/Modules/Module-Config.php on line 38 Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : Blank needed here in /home/atomnetw/public_html/Atom-Host.com/Modules/Module-Config.php on line 38 Warning: simplexml_load_string() [function.simplexml-load-string]: <?xml version=1.0?> in /home/atomnetw/public_html/Atom-Host.com/Modules/Module-Config.php on line 38 Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in /home/atomnetw/public_html/Atom-Host.com/Modules/Module-Config.php on line 38 Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : parsing XML declaration: '?>' expected in /home/atomnetw/public_html/Atom-Host.com/Modules/Module-Config.php on line 38 Warning: simplexml_load_string() [function.simplexml-load-string]: <?xml version=1.0?> in /home/atomnetw/public_html/Atom-Host.com/Modules/Module-Config.php on line 38 Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in /home/atomnetw/public_html/Atom-Host.com/Modules/Module-Config.php on line 38 Fatal error: Call to undefined method stdClass::asXML() in /home/atomnetw/public_html/Atom-Host.com/Modules/Module-Config.php on line 52 B) I've tried by doing the following with the edited string: $new_config = str_replace("<?xml version=1.0?>", "", $XMLConfig->asXML( )); But for some reason it still appears in the file after reading. Thanks for your response. Link to comment https://forums.phpfreaks.com/topic/224384-simplexml-support/#findComment-1159163 Share on other sites More sharing options...
PFMaBiSmAd Posted January 14, 2011 Share Posted January 14, 2011 Since you are using simplexml_load_string(), it stands to reason that your code might be doing something to the string data between where it is being read and where you are calling simplexml_load_string() Quote "<?xml version=1.0?>" ^^^ That's not the exact value being put into the start of the file and so your code cannot match it to replace it. You are missing some double-quotes around the 1.0. Edit: based on what you have been posting for that line, I would say either that there is something wrong with your php version or something wrong with the editor you are using to look at the data in the file. Link to comment https://forums.phpfreaks.com/topic/224384-simplexml-support/#findComment-1159167 Share on other sites More sharing options...
ChrisMartino Posted January 14, 2011 Author Share Posted January 14, 2011 Quote Since you are using simplexml_load_string(), it stands to reason that your code might be doing something to the string data between where it is being read and where you are calling simplexml_load_string() Quote "<?xml version=1.0?>" ^^^ That's not the exact value being put into the start of the file and so your code cannot match it to replace it. You are missing some double-quotes around the 1.0. I've just checked there and it definitely isn't putting double quotes around the version attribute of that. I looked at the raw version and it was "<?xml version=1.0?>" Here is my entire function: function verify_xml($server_id) { global $Class; if(!$Class['Servers']->is_valid($server_id)) return 0; $Server = $Class['MySQL']->fetch_data("SELECT * FROM atom_servers WHERE Server_ID = '{$Class['MySQL']->escape_string($server_id)}'"); $Product = $Class['MySQL']->fetch_data("SELECT * FROM atom_products WHERE Product_ID = '{$Server['Server_Product']}'"); $server_machine = $Class['Servers']->fetch_value($Server['Server_ID'], "Server_Machine"); $remote_machine = new Machines_Module($server_machine, ROOT); $File = "/home/".$Server['Server_ID']."/".$Product['Product_Config']; $XMLConfig = simplexml_load_string($remote_machine->read_file($File)); //if(!$XMLConfig) $XMLConfig = simplexml_load_string($Product['Product_DefConfig']); if($XMLConfig->maxplayers > $Server['Server_Slots']) { $XMLConfig->maxplayers = $Server['Server_Slots']; } if($XMLConfig->port != $Server['Server_Port'] || $XMLConfig->port != $Server['Server_Port2']) { $XMLConfig->port = $Server['Server_Port']; } $new_config = str_replace("<?xml version=1.0?>", "", $XMLConfig->asXML( )); $remote_machine->run_command("cd /home/{$Server['Server_ID']} && echo \"{$new_config}\" > {$Product['Product_Config']}"); return 1; } Link to comment https://forums.phpfreaks.com/topic/224384-simplexml-support/#findComment-1159171 Share on other sites More sharing options...
PFMaBiSmAd Posted January 14, 2011 Share Posted January 14, 2011 Quote I looked at the raw version What raw version and how did you look? Link to comment https://forums.phpfreaks.com/topic/224384-simplexml-support/#findComment-1159173 Share on other sites More sharing options...
ChrisMartino Posted January 14, 2011 Author Share Posted January 14, 2011 Quote Quote I looked at the raw version What raw version and how did you look? I looked at the file that it edited and removed all str_replace's. The file is located on the dedicated server so I simply cat the file after editing like so: [root@monster 19]# cat settings.xml <?xml version=1.0?> <settings> <!-- Port the server will listen on --> <port>7781</port> <!-- Maximum number of players the server will support (Max 32) --> <maxplayers>10</maxplayers> <!-- Maximum number of vehicles the server will support (Max 140) --> <maxvehicles>100</maxvehicles> <!-- Password clients will have to enter to connect --> <!-- password>None</password --> <!-- Add the server to the master list --> <listed>true</listed> <!-- The hostname players will see --> <hostname>IV:MP Server</hostname> <!-- The address the server will bind to --> <!-- hostaddress>127.0.0.1</hostaddress --> <!-- Toggles frequently called events which has impact on CPU usage --> <frequentevents>false</frequentevents> <!-- The scripts the server will load and run --> <script>cp.nut</script> <script>whisper.nut</script> <script>namecheck.nut</script> <script>runcode.nut</script> <!-- The modules the server will load and run --> <!-- For windows: --> <!-- module>Sample.dll</module --> <!-- For linux: --> <!-- module>Sample.so</module --> </settings> Link to comment https://forums.phpfreaks.com/topic/224384-simplexml-support/#findComment-1159174 Share on other sites More sharing options...
PFMaBiSmAd Posted January 14, 2011 Share Posted January 14, 2011 Without the code that is writing the file (and reading it later), it would be just a little hard to help you, but try the str_replace() with '<?xml version="1.0"?>' Link to comment https://forums.phpfreaks.com/topic/224384-simplexml-support/#findComment-1159177 Share on other sites More sharing options...
ChrisMartino Posted January 14, 2011 Author Share Posted January 14, 2011 Quote Without the code that is writing the file (and reading it later), it would be just a little hard to help you, but try the str_replace() with '<?xml version="1.0"?>' Thanks a lot that worked like a treat problem solved. It's people like you that dedicate time to people needing assistance that make these forums great. Much obliged Link to comment https://forums.phpfreaks.com/topic/224384-simplexml-support/#findComment-1159179 Share on other sites More sharing options...
PFMaBiSmAd Posted January 14, 2011 Share Posted January 14, 2011 Based on your overall symptoms, your code that is saving the file is stripping the double-quotes from the data. Link to comment https://forums.phpfreaks.com/topic/224384-simplexml-support/#findComment-1159302 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.