How to show the data of an XML file in an input field with jquery?

Asked

Viewed 1,048 times

2

I am having to develop a function that reads the information that is in the fields of a file of type . xml in PHP and trying to return this information in the form fields. More so far I can only return the data in the form of an object and from there I am not able to handle properly the returned data to fill in the form fields. I hope I have been clear and I thank everyone for their 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;
});  

lerArchvoXML.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!');
}

}

1 answer

2


To take only the data inside the Voce 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 the type use GET instead of POST, as it will only receive data, on contenttype change to application/json, since Voce is returning a JSON in his PHP.

From a read in documentation

Browser other questions tagged

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