繁体   English   中英

使用 PHP (DOMDocument?) 将 HTML 表格转换为 XML

[英]Convert HTML table to XML using PHP (DOMDocument?)

我希望将以下 HTML 表标记转换为 XML 格式。

<table class='tbl-class'>
  <thead>
    <tr>
      <th>Island</th>
      <th>Number of nights</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Guadeloupe</td>
      <td>1</td>
    </tr>
    <tr>
      <td>Antigua</td>
      <td>5</td>
    </tr>
  <tbody>
</table>

理想情况下,我希望 XML 输出是这样的:

<location>
  <island>Guadeloupe</island>
  <nights>1</nights>
</location>
<location>
  <island>Antigua</island>
  <nights>5</nights>
</location>

我目前正在尝试使用 DOMDocument 来执行此操作,但几乎没有任何使用它的经验。 到目前为止,我已经完成了以下工作: - 我认为我需要在 foreach 循环中做更多的事情,但不确定是什么..

$doc = new DOMDocument();
$doc->load($convertedString);
$classname = 'tbl-class';
$finder = new DomXPath($doc);
$nodes = $finder->query("//*[contains(@class, '$classname')]");

foreach ($nodes as $node) {
  $node->parentNode->removeChild($node);
}

$convertedString = $doc->saveHTML();

我发现使用 SimpleXML 顾名思义 - 更简单。 此代码读取 XML 并像您一样 - 找到<table>元素。

然后使用foreach()它使用 SimpleXML 将元素层次结构引用为对象的能力,因此$table[0]->tbody->tr引用表的<tbody>部分中的<tr>元素。

然后它将每个<td>元素与$headers ...

$xml= simplexml_load_string($convertedString);

$classname = 'tbl-class';
$table = $xml->xpath("//*[contains(@class, '$classname')]");

$headers = ["island", "nights"];
$out = new SimpleXMLElement("<locations />");
foreach ( $table[0]->tbody->tr as $tr ){
    $location = $out->addChild("location");
    $key = 0;
    foreach ( $tr->td as $td )  {
        $location->addChild($headers[$key++], (string)$td);
    }
}

echo $out->asXML();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM