Rename XML nodes and subnodes from an array

0

I have an array, which I want to transform into an xml document, I already have it created but I can not change the names of some nodes and sub-nodes, here's an example of an array to be transformed, the code that does the transformation and an example of the xml that is created.

Example of the Array to be transformed into XML:

array(1) {
 [0]=>
array(31) {
["idCandidato"]=>
string(2) "80"
["nome"]=>
string(14) "Vitor Bonzinho"
["genero"]=>
string(1) "M"
["nacionalidade"]=>
string(6) "Brasil"
["dataNascimento"]=>
string(10) "2017-02-15"
["tipoIdentificacao"]=>
string(18) "carteiraIdentidade"
["numeroIdentificacao"]=>
string(7) "7987987"
["cpf"]=>
string(6) "654654"
["validadeID"]=>
string(10) "2017-02-17"
["morada"]=>
string(20) "Rua do Mormugão 405"
["localidade"]=>
string(25) "S.mamede infesta , Lisboa"
["codPostal"]=>
string(8) "4465-213"
["pais_residencia"]=>
string(8) "Portugal"
["indicativoTlf"]=>
string(3) "123"
["numTelefone"]=>
string(9) "938353294"
["email"]=>
string(17) "[email protected]"
["codCurso"]=>
string(4) "M543"
["docID"]=>
string(114) "http://localhost/candidaturas/candi/mestrados_independentes/fileIdentificacao/facb9a2a4d31608e83888d97cda240d2.pdf"
["fileConclusaoCursoSuperior"]=>
string(123) "http://localhost/candidaturas/candi/mestrados_independentes/fileConclusaoCursoSuperior/c39dfe57ba49ac5d9eee19645a26eaef.pdf"
["fileCertidaoUnidadesCurriculares"]=>
string(129) "http://localhost/candidaturas/candi/mestrados_independentes/fileCertidaoUnidadesCurriculares/0863719cfe014f42a0f23b1576a69dce.pdf"
["fileCV"]=>
string(103) "http://localhost/candidaturas/candi/mestrados_independentes/fileCV/b1bf54e803c29bb555ffcc017019873b.pdf"
["fileCVPersonalizado"]=>
NULL
["filePortfolio"]=>
NULL
["fileCartaRecomendacao"]=>
string(118) "http://localhost/candidaturas/candi/mestrados_independentes/fileCartaRecomendacao/f489a61eada305dbb5f4d4c18090f829.pdf"
["fileCartaMotivacao"]=>
string(115) "http://localhost/candidaturas/candi/mestrados_independentes/fileCartaMotivacao/b8af87adcbac9b1208dea778159abad6.pdf"
["filePropostaProjeto"]=>
NULL
["fileCertificadoEN"]=>
NULL
["fileFotografia"]=>
NULL
["pagSucesso"]=>
string(1) "0"
["linguas"]=>
array(3) {
  [0]=>
  array(2) {
    ["nome"]=>
    string(10) "Português"
    ["nivel"]=>
    string(8) "satisfaz"
  }
  [1]=>
  array(2) {
    ["nome"]=>
    string(5) "Russo"
    ["nivel"]=>
    string(3) "bom"
  }
  [2]=>
  array(2) {
    ["nome"]=>
    string(7) "Alemão"
    ["nivel"]=>
    string(9) "muito bom"
  }
}
["ramos"]=>
array(2) {
  [0]=>
  array(3) {
    ["cod_ramo"]=>
    string(3) "521"
    ["nome"]=>
    string(8) "Sólidos"
    ["opcao"]=>
    string(1) "1"
  }
  [1]=>
  array(3) {
    ["cod_ramo"]=>
    string(3) "522"
    ["nome"]=>
    string(8) "Fluídos"
    ["opcao"]=>
    string(1) "2"
  }
}
}
}

PHP code I use to create XML:

//function defination to convert array to xml
private function array_to_xml($array, &$xml_user_info) {
    $x = 0;
    foreach($array as $key => $value) {  //$key nome do campo $value valor do campo
        if(is_array($value)) { // so entra na lingua e nos ramos
            if(!is_numeric($key)){ // se a $key não for um numero
                $subnode = $xml_user_info->addChild("$key");
                $this->array_to_xml($value, $subnode);
            }else{
                $subnode = $xml_user_info->addChild("item$key");
                $this->array_to_xml($value, $subnode);
            }
        }else {
            $xml_user_info->addChild("$key",htmlspecialchars("$value"));
        }
     }
    }

XML example created:

Inthecreatedxml,thenodemarked"blue" is called "candidate", the red node is called "branch" and the green node is called "lingua" p>     

asked by anonymous 16.02.2017 / 15:08

0 answers