Alex Jenkinson Alex Jenkinson -4 years ago 137
PHP Question

How to parse this XML file in PHP?

I am failing to understand the examples provided on most sites relating to this. I want to be able to get a users input for example "Month: 1", "ID: 1", "Date: 01-01-2017" which then display that exact one.

I can't figure out how to move through XML this is my current idea

echo $xml->fullyear->month[$monthGET]->finances[$passID]->projection


However this doesn't work. How can I get it to output using the user inputs, the inputs work however.

This is my current PHP file to read the data:

<?php
$xml=simplexml_load_file("XML1.xml") or die("Error: Cannot create object");

echo $_GET["SearchChoice"];
echo $_GET["IDsend"];
echo $_GET["DateSend"];

switch ($_GET["SearchChoice"])
{
case "ID Search":
$passID = intval($_GET["IDsend"]);
if ($passID !== 0)
{
$passID = $passID - 1;
}
$attr = $xml->finances[$passID]->attributes();
echo "Date: ";
echo $attr['date'];
echo "<br> Projection: ";
echo $xml->finances[$passID]->projection . ",<br>Recommended Staff: ";
echo $xml->finances[$passID]->recommendedStaff . ",<br>Staff Wages: ";
echo $xml->finances[$passID]->staffWages . ",<br>Actual: ";
echo $xml->finances[$passID]->actual . "<br>";
break;
case "Date Search":
$passDate = strval($_GET["DateSend"]);
$nodes = $xml->xpath("/year2017/finances[@date='". $passDate . "']");
if ($nodes)
$nodes = array_shift($nodes);
break;
default:
$nodes = NULL;
}

if ($nodes)
{
echo "Date: ";
$attr = $nodes->attributes();
echo $attr['date'];
echo "<br> Projection: ";
echo $nodes->projection . ",<br>Recommended Staff: ";
echo $nodes->recommendedStaff . ",<br>Staff Wages: ";
echo $nodes->staffWages . ",<br>Actual: ";
echo $nodes->actual . "<br>";
}
?>


Here is the snippet of my XML file.

<fullyear>
<month value="1">
<finances id="1" date="01-01-2017">
<projection>414</projection>
<recommendedStaff>20</recommendedStaff>
<staffWages>100</staffWages>
<actual>250</actual>
</finances>
<finances id="2" date="02-01-2017">
<projection>124</projection>
<recommendedStaff>8</recommendedStaff>
<staffWages>150</staffWages>
<actual>250</actual>
</finances>
<finances id="3" date="03-01-2017">
<projection>687</projection>
<recommendedStaff>20</recommendedStaff>
<staffWages>150</staffWages>
<actual>250</actual>
</finances>
<finances id="4" date="04-01-2017">
<projection>587</projection>
<recommendedStaff>15</recommendedStaff>
<staffWages>150</staffWages>
<actual>250</actual>
</finances>
<finances id="5" date="05-01-2017">
<projection>124</projection>
<recommendedStaff>5</recommendedStaff>
<staffWages>150</staffWages>
<actual>250</actual>
</finances>
<finances id="6" date="06-01-2017">
<projection>874</projection>
<recommendedStaff>22</recommendedStaff>
<staffWages>150</staffWages>
<actual>250</actual>
</finances>
<finances id="7" date="07-01-2017">
<projection>574</projection>
<recommendedStaff>18</recommendedStaff>
<staffWages>150</staffWages>
<actual>250</actual>
</finances>
</month>
<month value="2">
<finances id="32" date="01-02-2017">
<projection>101</projection>
<recommendedStaff>10</recommendedStaff>
<staffWages>101</staffWages>
<actual>101</actual>
</finances>
<finances id="33" date="02-02-2017">
<projection>101</projection>
<recommendedStaff>10</recommendedStaff>
<staffWages>101</staffWages>
<actual>101</actual>
</finances>
<finances id="34" date="03-02-2017">
<projection>101</projection>
<recommendedStaff>10</recommendedStaff>
<staffWages>101</staffWages>
<actual>101</actual>
</finances>
<finances id="35" date="04-02-2017">
<projection>101</projection>
<recommendedStaff>10</recommendedStaff>
<staffWages>101</staffWages>
<actual>101</actual>
</finances>
<finances id="36" date="05-02-2017">
<projection>101</projection>
<recommendedStaff>10</recommendedStaff>
<staffWages>101</staffWages>
<actual>101</actual>
</finances>
</month>
</fullyear>

Answer Source

You are missing few thing. like month in the case "ID Search": and not providing proper path in xpath.

Try below:

<?php
$xml=simplexml_load_file("XML1.xml") or die("Error: Cannot create object");

$_GET["SearchChoice"]="Date Search";
$_GET["IDsend"]=1;// these are just sample values
$_GET["DateSend"]="01-01-2017";
$month=1;
switch ($_GET["SearchChoice"])
{
    case "ID Search":
        $passID = intval($_GET["IDsend"]);
        if ($passID !== 0)
        {
            $passID = $passID - 1;
        }

        $attr = $xml->month[$month]->finances[$passID]->attributes();
        echo "Date: ";
        echo $attr['date'];
        echo "<br> Projection: ";
        echo $xml->month[$month]->finances[$passID]->projection . ",<br>Recommended Staff: ";
        echo $xml->month[$month]->finances[$passID]->recommendedStaff . ",<br>Staff Wages: ";
        echo $xml->month[$month]->finances[$passID]->staffWages . ",<br>Actual: ";
        echo $xml->month[$month]->finances[$passID]->actual . "<br>";
        break;
    case "Date Search":
        $passDate = strval($_GET["DateSend"]);
        $nodes = $xml->xpath("/fullyear/month/finances[@date='". $passDate . "']");
        if  ($nodes)
            $nodes = array_shift($nodes);
        break;
    default:
        $nodes = NULL;
}

if  ($nodes)
{
    echo "Date: ";
    $attr = $nodes->attributes();
    echo $attr['date'];
    echo "<br> Projection: ";
    echo $nodes->projection . ",<br>Recommended Staff: ";
    echo $nodes->recommendedStaff . ",<br>Staff Wages: ";
    echo $nodes->staffWages . ",<br>Actual: ";
    echo $nodes->actual . "<br>";
}

OUTPUT FOR XPATH Date Search:

Date: 01-01-2017
Projection: 414,
Recommended Staff: 20,
Staff Wages: 100,
Actual: 250

OUTPUT FOR XPATH ID Search:

Date: 01-02-2017
Projection: 101,
Recommended Staff: 10,
Staff Wages: 101,
Actual: 101
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download