How to read XML in PHP

Skythrust

New Member
Messages
14
Likes
0
#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
4,998
Likes
1,124
#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
 

Skythrust

New Member
Messages
14
Likes
0
#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,321
Likes
900
#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>
 

Skythrust

New Member
Messages
14
Likes
0
#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.
 

Skythrust

New Member
Messages
14
Likes
0
#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,321
Likes
900
#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)
 

Skythrust

New Member
Messages
14
Likes
0
#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
891
Likes
711
#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"];
 

Skythrust

New Member
Messages
14
Likes
0
#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:

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

Top