Extraer HTML de un archivo XML usando simpleXML
Estoy leyendo un archivo xml generado por una aplicación de terceros que incluye lo siguiente:
<Cell>
<Comment ss:Author="Mark Baker">
<ss:Data xmlns="http://www.w3.org/TR/REC-html40"><B><Font html:Face="Tahoma" html:Size="8" html:Color="#000000">Mark Baker:</Font></B><Font html:Face="Tahoma" html:Size="8" html:Color="#000000"> Comment 1 - No align</Font></ss:Data>
</Comment>
</Cell>
Lo que estoy tratando de hacer es acceder a los datos sin procesar desde el elemento Celda-> Comentario-> Datos "tal cual" o como un bloque real de marcado (X) HTML (preferiblemente el último).
if (isset($cell->Comment)) {
echo 'comment found<br />';
$commentAttributes = $cell->Comment->attributes($namespaces['ss']);
if (isset($commentAttributes->Author)) {
echo 'Author: ',(string)$commentAttributes->Author,'<br />';
}
$commentData = $cell->Comment->children($namespaces['ss']);
var_dump($commentData);
echo '<br />';
}
me da
comment found
Author: Mark Baker
object(SimpleXMLElement)#130 (2) { ["@attributes"]=> array(1) { ["Author"]=> string(10) "Mark Baker" } ["Data"]=> object(SimpleXMLElement)#129 (0) { } }
mientras
if (isset($cell->Comment)) {
echo 'comment found<br />';
$commentAttributes = $cell->Comment->attributes($namespaces['ss']);
if (isset($commentAttributes->Author)) {
echo 'Author: ',(string)$commentAttributes->Author,'<br />';
}
$commentData = $cell->Comment->Data->children();
var_dump($commentData);
echo '<br />';
}
me da
comment found
Author: Mark Baker
object(SimpleXMLElement)#129 (2) { ["B"]=> object(SimpleXMLElement)#118 (1) { ["Font"]=> string(11) "Mark Baker:" } ["Font"]=> string(21) " Comment 1 - No align" }
Desafortunadamente, simpleXML parece estar tratando todo el elemento como una serie de nodos XML. Estoy seguro de que debería poder obtener estos datos sin procesar sin bucles complejos o alimentar el elemento a un analizador DOM; tal vez usando el espacio de nombres xmlns = "http://www.w3.org/TR/REC-html40" para extraer esto limpiamente, pero no puedo entender cómo.
Cualquier ayuda apreciada.
Un ejemplo más complejo de los datos XML:
<Cell>
<Comment ss:Author="Mark Baker">
<ss:Data xmlns="http://www.w3.org/TR/REC-html40">
<B><Font html:Face="Tahoma" html:Size="8" html:Color="#000000">Mark Baker:</Font></B><Font html:Face="Tahoma" html:Size="8" html:Color="#000000"> </Font><B><Font html:Face="Tahoma" x:Family="Swiss" html:Size="8" html:Color="#000000">Rich </Font><U><Font html:Face="Tahoma" x:Family="Swiss" html:Size="8" html:Color="#FF0000">Text </Font></U><Font html:Face="Tahoma" x:Family="Swiss" html:Size="8" html:Color="#000000">Comment</Font></B><Font html:Face="Tahoma" html:Size="8" html:Color="#000000"> Center Aligned</Font>
</ss:Data>
</Comment>
</Cell>