Reputation: 557
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();
Upvotes: 1
Views: 614
Reputation: 57121
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();
Upvotes: 1