【问题标题】:Displaying XML Contents in a table with php [closed]使用 php 在表中显示 XML 内容 [关闭]
【发布时间】:2023-04-05 04:05:01
【问题描述】:

我正在尝试将位于 http://data.fcc.gov/api/license-view/basicSearch/getLicenses?searchValue=W1AW 的此 XML 的内容(我使用 W1AW 呼号作为示例)显示在表格中(不是每个条目,只有“licName”、“呼号”、“ serviceDesc”、“statusDesc”和“expiredDate”。)

在我的网站上,我希望用户能够根据 url parremeter ?callsign=choicehere 选择要搜索的呼号(例如:mywebsite.com/callsignresults.php?callsign=w1aw)

我目前有以下代码。我只是让参数工作。

<?php
$file = file_get_contents('http://data.fcc.gov/api/license-view/basicSearch/getLicenses?searchValue=' . ($_GET["callsign"]));
echo $file;
?>

那么我该如何将 XML 数据转换为用户友好的可读表格。

表格示例思路:

| NAME     | CALL SIGN | TYPE    | STATUS  | EXPIRATION DATE |
--------------------------------------------------------------
| John Doe | XXXX      | Amature | Active  |          1/1/13 |
| Jane Doe | X1X1X     | Amature | Expired |          1/1/13 |

API Documentation

【问题讨论】:

  • 呼号里面是什么?
  • 你卡在哪一部分?要读取(解析)XML,您可以使用 DOMSimpleXML
  • 我一直在解析 XML 中的数据。我不太擅长 XML
  • 好问题! :-) 不知道为什么人们会认为它不是......

标签: php html xml


【解决方案1】:

基本上你可以使用 SimpleXML

$file = file_get_contents('http://data.fcc.gov/api/license-view/basicSearch/getLicenses?searchValue=Verizon+Wireless');
$movies = new SimpleXMLElement($file);

echo '<pre>';
print_r($movies);
echo '<pre>';

并遍历获得的对象以显示页面。

【讨论】:

    【解决方案2】:

    基于 Neta Meta 的回答:

    我在实际迭代构建的 XML 对象时遇到了一些麻烦,直到我意识到返回的 XML 的大小写不一致。以下是将该文件解析为表格的一些工作代码:

    <?php
    $xml = new SimpleXMLElement('http://data.fcc.gov/api/license-view/basicSearch/getLicenses?searchValue='.$_GET["callsign"], 0, TRUE);
    ?>
    <table>
      <thead>
        <tr>
          <th>Name</th>
          <th>Call Sign</th>
          <th>Type</th>
          <th>Status</th>
          <th>Expiration Date</th>
        </tr>
      </thead>
      <tbody>
    
    <?php foreach ($xml->Licenses->License as $licenseElement) :?>
        <tr>
          <td><?php echo $licenseElement->licName; ?></td>
          <td><?php echo $licenseElement->callsign; ?></td>
          <td><?php echo $licenseElement->serviceDesc; ?></td>
          <td><?php echo $licenseElement->statusDesc; ?></td>
          <td><?php echo $licenseElement->expiredDate; ?></td>
        </tr>
    <?php endforeach; ?>
      </tbody>
    </table>
    

    【讨论】:

    • 谢谢,这正是我想要完成的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-28
    • 2018-07-23
    • 2014-11-26
    • 2016-08-25
    • 2017-07-16
    • 2016-01-01
    相关资源
    最近更新 更多