Estoy intentando leer un xml con esta estructura:
Cita:
El código php que estoy usando es este:<hashtable>
<entry><string>city</string>
<string>Madrid</string></entry>
<entry><string>langpref</string>
<string>en</string></entry>
</hashtable>
<entry><string>city</string>
<string>Madrid</string></entry>
<entry><string>langpref</string>
<string>en</string></entry>
</hashtable>
Cita:
Llega a crear las celdas pero vacías, no carga ningún valor. Alguien saben como leer esta estructura de xml? Gracias!! <?php
$cdCatalogXMLReader = new CDCatalogXMLReader();
$cdCatalogXMLReader->showCatalogAsTable('details-by-id.xml');
class CDCatalogXMLReader {
public function showCatalogAsTable($xmlPath) {
// Loads XML.
$doc = new DOMDocument();
$doc->load($xmlPath);
echo $doc->saveXML();
// Reading tag's value.
echo "<h1>Datos cliente</h1>";
// Reading all elements with tag name="cd".
$cds = $doc->getElementsByTagName( "entry" );
echo '<table border="1">';
echo '<tr><th>ID</th><th>Title</th><th>Artist</th><th>Country</th><th>Company</th><th>Price</th><th>Year</th></tr>';
foreach ($cds as $cd) {
echo '<tr>'; // Reading attributes.
echo '<td>' . $cd->getAttribute('entry') . '</td>';
echo '<td>' . $cd->getElementsByTagName("string")->item(0)->getAttribute('value') . '</td>';
echo '<td>' . $cd->getElementsByTagName("string")->item(0)->getAttribute('value') . '</td>';
echo '<td>' . $cd->getElementsByTagName("string")->item(0)->getAttribute('value') . '</td>';
echo '<td>' . $cd->getElementsByTagName("string")->item(0)->getAttribute('value') . '</td>';
echo '<td>' . $cd->getElementsByTagName("string")->item(0)->getAttribute('value') . '</td>';
echo '<td>' . $cd->getElementsByTagName("string")->item(0)->getAttribute('value') . '</td>';
echo '</tr>';
}
echo '</table>';
}
}
?>
$cdCatalogXMLReader = new CDCatalogXMLReader();
$cdCatalogXMLReader->showCatalogAsTable('details-by-id.xml');
class CDCatalogXMLReader {
public function showCatalogAsTable($xmlPath) {
// Loads XML.
$doc = new DOMDocument();
$doc->load($xmlPath);
echo $doc->saveXML();
// Reading tag's value.
echo "<h1>Datos cliente</h1>";
// Reading all elements with tag name="cd".
$cds = $doc->getElementsByTagName( "entry" );
echo '<table border="1">';
echo '<tr><th>ID</th><th>Title</th><th>Artist</th><th>Country</th><th>Company</th><th>Price</th><th>Year</th></tr>';
foreach ($cds as $cd) {
echo '<tr>'; // Reading attributes.
echo '<td>' . $cd->getAttribute('entry') . '</td>';
echo '<td>' . $cd->getElementsByTagName("string")->item(0)->getAttribute('value') . '</td>';
echo '<td>' . $cd->getElementsByTagName("string")->item(0)->getAttribute('value') . '</td>';
echo '<td>' . $cd->getElementsByTagName("string")->item(0)->getAttribute('value') . '</td>';
echo '<td>' . $cd->getElementsByTagName("string")->item(0)->getAttribute('value') . '</td>';
echo '<td>' . $cd->getElementsByTagName("string")->item(0)->getAttribute('value') . '</td>';
echo '<td>' . $cd->getElementsByTagName("string")->item(0)->getAttribute('value') . '</td>';
echo '</tr>';
}
echo '</table>';
}
}
?>