Search code examples
phphtmlsimplexml

php xml listing in body part of html


I'm having a xml file (changes.xml) that look like this:

<changelog>
   <version name="1.0.20">
     <bug id="5">a</bug>
     <bug id="4">b</bug>
     <bug id="3">c</bug>
     <task id="2">d</task>
    </version>
    <version name="1.0.19">
     <bug id="1">a</bug>
    </version>
</changelog>

I loading this file with php this way:

$file = "path/to/file/changes.xml";
$version = $_GET['version'];
$xml = simplexml_load_file($file);
$path = "/changelog/version[@name='$version']";
$res = $xml->xpath($path);

$object = $res[0];

$bugs = $object->xpath("bug");
$tasks = $object->xpath("task");

when iterating all nodes right after

    while(list( , $node) = each($bugs)) {
        echo "NODE:" . $node . "<br>";
    }

I am able to print them:

NODE:a
NODE:b
NODE:c

but in the body part of the html it doesn't work as expected:

<?
function h($string) {
    return htmlentities($string, ENT_COMPAT, "UTF-8");
}?>

<?='<?xml version="1.0" encoding="utf-8" ?>' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Changelog Version <?=h($version) ?></title>
<style>
li, h1,h2 { font-family: arial; } 
</style>
</head>
<body>

<h1>Changelog Version <?=h($version) ?></h1>

<? if($bugs) { ?>
<h2>Bugs:</h2>
<ul>
<? while(list( , $node) = each($bugs)) { ?>
    <li>
        <?=h($node) ?>
    </li>
<? } ?>
</ul>
<? } ?>

</body>
</html>

I just get a single <li></li>-item that has no content:

Changelog Version 1.0.20

Bugs:

the outcoming source looks like this in the browser:

NODE:a<br>NODE:b<br>NODE:c<br>
<?xml version="1.0" encoding="utf-8" ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Changelog Version 1.0.20</title>
<style>
li, h1,h2 { font-family: arial; } 
</style>
</head>
<body>

<h1>Changelog Version 1.0.20</h1>

<? if($bugs) { ?>
<h2>Bugs:</h2>
<ul>
<? while(list( , $node) = each($bugs)) { ?>
    <li>
            </li>
<? } ?>
</ul>
<? } ?>

</body>
</html>

What am I missing here?


Solution

  • If you look at the HTML you've generated. it has PHP still in it. Clearly, this should have been run by the server, and not be visible.

    This has nothing to do with XML parsing, but is just a setting in PHP: to use <? rather than <?php, you need to enable the short_open_tags setting.