0

I have XML output written in PHP which looks like below

<Twitch_XML>
  <twitch>
    <Bird id="1">
      <Species>Willow ptarmigan</Species>
      <Age>2</Age>
      <Sex>Male</Sex>
      <Location>London</Location>
      <Image>
        http://www.kidzone.ws/images-changed/birds/willow_ptarmigan.jpg
      </Image>
      <User>Chamith</User>
    </Bird>
    <Bird id="5">
      <Species>Grey partridge</Species>
      <Age>2</Age>
      <Sex>Male</Sex>
      <Location>London</Location>
      <Image>
        https://www.rspb.org.uk/Images/greypartridge_tcm9-17615.jpg?width=530&crop=(196,364,1006,820)
      </Image>
      <User>Raveen</User>
    </Bird>
  </twitch>
</Twitch_XML>

I want to put this output inside a HTML table. Please note that I'm not using external XML document. I retrieve data from MySQL and print directly to XML using savexml() method

To render data inside the HTML table I tried to use the following code:

$twitcdm->saveXML();
foreach($twitcdm->Twitch_XML->twitch as $xmv){
    echo "<tr>";
    echo "<td>{$xmv->id}</td>";
    echo "<td>{$xmv->Species}</td>";
    echo "<td>{$xmv->Age}</td>";
    echo "<td>{$xmv->Sex}</td>";
    echo "<td>{$xmv->Location}</td>";
    echo "<td>{$xmv->Image}</td>";
    echo "<td>{$xmv->User}</td>";
    echo "<br />";
    echo "</tr>";
}

But when I run this it gives me an error:

Warning: Invalid argument supplied for foreach() in
C:\Winginx\home\localhost\public_html\chamith\twitch_id.php on line 67

This is the whole PHP code that I used to generate XML output

    $id = $_GET["twitch_id"];
    $command = "SELECT  id,species,age,sex,location,image,username FROM chamith_twitch.twitch  WHERE id = $id";
    $dboutput = $mysqlconnection->query($command);
    $mysqlconnection->close();
    $no_of_raws  = $dboutput->num_rows;


    if ($no_of_raws > 0) {
        while ($line = $dboutput->fetch_array()) {
            $lines[] = $line;
        }

        $dboutput->close();
        $twitcdm = new DOMDocument();
        // $twitcdm->formatOutput = true;
        $twitcdm->appendChild($twitcdm->createElement('Twitch_XML'));
        $fetch_xml = $twitcdm->documentElement;     
        $xmldoc = $twitcdm->createElement('twitch');


        foreach ($lines as $line) {
            $dbpkey  = $line['id'];

            $twitch = $twitcdm->createElement('Bird');
            $twitch->setAttribute("id", $line['id']);
            $Species = $twitcdm->createElement('Species');
            $age = $twitcdm->createElement('Age');
            $Sex  = $twitcdm->createElement('Sex');
            $address = $twitcdm->createElement('Location');
            $twitch_photo = $twitcdm->createElement('Image');
            $record_owner = $twitcdm->createElement('User');

            $header    = $twitcdm->createTextNode($line['species']);
            $Species->appendChild($header);
            $twitch->appendChild($Species);        
            $header    = $twitcdm->createTextNode($line['age']);
            $age->appendChild($header);
            $twitch->appendChild($age);
            $header = $twitcdm->createTextNode($line['sex']);
            $Sex->appendChild($header);
            $twitch->appendChild($Sex);          
            $header    = $twitcdm->createTextNode($line['location']);
            $address->appendChild($header);
            $twitch->appendChild($address); 
            $header  = $twitcdm->createTextNode($line['image']);
            $twitch_photo->appendChild($header);
            $twitch->appendChild($twitch_photo);
            $header   = $twitcdm->createTextNode($line['username']);
            $record_owner->appendChild($header);
            $twitch->appendChild($record_owner);
            $xmldoc->appendChild($twitch);   
        }
        $fetch_xml->appendChild($xmldoc);
        // header('Content-type:  text/xml');
        $twitcdm->saveXML();

I want to populate these XML data inside table, how can I fix this ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
rafalefighter
  • 714
  • 2
  • 11
  • 39

1 Answers1

0

Several issues here:

1. invalid XML
see http://www.xmlvalidation.com and test your XML. Turns out that this line

https://www.rspb.org.uk/Images/greypartridge_tcm9-17615.jpg?width=530&crop=(196,364,1006,820)

is causing problems because it contains invalid characters. See Invalid Characters in XML on this site. You may want to check out How do you make strings XML"safe"? for solutions.

2. wrong referencing of the XML object

foreach ($twitcdm->Twitch_XML->twitch as $xmv) {
    // etc.
}
  • $twitcdm represents the root node <Twitch_XML>
  • you forgot the <Bird> node in the above XML path

Do instead:

foreach ($twitcdm->twitch->Bird as $xmv) {
    echo $xmv->Image . PHP_EOL;
    // etc.
}

See a working example using CDATA to include invalid characters in the XML and a correct XML path in the foreach: https://eval.in/321334
(using SimpleXML instead of DOM, no big deal though)

Notes:

Since you create the XML yourself, you can include handling of invalid characters there. Moreover, I'd advise to use consistent uppercase/lowercase in naming nodes, e.g. <twitch> but <Bird>.

Community
  • 1
  • 1
michi
  • 6,565
  • 4
  • 33
  • 56