使用 php 在表格中显示 XML 内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16930765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:54:05  来源:igfitidea点击:

Displaying XML Contents in a table with php

phphtmlxml

提问by Justin Harper

I am trying to have the contents of this XML located at http://data.fcc.gov/api/license-view/basicSearch/getLicenses?searchValue=W1AW(I'm using the W1AW call sign as an example) displayed in a table (not every entry, only "licName", "callsign", "serviceDesc", "statusDesc", and "expiredDate".)

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

On my website I want the use to be able to choose what call sign to search based on the url parremeter ?callsign=choicehere (ex: mywebsite.com/callsignresults.php?callsign=w1aw)

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

I currently have the following code. I only got the parameter working.

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

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

So how do I go about converting the XML data into a user friendly readable table.

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

Table Example Idea:

表示例思想:

| 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

API 文档

回答by grandivory

Building on Neta Meta's answer:

基于 Neta Meta 的回答:

I was having some trouble actually iterating through the XML object that was built until I realized that the XML returned is inconsistent in its capitalization. Here's some working code to parse that file into a table:

我在遍历构建的 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>

回答by Neta Meta

Well basically you can use SimpleXML :

那么基本上你可以使用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>';

And iterate through the the object you get to display the page.

并遍历您要显示页面的对象。