How to display data from an XML file in an input field with jquery?

2

I am having to develop a function that reads the information that in the fields of a file of type .xml in PHP and trying to return that information in the fields of a form. More so far I can only return the data in the form of an object and from there I am not able to correctly handle the returned data to fill the form fields. I hope it has been clear and I thank you for all your help. HTML

<form action="xmlNF-e/uploadXML.php" id="frmLoadXML" name="frmLoadXML" method="post" enctype="multipart/form-data">
                <!-- CAMPO DE BUSCA DO ARQUIVO XML -->
                <fieldset>
                    <legend>Carregar Arquivo XML:</legend>
                    <div class="campo_carregamento">
                        <input type="file" name="carregarArquivoXML" id="carregarArquivoXML" /><span class="loading_file_xml"></span>
                    </div>
                    <hr>
                </fieldset>
            </form>
            <form action="manutencaoConferenciaFaturamento.php" id="frmDados" name="frmDados" method="post" enctype="multpart/form-data">
                                            Number                                                                                      
                                    <div class="form-group col-lg-2">
                                        <label for="serie_pedido">Série</label>
                                        <input type="text" name="serie_pedido" id="serie_pedido" value="" autocomplete="off" class="form-control" />
                                    </div>
                                    <div class="form-group col-lg-6">
                                        <label for="chave_acesso">Chave de Acesso</label>
                                        <input type="text" name="chave_acesso" id="chave_acesso" value="" autocomplete="off" class="form-control" />
                                    </div>

JQUERY

//Função responsável por carregar o arquivo xml do pc para uma pasta no projeto.
$("#carregarArquivoXML").on("change", function(){
    $.ajax({
        url   : lerArquivoXML.php,
        type  : 'POST',
        cache : false,
        data  : new FormData($("#frmLoadXML")[0]),
        processData: false,
        contentType: false,
        error : function(){
            alert('Erro ao tentar a ação!');
        },
        success : function( obj ){
            //var ex = dados.split('||');
            var dados = $.makeArray(obj);
            $.each(dados, function(chave, valor){
                alert(chave);
            });

            //alert(typeof dado);
            $("#carregarArquivoXML").val('');
            $(".loading_file_xml").fadeOut('slow');                
        },
        beforeSend : function(){
            $(".loading_file_xml").html("<img src='imagens/loading_xs.gif' alt='Enviando...' width='15' height='15' /> Enviando...").fadeIn('slow');        
        }
    }); 
    return false;
});  

readFileXML.php

function lerArquivoXML($nome=NULL){
if( $nome != NULL && file_exists("nota_compras/{$nome}")) {        
    // Converte o documento XML e o transforma em um objeto.
    $xml   = simplexml_load_file("nota_compras/{$nome}"); 
    $dados = array(        
        /* Dados da nota */            
        'nNF'      => $xml->NFe->infNFe->ide->nNF,       //Número da Nota.
        'serie'    => $xml->NFe->infNFe->ide->serie,     //Série da Nota.
        'id'       => $xml->NFe->infNFe->ide->id,        //Chave de Acesso.
    );

    return json_encode($dados);
} else {
    return json_encode('O arquivo não foi encontrado no diretorio!');
}

}

    
asked by anonymous 22.04.2015 / 16:18

1 answer

2

To get only the data inside the object you can use JSON

$.ajax({
        url   : "lerArquivoXML.php",
        type  : 'GET',
        cache : false,
        contentType  : "application/json",
        error : function(){
            alert('Erro ao tentar a ação!');
        },
        success : function(data){
           for(var i in data){
              console.log(data[i]);
           }         
        }
    });

In type use GET instead of POST, as it will only receive data, in the contentType switch to application / json because you are returning a JSON in your PHP .

Read the documentation

    
22.04.2015 / 16:58