Foros del Web » Programando para Internet » PHP »

leer xml con php

Estas en el tema de leer xml con php en el foro de PHP en Foros del Web. Buenos días a todos! Estoy intentando leer un xml con esta estructura: Cita: <hashtable> <entry><string>city</string> <string>Madrid</string></entry> <entry><string>langpref</string> <string>en</string></entry> </hashtable> El código php que estoy usando ...
  #1 (permalink)  
Antiguo 08/08/2011, 02:24
 
Fecha de Ingreso: octubre-2007
Mensajes: 130
Antigüedad: 16 años, 6 meses
Puntos: 2
leer xml con php

Buenos días a todos!
Estoy intentando leer un xml con esta estructura:
Cita:
<hashtable>
<entry><string>city</string>
<string>Madrid</string></entry>
<entry><string>langpref</string>
<string>en</string></entry>
</hashtable>
El código php que estoy usando es este:
Cita:
<?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>';
}
}
?>
Llega a crear las celdas pero vacías, no carga ningún valor. Alguien saben como leer esta estructura de xml? Gracias!!
  #2 (permalink)  
Antiguo 08/08/2011, 07:06
 
Fecha de Ingreso: octubre-2007
Mensajes: 130
Antigüedad: 16 años, 6 meses
Puntos: 2
Respuesta: leer xml con php

Dejo la solución para el que le pueda interesar:
Cita:
<?php
$cdCatalogXMLReader = new CDCatalogXMLReader();
$cdCatalogXMLReader->showCatalogAsTable($url);

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>';
foreach ($cds as $cd) {
// Reading attributes.
echo '<td>' . $cd->getElementsByTagName("string")->item(0)->nodeValue . '</td>';
}
echo '</tr>';
echo '<tr>';
foreach ($cds as $cd) {
// Reading attributes.
echo '<td>' . $cd->getElementsByTagName("string")->item(1)->nodeValue . '</td>';
}
echo '</tr>';
echo '</table>';
}
}
?>
Un saludo y espero que le sirva a alguien!!

Etiquetas: xml
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 19:35.