Using a series of foreach
for something like this is to take an immense turn into something that can be solved simply, using DOMDocument::saveHTML
combined with DOMDocument::getElementsByTagName
More efficient example without loops:
$dom = new DOMDocument();
$dom->load('arq.xml');
$tables = $dom->getElementsByTagName('table');
if ($tables->length) {
$table = $tables->item(0);//Pega a primeira tabela
echo $dom->saveHTML($table); //Exibe o conteudo da tabela
} else {
echo 'Tabela não encontrada';
}
Sample test online: link
Documentation for useful functions for manipulating DOM with PHP:
- upload an XML file or stream: link
- Load a string in XML format: link
- Load a string in HTML format : link
- Load a string in HTML format : link
- element by the ID: link
- Get one or more elements (
DOMNodeList
) for tag name: link
- Get one or more elements (
DOMNodeList
) for the name of the tag that uses the document's namespace: link
- Returns the DOM content as a string in XML format: link
- Returns the DOM content as a string in HTML format: link