How to transform xml tags to table columns in html?

Asked

Viewed 252 times

0

is the following, I want to transform the xml tags to a table in html, example (beeeem look great, but meets my need):

XML:

<animal>
 <humano>
  <clientes>
   <cliente>
    <Nome_cli>Froslass</nome_cli>
    <Idade_cli>18</Idade_cli>
    <Ende_cli>Rua xyz</Ende_cli>
   </cliente>
   <cliente>
    <Nome_cli>Gengar</nome_cli>
    <Idade_cli>16</Idade_cli>
    <Ende_cli>Rua xyz</Ende_cli>
   </cliente>
  </clientes>
 </humano>
</animal>

HTML:

<html>
 <body>
  <table>
   <thead>
   <tr>
    <th>Nome do Cliente</th>
    <th>Idade</th>                       
    <th>Endereço</th>                        
   </tr>
   </thead>
   <tbody>                    
   <tr>                        
    <td>Froslass</td>                       
    <td>18</td>                       
    <td>Rua xyz</td>                        
   </tr>
   <tr>
    <td>Gengar</td>                        
    <td>16</td>                        
   <td>Rua xyz</td>
   </tr>
  </table>
 </body>
</html>

I wanted to leave him like this: https://i.stack.Imgur.com/RUB6A.jpg In short, I want to get straight from xml, because if someone makes the change in the question of the order of the data, it would be updated directly, the only job I would have was to rename the patterns, type Client Name, etc., AHHHH something else, how do I go through the information directly, in case xml doesn’t need to get xml->animal->human->clients->client and go straight to xml->client and get all the information from these clients? What I think is to always take this xml information and with the help of php or javascript+DOM count which tags have inside the client tag create the columns, and take the value of each of these tags and fill the table: Client Name Froslass Gengar

1 answer

1


On the question

how do I go through the information, in the case of xml need to get xml->animal->human->clients->client and go straight in xml->client and take all information from these clients?

Using the class Domdocument php (5.7) you can do it, follow an example.

$file = new DomDocument();
$file->load("../myXmlFile.xml");
$clientes = $file->getElementsByTagName("clientes");

foreach($clientes as $cliente){
    echo $cliente->getAttribute("Nome_Cli");
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.