How to read XML in PHP

Messages
53
Likes
3
#1
Hi Guys,

I am trying to read a connection string (made in c# (at C:\ProgramData)) inside PHP, but without success.

Any idea what the problem could be?

Code;
PHP:
$xmldata = simplexml_load_file("C:\ProgramData\ID\Config\Config.xml") or die("Failed to load");
echo $xmldata->DatabaseConnection[0]->DataSource;
echo $xmldata->DatabaseSettings[0]->LicenseKey;
XML:
<?xml version="1.0" encoding="utf-8"?><Config><DatabaseConnection DataSource="SQLSERVER" InitialCatalog="DATABASE" UserID="UID" Password="PWD" />
 
Messages
5,081
Likes
1,164
#2
What in God's green earth would you want to do this for? XML can be viewed in the dev tools. As soon as you load that XML file the client will see that used that file for something and get your database password / config.

Make a php file with the connection details... PHP is all server side
 
Messages
53
Likes
3
#3
What in God's green earth would you want to do this for? XML can be viewed in the dev tools. As soon as you load that XML file the client will see that used that file for something and get your database password / config.

Make a php file with the connection details... PHP is all server side
I can understand your reaction. I have a Windows form application which one need to load to something where PHP can read from.

For now;

- WinForm creates a config.xml.
- PHP has a connection string to a database etc etc..
- PHP need to read the xml file to update the connection string, to make it child friendly.

Else I need to create a WinForm with readwrite things, but I was assuming that this would be easier.
 

Ecko

23:37 [autobots] -!- eckostylez [[email protected]]
Messages
1,348
Likes
918
#6
If that is your full Config.xml file, then it is not valid XML. You will get an error about the Config tag. Unless it actually looks something like this:
Code:
<?xml version="1.0" encoding="utf-8"?>
<Config>
    <DatabaseConnection DataSource="SQLSERVER" InitialCatalog="DATABASE" UserID="UID" Password="PWD" />
</Config>
 
Messages
53
Likes
3
#7
If that is your full Config.xml file, then it is not valid XML. You will get an error about the Config tag. Unless it actually looks something like this:
Code:
<?xml version="1.0" encoding="utf-8"?>
<Config>
    <DatabaseConnection DataSource="SQLSERVER" InitialCatalog="DATABASE" UserID="UID" Password="PWD" />
</Config>
I have that tag. But there is a lot of more information but not needed for this intergration thing.
 
Messages
53
Likes
3
#9
Post the full Config.xml

I get no errors running the PHP script with the XML I posted above.
I get no errors to, but I would like to have the XML things as echo.

XML:
<?xml version="1.0" encoding="utf-8"?><Config><DatabaseConnection DataSource="localhost" InitialCatalog="Denzi" UserID="sa" Password="Abc123" /><DatabaseSettings LicenseKey="QMH8-J178-0YDK-5R5Y" LanguageDutch="1" LanguageEnglish="1" GDPRValue="14" Project="Test" UserField="Username" /></Config>
PHP:
<b>//-----  XML Connection Settings  -----\\</b>
<br><br>
<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);


$xmldata = simplexml_load_file("C:\ProgramData\Denzi\Config\Config.xml") or die("Failed to load");
echo $xmldata->DatabaseConnection[0]->DataSource;
echo $xmldata->DatabaseSettings[0]->LicenseKey;



?>
 

Ecko

23:37 [autobots] -!- eckostylez [[email protected]]
Messages
1,348
Likes
918
#10
Shouldn't it be something like echo $xmldata->Config->DatabaseConnection? If that's not working, var_dump() is your friend. Haven't messed with PHP in forever to recall the shenanigans it does with XML (json > xml anyways)
 
Messages
53
Likes
3
#11
Shouldn't it be something like echo $xmldata->Config->DatabaseConnection? If that's not working, var_dump() is your friend. Haven't messed with PHP in forever to recall the shenanigans it does with XML (json > xml anyways)
Notice: Trying to get property of non-object in B:\_Websites\inc\testpage.php
 

griimnak

You're a slave to the money then you die
Messages
901
Likes
740
#12
Notice: Trying to get property of non-object in B:\_Websites\inc\testpage.php
I replicated your code, this is what i got:
PHP:
<?php
// set reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
// test php
echo "ping";
// load
$xmldata = simplexml_load_file("C:\ProgramData\griimnak\Config\Config.xml") or die("Failed to load");
// This does not work for me **
echo $xmldata->DatabaseConnection[0]->DataSource;
echo $xmldata->DatabaseSettings[0]->LicenseKey;
// **
// but this does work for me
echo "\n";
echo $xmldata->DatabaseConnection[0]["DataSource"];
echo "\n";
echo $xmldata->DatabaseConnection[0]["UserID"];
// raw print confirms it works
echo "\n";
print_r($xmldata->DatabaseConnection);


Try changing
PHP:
echo $xmldata->DatabaseConnection[0]->DataSource;
echo $xmldata->DatabaseSettings[0]->LicenseKey;
To
PHP:
echo $xmldata->DatabaseConnection[0]["DataSource"];
echo $xmldata->DatabaseSettings[0]["LicenseKey"];
 
Messages
53
Likes
3
#13
I replicated your code, this is what i got:
PHP:
<?php
// set reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
// test php
echo "ping";
// load
$xmldata = simplexml_load_file("C:\ProgramData\griimnak\Config\Config.xml") or die("Failed to load");
// This does not work for me **
echo $xmldata->DatabaseConnection[0]->DataSource;
echo $xmldata->DatabaseSettings[0]->LicenseKey;
// **
// but this does work for me
echo "\n";
echo $xmldata->DatabaseConnection[0]["DataSource"];
echo "\n";
echo $xmldata->DatabaseConnection[0]["UserID"];
// raw print confirms it works
echo "\n";
print_r($xmldata->DatabaseConnection);


Try changing
PHP:
echo $xmldata->DatabaseConnection[0]->DataSource;
echo $xmldata->DatabaseSettings[0]->LicenseKey;
To
PHP:
echo $xmldata->DatabaseConnection[0]["DataSource"];
echo $xmldata->DatabaseSettings[0]["LicenseKey"];
Thanks! This works for me..

I can now echo things from the xml file.

Only I get now; Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -33
Code:
 => -33            [2] => Invalid value type for option Database was specified.  String type was expected.            [message] => Invalid value type for option Database was specified.  String type was expected.        ) )

[CODE=PHP]$xmldata = simplexml_load_file("C:\ProgramData\IDS\Config\Config.xml") or die("Failed to load");
$XML_SQL_DataSource = $xmldata->DatabaseConnection[0]["DataSource"];
$XML_SQL_InitialCatalog = $xmldata->DatabaseConnection[0]["InitialCatalog"];
$XML_SQL_UserID = $xmldata->DatabaseConnection[0]["UserID"];
$XML_SQL_Password = $xmldata->DatabaseConnection[0]["Password"];


$connectionInfo = array( "Database"=>$XML_SQL_InitialCatalog, "UID"=>$XML_SQL_UserID, "PWD"=>$XML_SQL_Password);
$conn = sqlsrv_connect( $XML_SQL_DataSource, $connectionInfo);
When I try this, I get the correct values.
PHP:
echo "    
     <b>SQL Server:</b>     $XML_SQL_DataSource <br>
     <b>SQL Database:</b>     $XML_SQL_InitialCatalog <br>
     <b>SQL Username:</b>     $XML_SQL_UserID <br>
     <b>SQL Password:</b>     ".md5($XML_SQL_Password)." (For Secure password is encrypted.)<br>
     ";
Post automatically merged:

Any update? :-(
Post automatically merged:

Is there someone else who can help me out with this case?
 
Last edited:
Messages
38
Likes
15
#14
dont know if this is still relevant.

you are trying to access xml attributes therefore you need to call attributes() to access it.
you can do it like so:
PHP:
$xml = simplexml_load_file('yourFile.xml');

$connection = $xml->DatabaseConnection->attributes();
$settings   = $xml->DatabaseSettings->attributes();

echo $connection->DataSource;
echo $settings->LicenseKey;
...
 
Messages
53
Likes
3
#15
dont know if this is still
PHP:
$xmldata = simplexml_load_file("Config.xml") or die("Failed to load");
$XML_SQL_DataSource = $xmldata->DatabaseConnection[0]["DataSource"];
$XML_SQL_InitialCatalog = $xmldata->DatabaseConnection[0]["InitialCatalog"];
$XML_SQL_UserID = $xmldata->DatabaseConnection[0]["UserID"];
$XML_SQL_Password = $xmldata->DatabaseConnection[0]["Password"];
relevant.

you are trying to access xml attributes therefore you need to call attributes() to access it.
you can do it like so:
PHP:
$xml = simplexml_load_file('yourFile.xml');

$connection = $xml->DatabaseConnection->attributes();
$settings   = $xml->DatabaseSettings->attributes();

echo $connection->DataSource;
echo $settings->LicenseKey;
...
Something like this;
PHP:
$xmldata = simplexml_load_file("Config.xml") or die("Failed to load");
$XML_SQL_DataSource = $xmldata->DatabaseConnection[0]["DataSource"];
$XML_SQL_InitialCatalog = $xmldata->DatabaseConnection[0]["InitialCatalog"];
$XML_SQL_UserID = $xmldata->DatabaseConnection[0]["UserID"];
$XML_SQL_Password = $xmldata->DatabaseConnection[0]["Password"];
 
Messages
38
Likes
15
#16
I dont know if this what you got works but i would do it like this:
PHP:
$xml = simplexml_load_file('yourFile.xml');

$connection = $xml->DatabaseConnection->attributes();
$settings   = $xml->DatabaseSettings->attributes();

$XML_SQL_DataSource     = $connection->DataSource;
$XML_SQL_InitialCatalog = $connection->InitialCatalog;
$XML_SQL_UserID         = $connection->UserID;
$XML_SQL_Password       = $connection->Password;
this is the correct way and also much cleaner solution.
 
Messages
53
Likes
3
#17
I dont know if this what you got works but i would do it like this:
PHP:
$xml = simplexml_load_file('yourFile.xml');

$connection = $xml->DatabaseConnection->attributes();
$settings   = $xml->DatabaseSettings->attributes();

$XML_SQL_DataSource     = $connection->DataSource;
$XML_SQL_InitialCatalog = $connection->InitialCatalog;
$XML_SQL_UserID         = $connection->UserID;
$XML_SQL_Password       = $connection->Password;
this is the correct way and also much cleaner solution.
I got an error with this code, seems like the same I already had before.
Code:
Array (    [0] => Array        (            [0] => IMSSP            [SQLSTATE] => IMSSP            [1] => -33            [code] => -33            [2] => Invalid value type for option Database was specified.  String type was expected.            [message] => Invalid value type for option Database was specified.  String type was expected.        ) )
 
Messages
38
Likes
15
#18
this should work:
PHP:
$XML_SQL_DataSource     = (string) $connection->DataSource;
$XML_SQL_InitialCatalog = (string) $connection->InitialCatalog;
$XML_SQL_UserID         = (string) $connection->UserID;
$XML_SQL_Password       = (string) $connection->Password;
currently they are type of SimpleXMLObject. but looking at the error you need a string type. so you can cast the object into a string by placing (string) in front of it.

let me know if it works. currently on mobile so i cant test it myself.
 
Messages
53
Likes
3
#19
this should work:
PHP:
$XML_SQL_DataSource     = (string) $connection->DataSource;
$XML_SQL_InitialCatalog = (string) $connection->InitialCatalog;
$XML_SQL_UserID         = (string) $connection->UserID;
$XML_SQL_Password       = (string) $connection->Password;
currently they are type of SimpleXMLObject. but looking at the error you need a string type. so you can cast the object into a string by placing (string) in front of it.

let me know if it works. currently on mobile so i cant test it myself.
Thanks this is the solution :)
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top