0

I need to output the response from the database in XML. So far I have gotten it to output this:

enter image description here

The outermost tag needs to match the name of the action query, it'll either be <courses> or <students>.

Here is my code:

<?php
require_once('./database.php');

if (isset($_GET['format'])) {
    $format = filter_var($_GET['format']);
}

if (isset($_GET['action'])) {
    $action = filter_var($_GET['action'], FILTER_SANITIZE_STRING);
    $tableName = "sk_$action";
}

$query = "SELECT * FROM $tableName";

if (isset($_GET['course'])) {
    $course = filter_input(INPUT_GET, 'course');
    $query .= " WHERE courseID = :course";
}

function arrayToXml($arr, $i = 1, $flag = false)
{
    $sp = "";
    for ($j = 0; $j <= $i; $j++) {
        $sp .= " ";
    }
    foreach ($arr as $key => $val) {
        echo "$sp&lt;" . $key . "&gt;";
        if ($i == 1) echo "\n";
        if (is_array($val)) {
            if (!$flag) {
                echo "\n";
            }
            arrayToXml($val, $i + 5);
            echo "$sp&lt;/" . $key . "&gt;\n";
        } else {
            echo "$val" . "&lt;/" . $key . "&gt;\n";
        }
    }

}

$statement = $db->prepare($query);
$statement->bindValue(':course', $course);
$statement->execute();

$response = $statement->fetchAll(PDO::FETCH_ASSOC);

$statement->closeCursor();

if ($format == 'json') {
    echo json_encode($response);
}
if ($format == 'xml') {
    arrayToXml($response, 1, true);
}

I'm pretty new to PHP and have never worked with XML. All help is appreciated. Thanks.

MMelvin0581
  • 509
  • 6
  • 20
  • yeah i've tried almost all 32 of those answers lol – MMelvin0581 Mar 05 '19 at 01:59
  • 3
    Oh my gosh please don't format XML by doing string concatenation. You're sure to write invalid XML. There are several [XML extensions in PHP](http://php.net/manual/en/refs.xml.php) to choose from. – Bill Karwin Mar 05 '19 at 01:59
  • I'll try anything at this point, been beating my head against this for a while. I literally tried MOST of the answers that this question got marked as a duplicate as and it either renders nothing or much of the same I have above. I'm not good enough with PHP to parse the docs and figure this out, unfortunately. – MMelvin0581 Mar 05 '19 at 02:02

1 Answers1

3
function arrayToXml($arr, $collectionTag, $singleTag) {
    $collection = new SimpleXMLElement("<$collectionTag/>");
    foreach ($arr as $row) {
        $element = $root->addChild($singleTag);
        foreach ($row as $tag => $value) {
            $element->addChild($tag, $value);
        }
    }
    return $collection;
}

$courses = arrayToXml($response, 'courses', 'course');

echo $courses->asXML();

Tested with PHP 7.1.23. Output:

<?xml version="1.0"?>
<courses>
 <course><courseID>cs601</courseID><courseName>Web Application Development</courseName></course>
 <course><courseId>cs602</courseId><courseName>Server-Side Application Development</courseName></course>
 <course><courseId>cs701</courseId><courseName>Rich Internet Application Development</courseName></course>
</courses>

(I added newlines because by default it doesn't add any.)

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828