简体   繁体   中英

Convert HTML table to XML using PHP (DOMDocument?)

I'm looking to convert the below HTML Table markup into an XML format.

<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:

<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. 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..

$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. This code reads the XML and as you have - finds the <table> element.

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.

It then combines each of the <td> elements with the corresponding label from $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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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