简体   繁体   English

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

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

I'm looking to convert the below HTML Table markup into an XML format.我希望将以下 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>

I would ideally like the XML output to be something like this:理想情况下,我希望 XML 输出是这样的:

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

I'm currently attempting to use DOMDocument to do this but have little experience with it to get anywhere.我目前正在尝试使用 DOMDocument 来执行此操作,但几乎没有任何使用它的经验。 So far i've done the following: - I think there's much more i need to be doing in the foreach loop but unsure what..到目前为止,我已经完成了以下工作: - 我认为我需要在 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();

I find that using SimpleXML is as it's name implies - simpler.我发现使用 SimpleXML 顾名思义 - 更简单。 This code reads the XML and as you have - finds the <table> element.此代码读取 XML 并像您一样 - 找到<table>元素。

Then using foreach() it uses SimpleXML's ability to refer to the element hierarchy as objects, so $table[0]->tbody->tr refers to the <tr> elements in the <tbody> section of the table.然后使用foreach()它使用 SimpleXML 将元素层次结构引用为对象的能力,因此$table[0]->tbody->tr引用表的<tbody>部分中的<tr>元素。

It then combines each of the <td> elements with the corresponding label from $headers ...然后它将每个<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