Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Olá bom dia,
Como posso ler uma informação que está no cabeçalho de um XML de NFe?
Preciso pegar o Id e a Versão que estão no "infNFE", quarta linha abaixo
O trecho do XML é esse:
<?xml version="1.0" encoding="UTF-8" ?>
- <nfeProc versao="1.10" xmlns="[http://www.portalfiscal.inf.br/nfe">](http://www.portalfiscal.inf.br/nfe)
- <NFe xmlns="[http://www.portalfiscal.inf.br/nfe">](http://www.portalfiscal.inf.br/nfe)
- <infNFe xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance"](http://www.w3.org/2001/XMLSchema-instance) Id="NFe41100878956968004332550050000004171101465722" versao="1.10">
O meu PHP está assim:
<?php
/*------------------------------------------------------------
Data - 02/09/2010
Função - Ler e Salvar informações do XML no banco de dados
--------------------------------------------------------------
*/
header("Content-Type: text/html; charset=ISO8859-1");
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
require_once("../db/conexao.php");
// diretorio onde estao os arquivos - criado a pasta NFEArquivo temporariamente
$dir = "NFEletronica/";
// esse seria o "handler" do diretório
$dh = opendir($dir);
// loop que busca todos os arquivos até que não encontre mais nada
while (false !== ($filename = readdir($dh))) {
// verificando se o arquivo é .xml
if (substr($filename,-4) == ".xml") {
// Composição do caminho
$arquivo = "NFEletronica/".$filename;
// converter XML em objetos
$xml = simplexml_load_file($arquivo);
// print_r($xml);
// Leitura do ramo NFe
$Id = utf8_decode($xml->NFe->infNFe->Id);
$Versao = utf8_decode($xml->NFe->infNFe->versao);
// identificação
$nfNumeroNF = utf8_decode($xml->NFe->infNFe->ide->nNF);
$nfDataEmissao = utf8_decode($xml->NFe->infNFe->ide->dEmi);
// Emitente
$nfCPFCNPJEmit = utf8_decode($xml->NFe->infNFe->emit->CNPJ);
$nfNomeEmit = utf8_decode($xml->NFe->infNFe->emit->xNome);
// Destinatário
$nfCPFCNPJDest = utf8_decode($xml->NFe->infNFe->dest->CPF);
$nfNomeDest = utf8_decode($xml->NFe->infNFe->dest->xNome);
mysql_select_db($database_conexao, $conexao);
$query_BuscaNFE = "SELECT * FROM NFEArquivo WHERE IdNFEArquivo = '$Id'";
$BuscaNFE = mysql_query($query_BuscaNFE, $conexao) or die(mysql_error());
$row_BuscaNFE = mysql_fetch_assoc($BuscaNFE);
$totalRows_BuscaNFE = mysql_num_rows($BuscaNFE);
if($totalRows_BuscaNFE == 0){
// Insere as informações no banco de dados
mysql_select_db($database_conexao, $conexao);
$sqlNFE = "INSERT INTO NFEArquivo(Id, Versao, NumeroNF, DataEmissao, CPF_CNPJ_Emitente, NomeEmitente, CPF_CNPJ_Destinatario, NomeDestinatario, TipoPessoaEmitente, TipoPessoaDestinatario, XMLNF) VALUES ('$Id', '$Versao','$nfNumeroNF', '$nfDataEmissao','$nfCPFCNPJEmit','$nfNomeEmit','$nfCPFCNPJDest','$nfNomeDest',1,1,'x')";
echo $sqlNFE;
$execNFE = mysql_query($sqlNFE, $conexao) or die("Não foi possível realizar a inserção");
}
}
}
?>
Mas não estou conseguindo obter sucesso.
Se puderem me ajudar, agradeço.
Valeu e fiquem na paz
Carregando comentários...