Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP在HTML表中呈现XML输出_Php_Html_Xml_Dom_Html Table - Fatal编程技术网

使用PHP在HTML表中呈现XML输出

使用PHP在HTML表中呈现XML输出,php,html,xml,dom,html-table,Php,Html,Xml,Dom,Html Table,我用PHP编写了XML输出,如下所示 <Twitch_XML> <twitch> <Bird id="1"> <Species>Willow ptarmigan</Species> <Age>2</Age> <Sex>Male</Sex> <Location>London</Location>

我用PHP编写了XML输出,如下所示

<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>
但当我运行此命令时,它会给我一个错误:

Warning: Invalid argument supplied for foreach() in
C:\Winginx\home\localhost\public_html\chamith\twitch_id.php on line 67
这是我用来生成XML输出的全部PHP代码

    $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();
我想在表中填充这些XML数据,如何解决这个问题?

这里有几个问题:

1。无效的XML
查看并测试XML。原来这条线

https://www.rspb.org.uk/Images/greypartridge_tcm9-17615.jpg?width=530&crop=(196,364,1006,820)
正在导致问题,因为它包含无效字符。请参阅此网站上的。您可能需要查看解决方案

2。错误引用XML对象

foreach ($twitcdm->Twitch_XML->twitch as $xmv) {
    // etc.
}
  • $twitcdm
    表示根节点
  • 您忘记了上述XML路径中的
    节点
改为:

foreach ($twitcdm->twitch->Bird as $xmv) {
    echo $xmv->Image . PHP_EOL;
    // etc.
}
请参阅使用
CDATA
在XML中包含无效字符和在
foreach
中包含正确的XML路径的工作示例:
(使用SimpleXML而不是DOM,不过没什么大不了的)

注意事项:

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

由于您自己创建XML,因此可以在其中包含无效字符的处理。此外,我建议在命名节点时使用一致的大小写,例如
,但是

请有人帮我解决这个问题??为什么不直接从数据库生成表而不在两者之间使用XML?