Jump to content

SimpleXML Support?


ChrisMartino

Recommended Posts

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
Share on other sites

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
Share on other sites

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()

 

"<?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
Share on other sites

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()

 

"<?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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.