When importing an xml file it comes blank

Asked

Viewed 83 times

0

I created this following xml file:

<?xml version="1.0" encoding="UTF-8"?>
<usuarios>
    <usuario>
        <nome>Andrêy Ferraz</nome>
        <idade>25</idade>
    </usuario>
    <usuario>
        <nome>Carlos Meira</nome>
        <idade>30</idade>
    </usuario>
    <usuario>
        <nome>Fernando Amarelo</nome>
        <idade>40</idade>
    </usuario>
    <usuario>
        <nome>Carlos Santos</nome>
        <idade>50</idade>
    </usuario>
    <usuario>
        <nome>Jose Santana</nome>
        <idade>78</idade>
    </usuario>
    <usuario>
        <nome>Marcus Santos</nome>
        <idade>56</idade>
    </usuario>
    <usuario>
        <nome>Jose Ferreira</nome>
        <idade>67</idade>
    </usuario>
    <usuario>
        <nome>Wallatas Silva</nome>
        <idade>78</idade>
    </usuario>
    <usuario>
        <nome>Marcus Vieira</nome>
        <idade>34</idade>
    </usuario>
</usuarios>

Here I created a case that allowed me to import this file and identify its location on my server:

case"importar"; //importando arquivo xml
            importarUsuario ($conexao, "usuario/_usuarios.xml");
            break;

Here I run the function and try to print the file on the screen:

function importarUsuario ($conexao, $arquivosXml){

    $xml = simplexml_load_file($arquivosXml);
    print_r($xml);  

So if you’ll just give me all the blanks, someone would be kind enough to show me where my mistake is??

2 answers

1


With the code snippets shown there is no error. Putting everything together in a file (except xml) and passing the right condition to the switch, everything works. See:

<?php
$condicao = 'importar';
$conexao = null;

switch($condicao){
    case "importar"; //importando arquivo xml
            importarUsuario ($conexao, "c.xml");
        break;
}

function importarUsuario ($conexao, $arquivosXml){
    $x = simplexml_load_file('c.xml');
    print_r($x);
}
?>

The code above prints:

SimpleXMLElement Object ( [usuario] => Array ( [0] => SimpleXMLElement Object ( [nome] => Andrêy Ferraz [idade] => 25 ) [1] => SimpleXMLElement Object ( [nome] => Carlos Meira [idade] => 30 ) [2] => SimpleXMLElement Object ( [nome] => Fernando Amarelo [idade] => 40 ) [3] => SimpleXMLElement Object ( [nome] => Carlos Santos [idade] => 50 ) [4] => SimpleXMLElement Object ( [nome] => Jose Santana [idade] => 78 ) [5] => SimpleXMLElement Object ( [nome] => Marcus Santos [idade] => 56 ) [6] => SimpleXMLElement Object ( [nome] => Jose Ferreira [idade] => 67 ) [7] => SimpleXMLElement Object ( [nome] => Wallatas Silva [idade] => 78 ) [8] => SimpleXMLElement Object ( [nome] => Marcus Vieira [idade] => 34 ) ) ) 
  • Thank you so much, so I can solve it just another way. I’ll show you answering below.

0

For the solution I did as follows in the case:

case"importar"; //importando arquivo xml
            importarUsuario ($conexao);
            break;

And in the role I had to do as follows:

function importarUsuario ($conexao){

    $xml = simplexml_load_file('_usuarios.xml');
    print_r($xml);

That way avoided to appear all blank, I honestly did not understand why this occurred, it would be perhaps due to the fact gave this using version 7 of PHP, I think not right?

Browser other questions tagged

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