Changing the names of XML nodes and subnodes coming from an array

Asked

Viewed 36 times

0

I have an array, which I want to transform into an xml document, I already managed to create it but I can’t change the names of some nodes and subnodes, follows 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 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"));
        }
     }
    }

Example of XML created: inserir a descrição da imagem aqui

In the created xml you wanted the blue-marked Node is called "candidate", the red Node is called "branch" and the green Node is called "language"

  • "Is it straight", "It’s called"? What does that mean? Do you speak Portuguese? Are you using the Translate?

  • I’m sorry I just noticed that if you write <candidate></candidate> it does not appear, leaving the blank, it had written <candidate></candidate> <branch></branch> <language>/language> and showed nothing in this space, however I believe that the name of each Node would not make a difference in the solution

  • I really thought you were a foreigner. But since you managed to solve it, it would be nice to answer your own question with the solution and mark your answer ;)

  • Where did you read?

  • is... then I really don’t understand the language you speak :D

No answers

Browser other questions tagged

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