1

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();
0

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.