Jump to content

simplexml_load_file


jokimen
 Share

Recommended Posts

Bom dia Pessoal,

Estou com um trabalho em PHP por terminar, pois estou com dificuldades em ler um XML e guardar todos

os dados em variáveis e Arrays.

Alguém me pode ajudar?

Coloco em anexo o File XML. Obrigado desde já.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.002.001.03">

<CstmrPmtStsRpt>

<GrpHdr>

<MsgId>MG/20150421/00359</MsgId>

<CreDtTm>2015-04-21T09:12:41</CreDtTm>

</GrpHdr>

<OrgnlGrpInfAndSts>

<OrgnlMsgId>73730</OrgnlMsgId>

<OrgnlMsgNmId>PAIN.001.001.03</OrgnlMsgNmId>

<OrgnlNbOfTxs>27</OrgnlNbOfTxs>

<OrgnlCtrlSum>278258.10</OrgnlCtrlSum>

<StsRsnInf>

<Rsn>

<Prtry>M000</Prtry>

</Rsn>

</StsRsnInf>

</OrgnlGrpInfAndSts>

<OrgnlPmtInfAndSts>

<OrgnlPmtInfId>73730-001</OrgnlPmtInfId>

<OrgnlNbOfTxs>27</OrgnlNbOfTxs>

<OrgnlCtrlSum>278258.10</OrgnlCtrlSum>

<StsRsnInf>

<Rsn>

<Prtry>L000</Prtry>

</Rsn>

</StsRsnInf>

<TxInfAndSts>

<StsId>301</StsId>

<OrgnlEndToEndId>10859415</OrgnlEndToEndId>

<StsRsnInf>

<Rsn>

<Cd>0000</Cd>

</Rsn>

</StsRsnInf>

<OrgnlTxRef>

<Amt>

<InstdAmt Ccy="EUR">12345.00</InstdAmt>

</Amt>

<ReqdColltnDt>2015-04-20</ReqdColltnDt>

<PmtTpInf>

<SeqTp></SeqTp>

</PmtTpInf>

<PmtMtd>TRF</PmtMtd>

<Dbtr>

<Nm>NOME ORDENANTE.</Nm>

</Dbtr>

<DbtrAcct>

<Id>

<IBAN>PT50003601239910123456789</IBAN>

</Id>

</DbtrAcct>

<DbtrAgt>

<FinInstnId>

<BIC>MPIOPTPL</BIC>

</FinInstnId>

</DbtrAgt>

<CdtrAgt>

<FinInstnId>

<BIC>BCOMPTPL</BIC>

</FinInstnId>

</CdtrAgt>

<Cdtr>

<Nm>NOME DESTINATÁRIO</Nm>

</Cdtr>

<CdtrAcct>

<Id>

<IBAN>PT50003300004567890123456</IBAN>

</Id>

</CdtrAcct>

</OrgnlTxRef>

</TxInfAndSts>

<TxInfAndSts>

<StsId>301</StsId>

<OrgnlEndToEndId>10859415</OrgnlEndToEndId>

<StsRsnInf>

<Rsn>

<Cd>0000</Cd>

</Rsn>

</StsRsnInf>

<OrgnlTxRef>

<Amt>

<InstdAmt Ccy="EUR">12345.00</InstdAmt>

</Amt>

<ReqdColltnDt>2015-04-20</ReqdColltnDt>

<PmtTpInf>

<SeqTp></SeqTp>

</PmtTpInf>

<PmtMtd>TRF</PmtMtd>

<Dbtr>

<Nm>NOME ORDENANTE.</Nm>

</Dbtr>

<DbtrAcct>

<Id>

<IBAN>PT50003601239910123456789</IBAN>

</Id>

</DbtrAcct>

<DbtrAgt>

<FinInstnId>

<BIC>MPIOPTPL</BIC>

</FinInstnId>

</DbtrAgt>

<CdtrAgt>

<FinInstnId>

<BIC>BCOMPTPL</BIC>

</FinInstnId>

</CdtrAgt>

<Cdtr>

<Nm>NOME DESTINATÁRIO</Nm>

</Cdtr>

<CdtrAcct>

<Id>

<IBAN>PT50003300004567890123456</IBAN>

</Id>

</CdtrAcct>

</OrgnlTxRef>

</TxInfAndSts>

<TxInfAndSts>

<StsId>301</StsId>

<OrgnlEndToEndId>10859415</OrgnlEndToEndId>

<StsRsnInf>

<Rsn>

<Cd>0000</Cd>

</Rsn>

</StsRsnInf>

<OrgnlTxRef>

<Amt>

<InstdAmt Ccy="EUR">12345.00</InstdAmt>

</Amt>

<ReqdColltnDt>2015-04-20</ReqdColltnDt>

<PmtTpInf>

<SeqTp></SeqTp>

</PmtTpInf>

<PmtMtd>TRF</PmtMtd>

<Dbtr>

<Nm>NOME ORDENANTE.</Nm>

</Dbtr>

<DbtrAcct>

<Id>

<IBAN>PT50003601239910123456789</IBAN>

</Id>

</DbtrAcct>

<DbtrAgt>

<FinInstnId>

<BIC>MPIOPTPL</BIC>

</FinInstnId>

</DbtrAgt>

<CdtrAgt>

<FinInstnId>

<BIC>BCOMPTPL</BIC>

</FinInstnId>

</CdtrAgt>

<Cdtr>

<Nm>NOME DESTINATÁRIO</Nm>

</Cdtr>

<CdtrAcct>

<Id>

<IBAN>PT50003300004567890123456</IBAN>

</Id>

</CdtrAcct>

</OrgnlTxRef>

</TxInfAndSts>

</OrgnlPmtInfAndSts>

</CstmrPmtStsRpt>

</Document>

Link to comment
Share on other sites

Qual o problema? O proprio titulo do topico criado por ti indica o que necessitas de fazer

Boas Happy,

Certo eu criei este script

<?php

$dadosQueSeRepetem = array();

$i = 0;

while ($i <= 5000) {

$xml = simplexml_load_file('C:/wwwroot/www-php/resposta.xml');

foreach($xml->resposta as $resposta)

{

if(empty($resposta)){

$i=5000;break;

}

/*Header*/

$MsgId= $resposta->MsgId;

$CreDtTm= $resposta->CreDtTm;

/*OrgnlGrpInfAndSts*/

$OrgnlMsgId= $resposta->OrgnlMsgId;

$OrgnlMsgNmId= $resposta->OrgnlMsgNmId;

$OrgnlNbOfTxs= $resposta->OrgnlNbOfTxs;

$OrgnlCtrlSum= $resposta->OrgnlCtrlSum;

$Prtry= $resposta->Prtry;

/*OrgnlPmtInfAndSts*/

$OrgnlPmtInfId= $resposta->OrgnlPmtInfId;

$OrgnlNbOfTxs= $resposta->OrgnlNbOfTxs;

$OrgnlCtrlSum= $resposta->OrgnlCtrlSum;

$Prtry= $resposta->Prtry;

/*TxInfAndSts*/

$StsId = $resposta ->StsId;

$OrgnlEndToEndId = $resposta ->OrgnlEndToEndId;

$RsnCd = $resposta ->Rsn->Cd;

$InstdAmt = $resposta ->InstdAmt;

$ReqdColltnDt = $resposta ->ReqdColltnDt;

$SeqTp = $resposta ->SeqTp;

$PmtMtd = $resposta ->PmtMtd;

$DbtrNm = $resposta ->Dbtr ->Nm;

$IBAN = $resposta ->IBAN;

$DbtrAgtBIC = $resposta ->DbtrAgt ->FinInstnId ->BIC;

$CdtrAgtBIC = $resposta ->CdtrAgt ->FinInstnId ->BIC;

$CdtrNm = $resposta ->Cdtr ->Nm;

$CdtrAcctIBAN = $resposta ->CdtrAcct ->Id ->IBAN;

$dadosQueSeRepetem[] = array("StsId" => $StsId, "OrgnlEndToEndId" => $OrgnlEndToEndId, "RsnCd" => $RsnCd, "InstdAmt" => $InstdAmt, "ReqdColltnDt" => $ReqdColltnDt,

"SeqTp" => $SeqTp, "PmtMtd" => $PmtMtd, "DbtrNm" => $DbtrNm, "IBAN" => $IBAN, "DbtrAgtBIC" => $DbtrAgtBIC, "CdtrAgtBIC" => $CdtrAgtBIC,

"CdtrNm" => $CdtrNm, "CdtrAcctIBAN" => $CdtrAcctIBAN);

echo $MsgId;

echo $CreDtTm;

echo $OrgnlMsgId;

echo $OrgnlMsgNmId;

echo $OrgnlNbOfTxs;

echo $OrgnlCtrlSum;

echo $Prtry;

echo $OrgnlPmtInfId;

echo $OrgnlNbOfTxs;

echo $OrgnlCtrlSum;

echo $Prtry;

echo $StsId;

echo $OrgnlEndToEndId;

echo $RsnCd;

echo $InstdAmt;

echo $ReqdColltnDt;

echo $SeqTp;

echo $PmtMtd;

echo $DbtrNm;

echo $IBAN;

echo $DbtrAgtBIC;

echo $CdtrAgtBIC;

echo $CdtrNm;

echo $CdtrAcctIBAN;

$i++;

}

}

?>

Mas depois quando executo ele não lê o file XML e dá este erro "Fatal error: Maximum execution time of 30 seconds exceeded in C:\wwwroot\www-php\new.php on line 8"

Link to comment
Share on other sites

não foi isso que perguntei ...

Sou novo no PHP e pensei que era a solução mas devia apenas fazer parsing da tag TxInfAndSts

Happy consegues me ajudar a resolver isto, tenho de entregar hoje ao final do dia a funcionar e só me falta isto

Link to comment
Share on other sites

não foi isso que perguntei ...

Boas Happy

Já resolvi a situação de ler o xml com recurso a outra função contudo necessitava correr vários ciclos à tag TxInfAndSts

pois podem existir várias transações, contudo quando corro o ciclo o script não responde, se correr o script sem um ciclo

funciona tudo bem.

Aqui fica o meu código

<?php

$dadosQueSeRepetem = array();

$doc = new DOMDocument();

$doc->load('resposta.xml');

$respostasGrpHdr = $doc->getElementsByTagName("GrpHdr");

foreach( $respostasGrpHdr as $respostaGrpHdr)

{

$MsgIds = $respostaGrpHdr->getElementsByTagName("MsgId");

$MsgId = $MsgIds->item(0)->nodeValue;

$CreDtTms = $respostaGrpHdr->getElementsByTagName("CreDtTm");

$CreDtTm = $CreDtTms->item(0)->nodeValue;

}

$respostasOrgnlGrpInfAndSts = $doc->getElementsByTagName("OrgnlGrpInfAndSts");

foreach( $respostasOrgnlGrpInfAndSts as $respostasOrgnlGrpInfAndSt)

{

$OrgnlMsgIds = $respostasOrgnlGrpInfAndSt->getElementsByTagName("OrgnlMsgId");

$OrgnlMsgId = $OrgnlMsgIds->item(0)->nodeValue;

$OrgnlMsgNmIds = $respostasOrgnlGrpInfAndSt->getElementsByTagName("OrgnlMsgNmId");

$OrgnlMsgNmId = $OrgnlMsgNmIds->item(0)->nodeValue;

$OrgnlNbOfTxs = $respostasOrgnlGrpInfAndSt->getElementsByTagName("OrgnlNbOfTxs");

$OrgnlNbOfTx = $OrgnlNbOfTxs->item(0)->nodeValue;

$OrgnlCtrlSums = $respostasOrgnlGrpInfAndSt->getElementsByTagName("OrgnlCtrlSum");

$OrgnlCtrlSum = $OrgnlCtrlSums->item(0)->nodeValue;

$Prtrys = $respostasOrgnlGrpInfAndSt->getElementsByTagName("Prtry");

$Prtry = $Prtrys->item(0)->nodeValue;

}

$respostasOrgnlPmtInfAndSts = $doc->getElementsByTagName("OrgnlPmtInfAndSts");

foreach( $respostasOrgnlPmtInfAndSts as $respostasOrgnlPmtInfAndSt)

{

$OrgnlPmtInfIds = $respostasOrgnlPmtInfAndSt->getElementsByTagName("OrgnlPmtInfId");

$OrgnlPmtInfId = $OrgnlPmtInfIds->item(0)->nodeValue;

$OOrgnlNbOfTxs = $respostasOrgnlPmtInfAndSt->getElementsByTagName("OrgnlNbOfTxs");

$OOrgnlNbOfTx = $OOrgnlNbOfTxs->item(0)->nodeValue;

$OOrgnlCtrlSums = $respostasOrgnlPmtInfAndSt->getElementsByTagName("OrgnlCtrlSum");

$OOrgnlCtrlSum = $OOrgnlCtrlSums->item(0)->nodeValue;

$PPrtrys = $respostasOrgnlPmtInfAndSt->getElementsByTagName("Prtry");

$PPrtry = $PPrtrys->item(0)->nodeValue;

$i = 0;

while ($i <= 50) {

if(empty($respostasOrgnlPmtInfAndSts)){

$i=50;break;

}

$StsIds = $respostasOrgnlPmtInfAndSt->getElementsByTagName("StsId");

$StsId = $StsIds->item(0)->nodeValue;

$OrgnlEndToEndIds = $respostasOrgnlPmtInfAndSt->getElementsByTagName("OrgnlEndToEndId");

$OrgnlEndToEndId = $OrgnlEndToEndIds->item(0)->nodeValue;

$Cds = $respostasOrgnlPmtInfAndSt->getElementsByTagName("Cd");

$Cd = $Cds->item(0)->nodeValue;

$InstdAmts = $respostasOrgnlPmtInfAndSt->getElementsByTagName("InstdAmt");

$InstdAmt = $InstdAmts->item(0)->nodeValue;

$ReqdColltnDts = $respostasOrgnlPmtInfAndSt->getElementsByTagName("ReqdColltnDt");

$ReqdColltnDt = $ReqdColltnDts->item(0)->nodeValue;

$SeqTps = $respostasOrgnlPmtInfAndSt->getElementsByTagName("SeqTp");

$SeqTp = $SeqTps->item(0)->nodeValue;

$PmtMtds = $respostasOrgnlPmtInfAndSt->getElementsByTagName("PmtMtd");

$PmtMtd = $PmtMtds->item(0)->nodeValue;

$respostasDbtrs = $doc->getElementsByTagName("Dbtr");

foreach( $respostasDbtrs as $respostasDbtr)

{

$DbtrNms = $respostasDbtr->getElementsByTagName("Nm");

$DbtrNm = $DbtrNms->item(0)->nodeValue;

}

$respostasDbtrAccts = $doc->getElementsByTagName("DbtrAcct");

foreach( $respostasDbtrAccts as $respostasDbtrAcct)

{

$DbtrAcctIBANs = $respostasDbtrAcct->getElementsByTagName("IBAN");

$DbtrAcctIBAN = $DbtrAcctIBANs->item(0)->nodeValue;

}

$respostasDbtrAgts = $doc->getElementsByTagName("DbtrAgt");

foreach( $respostasDbtrAgts as $respostasDbtrAgt)

{

$DbtrAgtBICs = $respostasDbtrAgt->getElementsByTagName("BIC");

$DbtrAgtBIC = $DbtrAgtBICs->item(0)->nodeValue;

}

$respostasCdtrAgts = $doc->getElementsByTagName("CdtrAgt");

foreach( $respostasCdtrAgts as $respostasCdtrAgt)

{

$CdtrAgtBICs = $respostasCdtrAgt->getElementsByTagName("BIC");

$CdtrAgtBIC = $CdtrAgtBICs->item(0)->nodeValue;

}

$respostasCdtrs = $doc->getElementsByTagName("Cdtr");

foreach( $respostasCdtrs as $respostasCdtr)

{

$CdtrNms = $respostasCdtr->getElementsByTagName("Nm");

$CdtrNm = $CdtrNms->item(0)->nodeValue;

}

$respostasCdtrAccts = $doc->getElementsByTagName("CdtrAcct");

foreach( $respostasCdtrAccts as $respostasCdtrAcct)

{

$CdtrAcctIBANs = $respostasCdtrAcct->getElementsByTagName("IBAN");

$CdtrAcctIBAN = $CdtrAcctIBANs->item(0)->nodeValue;

}

$dadosQueSeRepetem[] = array("StsId" => $StsId, "OrgnlEndToEndId" => $OrgnlEndToEndId, "Cd" => $Cd, "InstdAmt" => $InstdAmt, "ReqdColltnDt" => $ReqdColltnDt,

"SeqTp" => $SeqTp, "PmtMtd" => $PmtMtd, "DbtrNm" => $DbtrNm, "DbtrAcctIBAN" => $DbtrAcctIBAN, "DbtrAgtBIC" => $DbtrAgtBIC, "CdtrAgtBIC" => $CdtrAgtBIC,

"CdtrNm" => $CdtrNm, "CdtrAcctIBAN" => $CdtrAcctIBAN);

}

}

echo "$MsgId - $CreDtTm<br>";

echo "$OrgnlMsgId - $OrgnlMsgNmId - $OrgnlNbOfTx - $OrgnlCtrlSum - $Prtry<br>";

echo "$OrgnlPmtInfId - $OOrgnlNbOfTx - $OOrgnlNbOfTx - $OOrgnlCtrlSum - $PPrtry<br>";

print_r ($dadosQueSeRepetem);

?>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.